TBTK
Need a break? Support the development by playing Polarity Puzzles
SerializableVector.h
1 /* Copyright 2017 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
17 
24 #ifndef COM_DAFER45_TBTK_SERIALIZABLE_VECTOR
25 #define COM_DAFER45_TBTK_SERIALIZABLE_VECTOR
26 
27 #include "TBTK/Serializable.h"
28 
29 #include <string>
30 #include <vector>
31 
32 #ifndef TBTK_DISABLE_NLOHMANN_JSON
33 # include "TBTK/json.hpp"
34 #endif
35 
36 namespace TBTK{
37 
38 template<typename DataType, bool = std::is_base_of<Serializable, DataType>::value>
39 class SerializableVector : public std::vector<DataType>, Serializable{
40 public:
42  SerializableVector() : std::vector<DataType>(){};
43 
45  SerializableVector(
46  const std::vector<DataType> &v
47  ) : std::vector<DataType>(v){};
48 
51  SerializableVector(const std::string &serialization, Mode mode);
52 
54  std::string serialize(Mode mode) const;
55 private:
56 };
57 
58 template<typename DataType>
59 class SerializableVector<DataType, true> :
60  public std::vector<DataType>, Serializable
61 {
62 public:
64  SerializableVector() : std::vector<DataType>(){};
65 
67  SerializableVector(
68  const std::vector<DataType> &v
69  ) : std::vector<DataType>(v){};
70 
73  SerializableVector(const std::string &serialization, Mode mode);
74 
76  std::string serialize(Mode mode) const;
77 private:
78 };
79 
80 template<typename DataType>
81 class SerializableVector<DataType, false> :
82  public std::vector<DataType>, Serializable
83 {
84 public:
86  SerializableVector() : std::vector<DataType>(){};
87 
89  SerializableVector(
90  const std::vector<DataType> &v
91  ) : std::vector<DataType>(v){};
92 
95  SerializableVector(const std::string &serialization, Mode mode);
96 
98  std::string serialize(Mode mode) const;
99 private:
100 };
101 
102 #ifndef TBTK_DISABLE_NLOHMANN_JSON
103 
104 template<typename DataType>
105 SerializableVector<DataType, false>::SerializableVector(
106  const std::string &serialization,
107  Mode mode
108 ){
109  TBTKAssert(
110  validate(serialization, "SerializableVector", mode),
111  "SerializableVector::SerializableVector()",
112  "Unable to parse string as SerializableVector '" << serialization << "'.",
113  ""
114  );
115 
116  switch(mode){
117  case Mode::JSON:
118  try{
119  nlohmann::json j = nlohmann::json::parse(serialization);
120  nlohmann::json elements = j.at("elements");
121  for(
122  nlohmann::json::iterator it = elements.begin();
123  it < elements.end();
124  ++it
125  ){
126  std::vector<DataType>::push_back(
127  Serializable::deserialize<DataType>(*it, mode)
128  );
129  }
130  }
131  catch(nlohmann::json::exception &e){
132  TBTKExit(
133  "SerializableVector::SerializableVector()",
134  "Unable to parse string as SerializableVector"
135  << " '" << serialization << "'.",
136  ""
137  );
138  }
139 
140  break;
141  default:
142  TBTKExit(
143  "SerializableVector::SerializableVector()",
144  "Only SerializableVector::Mode::JSON is supported yet.",
145  ""
146  );
147  }
148 }
149 
150 template<typename DataType>
151 SerializableVector<DataType, true>::SerializableVector(
152  const std::string &serialization,
153  Mode mode
154 ){
155  TBTKAssert(
156  validate(serialization, "SerializableVector", mode),
157  "SerializableVector::SerializableVector()",
158  "Unable to parse string as SerializableVector '" << serialization << "'.",
159  ""
160  );
161 
162  switch(mode){
163  case Mode::JSON:
164  try{
165  nlohmann::json j = nlohmann::json::parse(serialization);
166  nlohmann::json elements = j.at("elements");
167  for(
168  nlohmann::json::iterator it = elements.begin();
169  it < elements.end();
170  ++it
171  ){
172  std::vector<DataType>::push_back(
173  DataType(*it, mode)
174  );
175  }
176  }
177  catch(nlohmann::json::exception &e){
178  TBTKExit(
179  "SerializableVector::SerializableVector()",
180  "Unable to parse string as SerializableVector"
181  << " '" << serialization << "'.",
182  ""
183  );
184  }
185 
186  break;
187  default:
188  TBTKExit(
189  "SerializableVector::SerializableVector()",
190  "Only SerializableVector::Mode::JSON is supported yet.",
191  ""
192  );
193  }
194 }
195 
196 template<typename DataType>
197 std::string SerializableVector<DataType, false>::serialize(Mode mode) const{
198  switch(mode){
199  case Mode::JSON:
200  {
201  nlohmann::json j;
202  j["id"] = "SerializableVector";
203  j["elements"] = nlohmann::json::array();
204  for(unsigned int n = 0; n < std::vector<DataType>::size(); n++){
205  j["elements"].push_back(
207  std::vector<DataType>::at(n),
208  mode
209  )
210  );
211  }
212 
213  return j.dump();
214  }
215  default:
216  TBTKExit(
217  "SerializableVector::serialize()",
218  "Only Serializable::Mode::JSON is supported yet.",
219  ""
220  );
221  }
222 }
223 
224 template<typename DataType>
225 std::string SerializableVector<DataType, true>::serialize(Mode mode) const{
226  switch(mode){
227  case Mode::JSON:
228  {
229  nlohmann::json j;
230  j["id"] = "SerializableVector";
231  j["elements"] = nlohmann::json::array();
232  for(unsigned int n = 0; n < std::vector<DataType>::size(); n++){
233  j["elements"].push_back(
234  std::vector<DataType>::at(n).serialize(mode)
235  );
236  }
237 
238  return j.dump();
239  }
240  default:
241  TBTKExit(
242  "SerializableVector::serialize()",
243  "Only Serializable::Mode::JSON is supported yet.",
244  ""
245  );
246  }
247 }
248 
249 #endif
250 
251 }; //End of namespace TBTK
252 
253 #endif
254 
Definition: Boolean.h:32
virtual std::string serialize(Mode mode) const =0
Abstract base class for serializable objects.