23 #ifndef COM_DAFER45_TBTK_SERIALIZABLE
24 #define COM_DAFER45_TBTK_SERIALIZABLE
26 #include "TBTK/SpinMatrix.h"
32 #include <type_traits>
40 class HoppingAmplitude;
41 template<
typename DataType>
class CArray;
47 enum class Mode {Debug, Binary, XML, JSON};
53 static bool hasID(
const std::string &serialization,
Mode mode);
56 static std::string
getID(
const std::string &serialization,
Mode mode);
76 const std::string &serialization,
77 const std::string &containerID,
78 const std::string &componentID,
79 const std::string &componentName,
85 const std::string &serialization,
86 const std::string &
id,
92 const std::string &serialization,
97 static std::vector<std::string>
split(
98 const std::string &content,
108 template<
typename DataType>
109 static typename std::enable_if<!std::is_pointer<DataType>::value, std::string>::type
serialize(
const DataType &data,
Mode mode);
110 template<
typename DataType>
111 static typename std::enable_if<std::is_pointer<DataType>::value, std::string>::type
serialize(
const DataType &data,
Mode mode);
121 template<
typename DataType>
123 const std::string &serialization,
128 static std::string _serialize(
bool b,
Mode mode);
131 static void _deserialize(
132 const std::string &serialization,
138 static std::string _serialize(
int i,
Mode mode);
141 static void _deserialize(
142 const std::string &serialization,
148 static std::string _serialize(
unsigned int u,
Mode mode);
151 static void _deserialize(
152 const std::string &serialization,
158 static std::string _serialize(
double d,
Mode mode);
161 static void _deserialize(
162 const std::string &serialization,
168 static std::string _serialize(std::complex<double> c,
Mode mode);
171 static void _deserialize(
172 const std::string &serialization,
173 std::complex<double> *c,
181 static void _deserialize(
182 const std::string &serialization,
192 const std::string &serialization,
194 std::string component
204 template<
typename DataType>
friend class CArray;
208 template<
typename DataType>
210 return data.serialize(mode);
213 template<
typename DataType>
214 typename std::enable_if<std::is_pointer<DataType>::value, std::string>::type
Serializable::serialize(
const DataType &data, Mode mode){
215 return data->serialize(mode);
225 return _serialize(data, mode);
230 return _serialize(data, mode);
235 const std::complex<double> &data,
238 return _serialize(data, mode);
243 return _serialize(data, mode);
248 return _serialize(data, mode);
253 TBTKNotYetImplemented(
"Serializable::serialize<SpinMatrix>()");
258 return _serialize(data, mode);
263 const std::vector<std::complex<double>> &data,
266 TBTKNotYetImplemented(
267 "Serializable::serialize<std::vector<std::complex<double>>>()"
271 template<
typename DataType>
273 return DataType(serialization, mode);
279 _deserialize(serialization, &i, mode);
286 _deserialize(serialization, &i, mode);
293 _deserialize(serialization, &d, mode);
299 const std::string &serialization,
302 std::complex<double> c;
303 _deserialize(serialization, &c, mode);
309 const std::string &serialization,
312 TBTKNotYetImplemented(
"Serializable::deserialize<SpinMatrix>()");
317 const std::string &serialization,
321 _deserialize(serialization, &s, mode);
327 const std::string &serialization,
330 TBTKNotYetImplemented(
331 "Serializable::deserialize<std::vector<std::complex<double>>>()"
335 inline std::string Serializable::_serialize(
bool b, Mode mode){
340 std::stringstream ss;
347 "Serializable::serialize()",
348 "Only Mode::Debug is supported yet.",
354 inline void Serializable::_deserialize(
355 const std::string &serialization,
363 std::stringstream ss;
364 ss.str(serialization);
371 "Serializable::deserialize()",
372 "Only Mode::Debug is supported yet.",
378 inline std::string Serializable::_serialize(
int i, Mode mode){
383 std::stringstream ss;
390 "Serializable::serialize()",
391 "Only Mode::Debug is supported yet.",
397 inline void Serializable::_deserialize(
398 const std::string &serialization,
406 std::stringstream ss;
407 ss.str(serialization);
414 "Serializable::deserialize()",
415 "Only Mode::Debug is supported yet.",
421 inline std::string Serializable::_serialize(
unsigned int u, Mode mode){
426 std::stringstream ss;
433 "Serializable::serialize()",
434 "Only Mode::Debug is supported yet.",
440 inline void Serializable::_deserialize(
441 const std::string &serialization,
449 std::stringstream ss;
450 ss.str(serialization);
457 "Serializable::deserialize()",
458 "Only Mode::Debug is supported yet.",
464 inline std::string Serializable::_serialize(
double d, Mode mode){
469 std::stringstream ss;
476 "Serializable::serialize()",
477 "Only Mode::Debug is supported yet.",
483 inline void Serializable::_deserialize(
484 const std::string &serialization,
492 std::stringstream ss;
493 ss.str(serialization);
500 "Serializable::deserialize()",
501 "Only Mode::Debug is supported yet.",
507 inline std::string Serializable::_serialize(std::complex<double> c, Mode mode){
512 std::stringstream ss;
519 "Serializable::serialize()",
520 "Only Mode::Debug is supported yet.",
526 inline void Serializable::_deserialize(
527 const std::string &serialization,
528 std::complex<double> *c,
535 std::stringstream ss;
536 ss.str(serialization);
543 "Serializable::deserialize()",
544 "Only Mode::Debug is supported yet.",
550 inline std::string Serializable::_serialize(Statistics statistics, Mode mode){
554 std::stringstream ss;
556 case Statistics::FermiDirac:
557 case Statistics::BoseEinstein:
558 ss << static_cast<int>(statistics);
562 "Serializable::serialize()",
563 "Unknown Statistics type '" <<
static_cast<int>(statistics) <<
"'",
564 "This should never happen, contact the developer."
572 std::stringstream ss;
574 case Statistics::FermiDirac:
577 case Statistics::BoseEinstein:
578 ss <<
"BoseEinstein";
582 "Serializable::serialize()",
583 "Unknown Statistics type '" <<
static_cast<int>(statistics) <<
"'",
584 "This should never happen, contact the developer."
592 "Serializable::serialize()",
593 "Only Mode::Debug is supported yet.",
599 inline void Serializable::_deserialize(
600 const std::string &serialization,
601 Statistics *statistics,
607 std::stringstream ss;
608 ss.str(serialization);
612 case static_cast<int>(Statistics::FermiDirac):
613 *statistics = Statistics::FermiDirac;
615 case static_cast<int>(Statistics::BoseEinstein):
616 *statistics = Statistics::BoseEinstein;
620 "Serializable::serialize()",
621 "Unknown Statistics type '" << i <<
"'",
622 "The serialization string is either corrupted"
623 <<
" or the the serialization was created with"
624 <<
" a newer version of TBTK that supports"
625 <<
" more types of Statistics."
633 if(serialization.compare(
"FermiDirac") == 0){
634 *statistics = Statistics::FermiDirac;
636 else if(serialization.compare(
"BoseEinstein") == 0){
637 *statistics = Statistics::BoseEinstein;
641 "Serializable::serialize()",
642 "Unknown Statistics type '" << serialization
644 "The serialization string is either corrupted"
645 <<
" or the the serialization was created with"
646 <<
" a newer version of TBTK that supports"
647 <<
" more types of Statistics."
655 "Serializable::deserialize()",
656 "Only Mode::Debug is supported yet.",