34 |
#include <string> |
#include <string> |
35 |
#include <vector> |
#include <vector> |
36 |
#include <map> |
#include <map> |
37 |
|
#include <time.h> |
38 |
|
#if __cplusplus < 201103L |
39 |
|
# include <tr1/type_traits> |
40 |
|
#endif |
41 |
|
|
42 |
/** @brief Serialization / deserialization framework. |
/** @brief Serialization / deserialization framework. |
43 |
* |
* |
84 |
*/ |
*/ |
85 |
namespace Serialization { |
namespace Serialization { |
86 |
|
|
87 |
|
// just symbol prototyping |
88 |
|
class DataType; |
89 |
|
class Object; |
90 |
|
class Member; |
91 |
class Archive; |
class Archive; |
92 |
|
class ObjectPool; |
93 |
class Exception; |
class Exception; |
94 |
|
|
95 |
typedef std::string String; |
typedef std::string String; |
106 |
OPERATION_DESERIALIZE |
OPERATION_DESERIALIZE |
107 |
}; |
}; |
108 |
|
|
109 |
|
enum time_base_t { |
110 |
|
LOCAL_TIME, |
111 |
|
UTC_TIME |
112 |
|
}; |
113 |
|
|
114 |
template<typename T> |
template<typename T> |
115 |
bool IsEnum(const T& data) { |
bool IsEnum(const T& data) { |
116 |
|
#if __cplusplus < 201103L |
117 |
|
return std::tr1::is_enum<T>::value; |
118 |
|
#else |
119 |
return __is_enum(T); |
return __is_enum(T); |
120 |
|
#endif |
121 |
} |
} |
122 |
|
|
123 |
template<typename T> |
template<typename T> |
124 |
bool IsUnion(const T& data) { |
bool IsUnion(const T& data) { |
125 |
|
#if __cplusplus < 201103L |
126 |
|
return false; // without compiler support we cannot distinguish union from class |
127 |
|
#else |
128 |
return __is_union(T); |
return __is_union(T); |
129 |
|
#endif |
130 |
} |
} |
131 |
|
|
132 |
template<typename T> |
template<typename T> |
133 |
bool IsClass(const T& data) { |
bool IsClass(const T& data) { |
134 |
|
#if __cplusplus < 201103L |
135 |
|
return std::tr1::__is_union_or_class<T>::value; // without compiler support we cannot distinguish union from class |
136 |
|
#else |
137 |
return __is_class(T); |
return __is_class(T); |
138 |
|
#endif |
139 |
} |
} |
140 |
|
|
141 |
/*template<typename T> |
/*template<typename T> |
198 |
|
|
199 |
typedef std::vector<UID> UIDChain; |
typedef std::vector<UID> UIDChain; |
200 |
|
|
201 |
|
// prototyping of private internal friend functions |
202 |
|
static String _encodePrimitiveValue(const Object& obj); |
203 |
|
static DataType _popDataTypeBlob(const char*& p, const char* end); |
204 |
|
static Member _popMemberBlob(const char*& p, const char* end); |
205 |
|
static Object _popObjectBlob(const char*& p, const char* end); |
206 |
|
static void _popPrimitiveValue(const char*& p, const char* end, Object& obj); |
207 |
|
static String _primitiveObjectValueToString(const Object& obj); |
208 |
|
|
209 |
/** @brief Abstract reflection of a native C++ data type. |
/** @brief Abstract reflection of a native C++ data type. |
210 |
* |
* |
211 |
* Provides detailed information about a C++ data type, whether it is a |
* Provides detailed information about a C++ data type, whether it is a |
309 |
bool m_isPointer; |
bool m_isPointer; |
310 |
|
|
311 |
friend DataType _popDataTypeBlob(const char*& p, const char* end); |
friend DataType _popDataTypeBlob(const char*& p, const char* end); |
312 |
|
friend class Archive; |
313 |
}; |
}; |
314 |
|
|
315 |
/** @brief Abstract reflection of a native C++ class/struct's member variable. |
/** @brief Abstract reflection of a native C++ class/struct's member variable. |
383 |
std::vector<Member>& members() { return m_members; } |
std::vector<Member>& members() { return m_members; } |
384 |
const std::vector<Member>& members() const { return m_members; } |
const std::vector<Member>& members() const { return m_members; } |
385 |
Member memberNamed(String name) const; |
Member memberNamed(String name) const; |
386 |
void remove(const Member& member); |
Member memberByUID(const UID& uid) const; |
387 |
std::vector<Member> membersOfType(const DataType& type) const; |
std::vector<Member> membersOfType(const DataType& type) const; |
388 |
int sequenceIndexOf(const Member& member) const; |
int sequenceIndexOf(const Member& member) const; |
389 |
bool isValid() const; |
bool isValid() const; |
394 |
bool operator<(const Object& other) const; |
bool operator<(const Object& other) const; |
395 |
bool operator>(const Object& other) const; |
bool operator>(const Object& other) const; |
396 |
|
|
397 |
|
protected: |
398 |
|
void remove(const Member& member); |
399 |
|
|
400 |
private: |
private: |
401 |
DataType m_type; |
DataType m_type; |
402 |
UIDChain m_uid; |
UIDChain m_uid; |
405 |
RawData m_data; |
RawData m_data; |
406 |
std::vector<Member> m_members; |
std::vector<Member> m_members; |
407 |
|
|
408 |
|
friend String _encodePrimitiveValue(const Object& obj); |
409 |
friend Object _popObjectBlob(const char*& p, const char* end); |
friend Object _popObjectBlob(const char*& p, const char* end); |
410 |
friend void _popPrimitiveValue(const char*& p, const char* end, Object& obj); |
friend void _popPrimitiveValue(const char*& p, const char* end, Object& obj); |
411 |
|
friend String _primitiveObjectValueToString(const Object& obj); |
412 |
|
friend class Archive; |
413 |
}; |
}; |
414 |
|
|
415 |
/** @brief Destination container for serialization, and source container for deserialization. |
/** @brief Destination container for serialization, and source container for deserialization. |
423 |
* @code |
* @code |
424 |
* Archive a; |
* Archive a; |
425 |
* a.serialize(&myRootObject); |
* a.serialize(&myRootObject); |
426 |
* @encode |
* @endcode |
427 |
* Or if you prefer the look of operator based code: |
* Or if you prefer the look of operator based code: |
428 |
* @code |
* @code |
429 |
* Archive a; |
* Archive a; |
430 |
* a << myRootObject; |
* a << myRootObject; |
431 |
* @encode |
* @endcode |
432 |
* The Archive object will then serialize all members of the passed C++ |
* The Archive object will then serialize all members of the passed C++ |
433 |
* object, and will recursively serialize all other C++ objects which it |
* object, and will recursively serialize all other C++ objects which it |
434 |
* contains or points to. So the root object is the starting point for the |
* contains or points to. So the root object is the starting point for the |
443 |
* @code |
* @code |
444 |
* Archive a(rawDataStream); |
* Archive a(rawDataStream); |
445 |
* a.deserialize(&myRootObject); |
* a.deserialize(&myRootObject); |
446 |
* @encode |
* @endcode |
447 |
* Or with operator instead: |
* Or with operator instead: |
448 |
* @code |
* @code |
449 |
* Archive a(rawDataStream); |
* Archive a(rawDataStream); |
450 |
* a >> myRootObject; |
* a >> myRootObject; |
451 |
* @encode |
* @endcode |
452 |
* Now this framework automatically handles serialization and |
* Now this framework automatically handles serialization and |
453 |
* deserialization of fundamental data types automatically for you (like |
* deserialization of fundamental data types automatically for you (like |
454 |
* i.e. char, int, long int, float, double, etc.). However for your own |
* i.e. char, int, long int, float, double, etc.). However for your own |
563 |
deserialize(&obj); |
deserialize(&obj); |
564 |
} |
} |
565 |
|
|
566 |
const RawData& rawData() const { return m_rawData; } |
const RawData& rawData(); |
567 |
virtual String rawDataFormat() const; |
virtual String rawDataFormat() const; |
568 |
|
|
569 |
template<typename T_classType, typename T_memberType> |
template<typename T_classType, typename T_memberType> |
597 |
virtual void decode(const RawData& data); |
virtual void decode(const RawData& data); |
598 |
virtual void decode(const uint8_t* data, size_t size); |
virtual void decode(const uint8_t* data, size_t size); |
599 |
void clear(); |
void clear(); |
600 |
|
bool isModified() const; |
601 |
|
void removeMember(Object& parent, const Member& member); |
602 |
void remove(const Object& obj); |
void remove(const Object& obj); |
603 |
Object& rootObject(); |
Object& rootObject(); |
604 |
Object& objectByUID(const UID& uid); |
Object& objectByUID(const UID& uid); |
605 |
|
void setAutoValue(Object& object, String value); |
606 |
|
void setIntValue(Object& object, int64_t value); |
607 |
|
void setRealValue(Object& object, double value); |
608 |
|
void setBoolValue(Object& object, bool value); |
609 |
|
void setEnumValue(Object& object, uint64_t value); |
610 |
|
String valueAsString(const Object& object); |
611 |
|
String name() const; |
612 |
|
void setName(String name); |
613 |
|
String comment() const; |
614 |
|
void setComment(String comment); |
615 |
|
time_t timeStampCreated() const; |
616 |
|
time_t timeStampModified() const; |
617 |
|
tm dateTimeCreated(time_base_t base = LOCAL_TIME) const; |
618 |
|
tm dateTimeModified(time_base_t base = LOCAL_TIME) const; |
619 |
|
|
620 |
protected: |
protected: |
621 |
// UID resolver for non-pointer types |
// UID resolver for non-pointer types |
722 |
operation_t m_operation; |
operation_t m_operation; |
723 |
UID m_root; |
UID m_root; |
724 |
RawData m_rawData; |
RawData m_rawData; |
725 |
|
bool m_isModified; |
726 |
|
String m_name; |
727 |
|
String m_comment; |
728 |
|
time_t m_timeCreated; |
729 |
|
time_t m_timeModified; |
730 |
}; |
}; |
731 |
|
|
732 |
/** |
/** |