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 
72  static std::string extractComponent(
73  const std::string &serialization,
74  const std::string &containerID,
75  const std::string &componentID,
76  const std::string &componentName,
77  Mode mode
78  );
79 protected:
81  static bool validate(
82  const std::string &serialization,
83  const std::string &id,
84  Mode mode
85  );
86 
88  static std::string getContent(
89  const std::string &serialization,
90  Mode mode
91  );
92 
94  static std::vector<std::string> split(
95  const std::string &content,
96  Mode mode
97  );
98 
100  static std::string serialize(bool b, Mode mode);
101 
103  static void deserialize(
104  const std::string &serialization,
105  bool *b,
106  Mode mode
107  );
108 
110  static std::string serialize(int i, Mode mode);
111 
113  static void deserialize(
114  const std::string &serialization,
115  int *i,
116  Mode mode
117  );
118 
120  static std::string serialize(unsigned int u, Mode mode);
121 
123  static void deserialize(
124  const std::string &serialization,
125  unsigned int *u,
126  Mode mode
127  );
128 
130  static std::string serialize(double d, Mode mode);
131 
133  static void deserialize(
134  const std::string &serialization,
135  double *d,
136  Mode mode
137  );
138 
140  static std::string serialize(std::complex<double> c, Mode mode);
141 
143  static void deserialize(
144  const std::string &serialization,
145  std::complex<double> *c,
146  Mode mode
147  );
148 
150  static std::string serialize(Statistics s, Mode mode);
151 
153  static void deserialize(
154  const std::string &serialization,
155  Statistics *s,
156  Mode mode
157  );
158 
163  static std::string extract(
164  const std::string &serialization,
165  Mode mode,
166  std::string component
167  );
168 
172  friend class Index;
173  friend class HoppingAmplitude;
174  friend class SourceAmplitude;
175 };
176 
177 inline std::string Serializable::serialize(bool b, Mode mode){
178  switch(mode){
179  case Mode::Debug:
180  case Mode::JSON:
181  {
182  std::stringstream ss;
183  ss << b;
184 
185  return ss.str();
186  }
187  default:
188  TBTKExit(
189  "Serializable::serialize()",
190  "Only Mode::Debug is supported yet.",
191  ""
192  );
193  }
194 }
195 
197  const std::string &serialization,
198  bool *b,
199  Mode mode
200 ){
201  switch(mode){
202  case Mode::Debug:
203  case Mode::JSON:
204  {
205  std::stringstream ss;
206  ss.str(serialization);
207  ss >> *b;
208 
209  break;
210  }
211  default:
212  TBTKExit(
213  "Serializable::deserialize()",
214  "Only Mode::Debug is supported yet.",
215  ""
216  );
217  }
218 }
219 
220 inline std::string Serializable::serialize(int i, Mode mode){
221  switch(mode){
222  case Mode::Debug:
223  case Mode::JSON:
224  {
225  std::stringstream ss;
226  ss << i;
227 
228  return ss.str();
229  }
230  default:
231  TBTKExit(
232  "Serializable::serialize()",
233  "Only Mode::Debug is supported yet.",
234  ""
235  );
236  }
237 }
238 
240  const std::string &serialization,
241  int *i,
242  Mode mode
243 ){
244  switch(mode){
245  case Mode::Debug:
246  case Mode::JSON:
247  {
248  std::stringstream ss;
249  ss.str(serialization);
250  ss >> *i;
251 
252  break;
253  }
254  default:
255  TBTKExit(
256  "Serializable::deserialize()",
257  "Only Mode::Debug is supported yet.",
258  ""
259  );
260  }
261 }
262 
263 inline std::string Serializable::serialize(unsigned int u, Mode mode){
264  switch(mode){
265  case Mode::Debug:
266  case Mode::JSON:
267  {
268  std::stringstream ss;
269  ss << u;
270 
271  return ss.str();
272  }
273  default:
274  TBTKExit(
275  "Serializable::serialize()",
276  "Only Mode::Debug is supported yet.",
277  ""
278  );
279  }
280 }
281 
283  const std::string &serialization,
284  unsigned int *u,
285  Mode mode
286 ){
287  switch(mode){
288  case Mode::Debug:
289  case Mode::JSON:
290  {
291  std::stringstream ss;
292  ss.str(serialization);
293  ss >> *u;
294 
295  break;
296  }
297  default:
298  TBTKExit(
299  "Serializable::deserialize()",
300  "Only Mode::Debug is supported yet.",
301  ""
302  );
303  }
304 }
305 
306 inline std::string Serializable::serialize(double d, Mode mode){
307  switch(mode){
308  case Mode::Debug:
309  case Mode::JSON:
310  {
311  std::stringstream ss;
312  ss << d;
313 
314  return ss.str();
315  }
316  default:
317  TBTKExit(
318  "Serializable::serialize()",
319  "Only Mode::Debug is supported yet.",
320  ""
321  );
322  }
323 }
324 
326  const std::string &serialization,
327  double *d,
328  Mode mode
329 ){
330  switch(mode){
331  case Mode::Debug:
332  case Mode::JSON:
333  {
334  std::stringstream ss;
335  ss.str(serialization);
336  ss >> *d;
337 
338  break;
339  }
340  default:
341  TBTKExit(
342  "Serializable::deserialize()",
343  "Only Mode::Debug is supported yet.",
344  ""
345  );
346  }
347 }
348 
349 inline std::string Serializable::serialize(std::complex<double> c, Mode mode){
350  switch(mode){
351  case Mode::Debug:
352  case Mode::JSON:
353  {
354  std::stringstream ss;
355  ss << c;
356 
357  return ss.str();
358  }
359  default:
360  TBTKExit(
361  "Serializable::serialize()",
362  "Only Mode::Debug is supported yet.",
363  ""
364  );
365  }
366 }
367 
369  const std::string &serialization,
370  std::complex<double> *c,
371  Mode mode
372 ){
373  switch(mode){
374  case Mode::Debug:
375  case Mode::JSON:
376  {
377  std::stringstream ss;
378  ss.str(serialization);
379  ss >> *c;
380 
381  break;
382  }
383  default:
384  TBTKExit(
385  "Serializable::deserialize()",
386  "Only Mode::Debug is supported yet.",
387  ""
388  );
389  }
390 }
391 
392 inline std::string Serializable::serialize(Statistics statistics, Mode mode){
393  switch(mode){
394  case Mode::Debug:
395  {
396  std::stringstream ss;
397  switch(statistics){
398  case Statistics::FermiDirac:
399  case Statistics::BoseEinstein:
400  ss << static_cast<int>(statistics);
401  break;
402  default:
403  TBTKExit(
404  "Serializable::serialize()",
405  "Unknown Statistics type '" << static_cast<int>(statistics) << "'",
406  "This should never happen, contact the developer."
407  );
408  }
409 
410  return ss.str();
411  }
412  case Mode::JSON:
413  {
414  std::stringstream ss;
415  switch(statistics){
416  case Statistics::FermiDirac:
417  ss << "FermiDirac";
418  break;
419  case Statistics::BoseEinstein:
420  ss << "BoseEinstein";
421  break;
422  default:
423  TBTKExit(
424  "Serializable::serialize()",
425  "Unknown Statistics type '" << static_cast<int>(statistics) << "'",
426  "This should never happen, contact the developer."
427  );
428  }
429 
430  return ss.str();
431  }
432  default:
433  TBTKExit(
434  "Serializable::serialize()",
435  "Only Mode::Debug is supported yet.",
436  ""
437  );
438  }
439 }
440 
442  const std::string &serialization,
443  Statistics *statistics,
444  Mode mode
445 ){
446  switch(mode){
447  case Mode::Debug:
448  {
449  std::stringstream ss;
450  ss.str(serialization);
451  int i;
452  ss >> i;
453  switch(i){
454  case static_cast<int>(Statistics::FermiDirac):
455  *statistics = Statistics::FermiDirac;
456  break;
457  case static_cast<int>(Statistics::BoseEinstein):
458  *statistics = Statistics::BoseEinstein;
459  break;
460  default:
461  TBTKExit(
462  "Serializable::serialize()",
463  "Unknown Statistics type '" << i << "'",
464  "The serialization string is either corrupted"
465  << " or the the serialization was created with"
466  << " a newer version of TBTK that supports"
467  << " more types of Statistics."
468  );
469  }
470 
471  break;
472  }
473  case Mode::JSON:
474  {
475  if(serialization.compare("FermiDirac") == 0){
476  *statistics = Statistics::FermiDirac;
477  }
478  else if(serialization.compare("BoseDirac") == 0){
479  *statistics = Statistics::BoseEinstein;
480  }
481  else{
482  TBTKExit(
483  "Serializable::serialize()",
484  "Unknown Statistics type '" << serialization
485  << "'",
486  "The serialization string is either corrupted"
487  << " or the the serialization was created with"
488  << " a newer version of TBTK that supports"
489  << " more types of Statistics."
490  );
491  }
492 
493  break;
494  }
495  default:
496  TBTKExit(
497  "Serializable::deserialize()",
498  "Only Mode::Debug is supported yet.",
499  ""
500  );
501  }
502 }
503 
504 }; //End namespace TBTK
505 
506 #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
static std::string extractComponent(const std::string &serialization, const std::string &containerID, const std::string &componentID, const std::string &componentName, Mode mode)
Flexible physical index.
Definition: Index.h:69
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:196
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