TBTK
Serializable.h
Go to the documentation of this file.
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 
23 #ifndef COM_DAFER45_TBTK_SERIALIZABLE
24 #define COM_DAFER45_TBTK_SERIALIZABLE
25 
26 #include "TBTK/Statistics.h"
27 #include "TBTK/TBTKMacros.h"
28 
29 #include <complex>
30 #include <sstream>
31 #include <vector>
32 
33 namespace TBTK{
34 
35 //Forward declaration of classes that are pseudo-Serializable (that implements
36 //the Serializable interface non-virtually).
37 class Index;
38 class HoppingAmplitude;
39 
41 public:
44  enum class Mode {Debug, Binary, XML, JSON};
45 
47  virtual std::string serialize(Mode mode) const = 0;
48 
50  static bool hasID(const std::string &serialization, Mode mode);
51 
53  static std::string getID(const std::string &serialization, Mode mode);
54 protected:
56  static bool validate(
57  const std::string &serialization,
58  const std::string &id,
59  Mode mode
60  );
61 
63  static std::string getContent(
64  const std::string &serialization,
65  Mode mode
66  );
67 
69  static std::vector<std::string> split(
70  const std::string &content,
71  Mode mode
72  );
73 
75  static std::string serialize(bool b, Mode mode);
76 
78  static void deserialize(
79  const std::string &serialization,
80  bool *b,
81  Mode mode
82  );
83 
85  static std::string serialize(int i, Mode mode);
86 
88  static void deserialize(
89  const std::string &serialization,
90  int *i,
91  Mode mode
92  );
93 
95  static std::string serialize(unsigned int u, Mode mode);
96 
98  static void deserialize(
99  const std::string &serialization,
100  unsigned int *u,
101  Mode mode
102  );
103 
105  static std::string serialize(double d, Mode mode);
106 
108  static void deserialize(
109  const std::string &serialization,
110  double *d,
111  Mode mode
112  );
113 
115  static std::string serialize(std::complex<double> c, Mode mode);
116 
118  static void deserialize(
119  const std::string &serialization,
120  std::complex<double> *c,
121  Mode mode
122  );
123 
125  static std::string serialize(Statistics s, Mode mode);
126 
128  static void deserialize(
129  const std::string &serialization,
130  Statistics *s,
131  Mode mode
132  );
133 
138  static std::string extract(
139  const std::string &serialization,
140  Mode mode,
141  std::string component
142  );
143 
147  friend class Index;
148  friend class HoppingAmplitude;
149  friend class SourceAmplitude;
150 };
151 
152 inline std::string Serializable::serialize(bool b, Mode mode){
153  switch(mode){
154  case Mode::Debug:
155  case Mode::JSON:
156  {
157  std::stringstream ss;
158  ss << b;
159 
160  return ss.str();
161  }
162  default:
163  TBTKExit(
164  "Serializable::serialize()",
165  "Only Mode::Debug is supported yet.",
166  ""
167  );
168  }
169 }
170 
172  const std::string &serialization,
173  bool *b,
174  Mode mode
175 ){
176  switch(mode){
177  case Mode::Debug:
178  case Mode::JSON:
179  {
180  std::stringstream ss;
181  ss.str(serialization);
182  ss >> *b;
183 
184  break;
185  }
186  default:
187  TBTKExit(
188  "Serializable::deserialize()",
189  "Only Mode::Debug is supported yet.",
190  ""
191  );
192  }
193 }
194 
195 inline std::string Serializable::serialize(int i, Mode mode){
196  switch(mode){
197  case Mode::Debug:
198  case Mode::JSON:
199  {
200  std::stringstream ss;
201  ss << i;
202 
203  return ss.str();
204  }
205  default:
206  TBTKExit(
207  "Serializable::serialize()",
208  "Only Mode::Debug is supported yet.",
209  ""
210  );
211  }
212 }
213 
215  const std::string &serialization,
216  int *i,
217  Mode mode
218 ){
219  switch(mode){
220  case Mode::Debug:
221  case Mode::JSON:
222  {
223  std::stringstream ss;
224  ss.str(serialization);
225  ss >> *i;
226 
227  break;
228  }
229  default:
230  TBTKExit(
231  "Serializable::deserialize()",
232  "Only Mode::Debug is supported yet.",
233  ""
234  );
235  }
236 }
237 
238 inline std::string Serializable::serialize(unsigned int u, Mode mode){
239  switch(mode){
240  case Mode::Debug:
241  case Mode::JSON:
242  {
243  std::stringstream ss;
244  ss << u;
245 
246  return ss.str();
247  }
248  default:
249  TBTKExit(
250  "Serializable::serialize()",
251  "Only Mode::Debug is supported yet.",
252  ""
253  );
254  }
255 }
256 
258  const std::string &serialization,
259  unsigned int *u,
260  Mode mode
261 ){
262  switch(mode){
263  case Mode::Debug:
264  case Mode::JSON:
265  {
266  std::stringstream ss;
267  ss.str(serialization);
268  ss >> *u;
269 
270  break;
271  }
272  default:
273  TBTKExit(
274  "Serializable::deserialize()",
275  "Only Mode::Debug is supported yet.",
276  ""
277  );
278  }
279 }
280 
281 inline std::string Serializable::serialize(double d, Mode mode){
282  switch(mode){
283  case Mode::Debug:
284  case Mode::JSON:
285  {
286  std::stringstream ss;
287  ss << d;
288 
289  return ss.str();
290  }
291  default:
292  TBTKExit(
293  "Serializable::serialize()",
294  "Only Mode::Debug is supported yet.",
295  ""
296  );
297  }
298 }
299 
301  const std::string &serialization,
302  double *d,
303  Mode mode
304 ){
305  switch(mode){
306  case Mode::Debug:
307  case Mode::JSON:
308  {
309  std::stringstream ss;
310  ss.str(serialization);
311  ss >> *d;
312 
313  break;
314  }
315  default:
316  TBTKExit(
317  "Serializable::deserialize()",
318  "Only Mode::Debug is supported yet.",
319  ""
320  );
321  }
322 }
323 
324 inline std::string Serializable::serialize(std::complex<double> c, Mode mode){
325  switch(mode){
326  case Mode::Debug:
327  case Mode::JSON:
328  {
329  std::stringstream ss;
330  ss << c;
331 
332  return ss.str();
333  }
334  default:
335  TBTKExit(
336  "Serializable::serialize()",
337  "Only Mode::Debug is supported yet.",
338  ""
339  );
340  }
341 }
342 
344  const std::string &serialization,
345  std::complex<double> *c,
346  Mode mode
347 ){
348  switch(mode){
349  case Mode::Debug:
350  case Mode::JSON:
351  {
352  std::stringstream ss;
353  ss.str(serialization);
354  ss >> *c;
355 
356  break;
357  }
358  default:
359  TBTKExit(
360  "Serializable::deserialize()",
361  "Only Mode::Debug is supported yet.",
362  ""
363  );
364  }
365 }
366 
367 inline std::string Serializable::serialize(Statistics statistics, Mode mode){
368  switch(mode){
369  case Mode::Debug:
370  {
371  std::stringstream ss;
372  switch(statistics){
373  case Statistics::FermiDirac:
374  case Statistics::BoseEinstein:
375  ss << static_cast<int>(statistics);
376  break;
377  default:
378  TBTKExit(
379  "Serializable::serialize()",
380  "Unknown Statistics type '" << static_cast<int>(statistics) << "'",
381  "This should never happen, contact the developer."
382  );
383  }
384 
385  return ss.str();
386  }
387  case Mode::JSON:
388  {
389  std::stringstream ss;
390  switch(statistics){
391  case Statistics::FermiDirac:
392  ss << "FermiDirac";
393  break;
394  case Statistics::BoseEinstein:
395  ss << "BoseEinstein";
396  break;
397  default:
398  TBTKExit(
399  "Serializable::serialize()",
400  "Unknown Statistics type '" << static_cast<int>(statistics) << "'",
401  "This should never happen, contact the developer."
402  );
403  }
404 
405  return ss.str();
406  }
407  default:
408  TBTKExit(
409  "Serializable::serialize()",
410  "Only Mode::Debug is supported yet.",
411  ""
412  );
413  }
414 }
415 
417  const std::string &serialization,
418  Statistics *statistics,
419  Mode mode
420 ){
421  switch(mode){
422  case Mode::Debug:
423  {
424  std::stringstream ss;
425  ss.str(serialization);
426  int i;
427  ss >> i;
428  switch(i){
429  case static_cast<int>(Statistics::FermiDirac):
430  *statistics = Statistics::FermiDirac;
431  break;
432  case static_cast<int>(Statistics::BoseEinstein):
433  *statistics = Statistics::BoseEinstein;
434  break;
435  default:
436  TBTKExit(
437  "Serializable::serialize()",
438  "Unknown Statistics type '" << i << "'",
439  "The serialization string is either corrupted"
440  << " or the the serialization was created with"
441  << " a newer version of TBTK that supports"
442  << " more types of Statistics."
443  );
444  }
445 
446  break;
447  }
448  case Mode::JSON:
449  {
450  if(serialization.compare("FermiDirac") == 0){
451  *statistics = Statistics::FermiDirac;
452  }
453  else if(serialization.compare("BoseDirac") == 0){
454  *statistics = Statistics::BoseEinstein;
455  }
456  else{
457  TBTKExit(
458  "Serializable::serialize()",
459  "Unknown Statistics type '" << serialization
460  << "'",
461  "The serialization string is either corrupted"
462  << " or the the serialization was created with"
463  << " a newer version of TBTK that supports"
464  << " more types of Statistics."
465  );
466  }
467 
468  break;
469  }
470  default:
471  TBTKExit(
472  "Serializable::deserialize()",
473  "Only Mode::Debug is supported yet.",
474  ""
475  );
476  }
477 }
478 
479 }; //End namespace TBTK
480 
481 #endif
static bool hasID(const std::string &serialization, Mode mode)
static std::string extract(const std::string &serialization, Mode mode, std::string component)
Precompiler macros.
Definition: Serializable.h:40
Enum class for Fermi-Dirac and Bose-Einstein statistics.
Source amplitude for equations with a source term.
Definition: SourceAmplitude.h:40
Hopping amplitude from state &#39;from&#39; to state &#39;to&#39;.
Definition: HoppingAmplitude.h:49
Flexible physical index.
Definition: Index.h:70
static std::string getID(const std::string &serialization, Mode mode)
Definition: ModelFactory.h:35
static std::string getContent(const std::string &serialization, Mode mode)
static void deserialize(const std::string &serialization, bool *b, Mode mode)
Definition: Serializable.h:171
Mode
Definition: Serializable.h:44
static bool validate(const std::string &serialization, const std::string &id, Mode mode)
static std::vector< std::string > split(const std::string &content, Mode mode)
virtual std::string serialize(Mode mode) const =0
Statistics
Definition: Statistics.h:29