00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #ifndef VRS_CONTAINER_STLSERIALIZATION_H
00050 #define VRS_CONTAINER_STLSERIALIZATION_H
00051
00052 #include <vrs/serializationmacros.h>
00053 #include <deque>
00054 #include <list>
00055 #include <map>
00056 #include <set>
00057 #include <vector>
00058
00059 #include <vrs/vertexdata.h>
00060
00061 namespace VRS {
00062
00063 template<typename T> struct CanWriteAsRawData { enum { value = false }; };
00064
00065 template<> struct CanWriteAsRawData<char> { enum { value = true }; };
00066 template<> struct CanWriteAsRawData<unsigned char> { enum { value = true }; };
00067 template<> struct CanWriteAsRawData<signed char> { enum { value = true }; };
00068 template<> struct CanWriteAsRawData<short> { enum { value = true }; };
00069 template<> struct CanWriteAsRawData<unsigned short> { enum { value = true }; };
00070 template<> struct CanWriteAsRawData<int> { enum { value = true }; };
00071 template<> struct CanWriteAsRawData<unsigned int> { enum { value = true }; };
00072 template<> struct CanWriteAsRawData<long> { enum { value = true }; };
00073 template<> struct CanWriteAsRawData<unsigned long> { enum { value = true }; };
00074 template<> struct CanWriteAsRawData<float> { enum { value = true }; };
00075 template<> struct CanWriteAsRawData<double> { enum { value = true }; };
00076
00077 template<unsigned int DIM, typename T>
00078 struct CanWriteAsRawData<VertexData<DIM, T> > {
00079 enum { value = CanWriteAsRawData<T>::value };
00080 };
00081
00082 template<typename T1, typename T2>
00083 struct CanWriteAsRawData<std::pair<T1, T2> > {
00084 enum { value = (CanWriteAsRawData<T1>::value && CanWriteAsRawData<T2>::value) };
00085 };
00086
00087 template<typename T>
00088 void serialization(
00089 SerializationManager& manager,
00090 const std::string& attributeName,
00091 std::vector<T>& attributeValue
00092 ) {
00093 VRS_SERIALIZATION_START_CLASS(manager, attributeName, attributeValue);
00094
00095 unsigned int size = static_cast<unsigned int>(attributeValue.size());
00096 serialization(manager, "Size", size);
00097 attributeValue.resize(size);
00098
00099 bool asRaw = manager.canHandleRawData() && CanWriteAsRawData<T>::value;
00100 serialization(manager, "asRaw", asRaw, false);
00101
00102 if(asRaw) {
00103 serializationRawData(manager, "raw", (size > 0) ? &attributeValue[0] : NULL, size * sizeof(T));
00104 } else {
00105 for(unsigned int i = 0; i < size; ++i) {
00106 T obj = attributeValue[i];
00107 serialization(manager, "Value", obj);
00108 attributeValue[i] = obj;
00109 }
00110 }
00111
00112 VRS_SERIALIZATION_END_CLASS(manager, attributeName, attributeValue);
00113 }
00114
00115 inline void serialization(
00116 SerializationManager& manager,
00117 const std::string& attributeName,
00118 std::vector<bool>& attributeValue
00119 ) {
00120 VRS_SERIALIZATION_START_CLASS(manager, attributeName, attributeValue);
00121
00122 unsigned int size = static_cast<unsigned int>(attributeValue.size());
00123 serialization(manager, "Size", size);
00124 attributeValue.resize(size);
00125
00126 bool asRaw = manager.canHandleRawData();
00127 serialization(manager, "asRaw", asRaw, false);
00128
00129 if(asRaw) {
00130 std::vector<char> temp(attributeValue.size(), 0);
00131 if(manager.isWriting()) {
00132 for(size_t i = 0, iEnd = temp.size(); i < iEnd; ++i) {
00133 if(attributeValue[i]) { temp[i] = 1; }
00134 }
00135 }
00136 serializationRawData(manager, "raw", &temp[0], size);
00137 if(manager.isReading()) {
00138 for(size_t i = 0, iEnd = temp.size(); i < iEnd; ++i) {
00139 attributeValue[i] = (temp[i] == 1);
00140 }
00141 }
00142 } else {
00143 for(unsigned int i = 0; i < size; ++i) {
00144 bool obj = attributeValue[i];
00145 serialization(manager, "Value", obj);
00146 attributeValue[i] = obj;
00147 }
00148 }
00149
00150 VRS_SERIALIZATION_END_CLASS(manager, attributeName, attributeValue);
00151 }
00152
00153 template<typename T1, typename T2>
00154 void serialization(
00155 VRS::SerializationManager& manager,
00156 const std::string& attributeName,
00157 std::pair<T1, T2>& attributeValue
00158 ) {
00159 VRS_SERIALIZATION_START_CLASS(manager, attributeName, attributeValue);
00160 serialization(manager, "first", attributeValue.first);
00161 serialization(manager, "second", attributeValue.second);
00162 VRS_SERIALIZATION_END_CLASS(manager, attributeName, attributeValue);
00163 }
00164
00165 template<typename T>
00166 void serialization(
00167 VRS::SerializationManager& manager,
00168 const std::string& attributeName,
00169 std::list<T>& attributeValue
00170 ) {
00171 std::vector<T> vec(attributeValue.begin(), attributeValue.end());
00172 serialization(manager, attributeName, vec);
00173 if(manager.isReading()) {
00174 attributeValue.clear();
00175 attributeValue.insert(attributeValue.end(), vec.begin(), vec.end());
00176 }
00177 }
00178
00179 template<typename T>
00180 void serialization(
00181 VRS::SerializationManager& manager,
00182 const std::string& attributeName,
00183 std::set<T>& attributeValue
00184 ) {
00185 std::vector<T> vec(attributeValue.begin(), attributeValue.end());
00186 serialization(manager, attributeName, vec);
00187 if(manager.isReading()) {
00188 attributeValue.clear();
00189 attributeValue.insert(vec.begin(), vec.end());
00190 }
00191 }
00192
00193 template<typename T1, typename T2>
00194 void serialization(
00195 VRS::SerializationManager& manager,
00196 const std::string& attributeName,
00197 std::map<T1, T2>& attributeValue
00198 ) {
00199 std::vector<std::pair<T1, T2> > vec(attributeValue.begin(), attributeValue.end());
00200 serialization(manager, attributeName, vec);
00201 if(manager.isReading()) {
00202 attributeValue.clear();
00203 attributeValue.insert(vec.begin(), vec.end());
00204 }
00205 }
00206
00207 }
00208
00209 #endif // VRS_CONTAINER_STLSERIALIZATION_H