IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
BoostSerializationOperators.h
Go to the documentation of this file.
1 // BoostSerializationOperators.h:
3 // ------------------------------
4 //
14 
15 #ifndef __IPSDKSERIALIZATION_BOOSTSERIALIZATIONOPERATORS_H__
16 #define __IPSDKSERIALIZATION_BOOSTSERIALIZATIONOPERATORS_H__
17 
22 #include <IPSDKSerialization/Archive/BaseIArchive.h>
23 #include <IPSDKSerialization/Archive/BaseOArchive.h>
24 #include <boost/filesystem/path.hpp>
25 #include <boost/date_time/gregorian/gregorian.hpp>
26 #include <boost/date_time/posix_time/posix_time.hpp>
27 #include <boost/optional.hpp>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/variant.hpp>
30 #include <boost/type_traits/remove_const.hpp>
31 
32 namespace ipsdk {
33 
36 
39 IPSDKSERIALIZATION_API BaseIArchive&
40 operator>> (BaseIArchive& ar, const SerializationIItem<boost::filesystem::path>& item);
41 IPSDKSERIALIZATION_API BaseOArchive&
42 operator<< (BaseOArchive& ar, const SerializationOItem<boost::filesystem::path>& item);
44 
47 IPSDKSERIALIZATION_API BaseIArchive&
48 operator>> (BaseIArchive& ar, const SerializationIItem<boost::gregorian::date>& item);
49 IPSDKSERIALIZATION_API BaseOArchive&
50 operator<< (BaseOArchive& ar, const SerializationOItem<boost::gregorian::date>& item);
52 
55 IPSDKSERIALIZATION_API BaseIArchive&
56 operator>> (BaseIArchive& ar, const SerializationIItem<boost::posix_time::ptime>& item);
57 IPSDKSERIALIZATION_API BaseOArchive&
58 operator<< (BaseOArchive& ar, const SerializationOItem<boost::posix_time::ptime>& item);
60 
63 template <class T>
64 BaseIArchive&
65 operator>> (BaseIArchive& ar, const SerializationIItem<boost::optional<T> >& item)
66 {
67  // retrieve input optional
68  boost::optional<T>& opt = item._value;
69 
70  // start a new archive section
71  IPSDK_START_ARCHIVE_SECTION(ar, item._itemName);
72 
73  // start a new archive section
74  IPSDK_START_ARCHIVE_SECTION(ar, "optional");
75 
76  bool bInit;
77  ar >> IPSDK_MAKE_IITEM_VALUE(bInit);
78  if(bInit) {
79  T value;
80  ar >> IPSDK_MAKE_IITEM_VALUE(value);
81  opt = value;
82  }
83  else {
84  opt = boost::none;
85  }
86 
87  // end archive section
88  IPSDK_END_ARCHIVE_SECTION("optional");
89 
90  // end archive section
91  IPSDK_END_ARCHIVE_SECTION(item._itemName);
92 
93  return ar;
94 }
95 
96 template <class T>
97 BaseOArchive&
98 operator<< (BaseOArchive& ar, const SerializationOItem<boost::optional<T> >& item)
99 {
100  // retrieve input collection
101  const boost::optional<T>& opt = item._value;
102 
103  // start a new archive section
104  IPSDK_START_ARCHIVE_SECTION(ar, item._itemName);
105 
106  // start a new archive section
107  IPSDK_START_ARCHIVE_SECTION(ar, "optional");
108 
109  const bool bInit = opt.is_initialized();
110  ar << IPSDK_MAKE_OITEM_VALUE(bInit);
111  if(bInit) {
112  const T& value = opt.get();
113  ar << IPSDK_MAKE_OITEM_VALUE(value);
114  }
115 
116  // end archive section
117  IPSDK_END_ARCHIVE_SECTION("optional");
118 
119  // end archive section
120  IPSDK_END_ARCHIVE_SECTION(item._itemName);
121 
122  return ar;
123 }
124 
126 
129 
134 
135 template <class T>
136 BaseIArchive&
137 operator>> (BaseIArchive& ar, const SerializationIItem<boost::shared_ptr<T> >& item)
138 {
139  // start a new archive section
140  IPSDK_START_ARCHIVE_SECTION(ar, item._itemName);
141 
142  // start a new archive section
143  IPSDK_START_ARCHIVE_SECTION(ar, "shared_ptr<>");
144 
145  // serialize base shared pointer
146  SerializationObjectPtr pObject;
147  ar.read(IPSDK_MAKE_IITEM_VALUE(pObject));
148 
149  // update of derived one
150  item._value = boost::static_pointer_cast<T>(pObject);
151 
152  // end archive section
153  IPSDK_END_ARCHIVE_SECTION("shared_ptr<>");
154 
155  // end archive section
156  IPSDK_END_ARCHIVE_SECTION(item._itemName);
157 
158  return ar;
159 }
160 
161 template <class T>
162 BaseOArchive&
163 operator<< (BaseOArchive& ar, const SerializationOItem<boost::shared_ptr<T> >& item)
164 {
165  // start a new archive section
166  IPSDK_START_ARCHIVE_SECTION(ar, item._itemName);
167 
168  // start a new archive section
169  IPSDK_START_ARCHIVE_SECTION(ar, "shared_ptr<" + T::getTypeName() + ">");
170 
171  // retrieve base shared pointer
172  SerializationObjectConstPtr pObject = item._value;
173 
174  // serialize it
175  ar.write(IPSDK_MAKE_OITEM_VALUE(pObject));
176 
177  // end archive section
178  IPSDK_END_ARCHIVE_SECTION("shared_ptr<" + T::getTypeName() + ">");
179 
180  // end archive section
181  IPSDK_END_ARCHIVE_SECTION(item._itemName);
182 
183  return ar;
184 }
185 
187 
190 
193 reportVariantSerializationError(const int expectedTypeId,
194  const std::string& typeSeqStr);
195 
196 template <typename Sequence>
197 struct VariantLoadManager
198 {
199  struct NullLoad {
200 
201  template<class VariantType>
202  static void apply(BaseIArchive& ar, int which, VariantType& v)
203  {}
204  };
205 
206  struct IterLoad {
207 
208  template<class VariantType>
209  static void apply(BaseIArchive& ar, int which, VariantType& v)
210  {
211  if(which == 0){
212 
213  typedef typename boost::mpl::front<Sequence>::type head_type;
214  head_type value;
215  ar >> IPSDK_MAKE_IITEM_VALUE(value);
216  v = value;
217  }
218  else {
219 
220  typedef typename boost::mpl::pop_front<Sequence>::type type;
221  VariantLoadManager<type>::load(ar, which - 1, v);
222  }
223  }
224  };
225 
226  template<class VariantType>
227  static void load(BaseIArchive& ar, int which, VariantType& v) {
228 
229  typedef typename boost::mpl::eval_if<boost::mpl::empty<Sequence>,
230  boost::mpl::identity<IterLoad>,
231  boost::mpl::identity<NullLoad>
232  >::type typex;
233  typex::apply(ar, which, v);
234  }
235 };
237 
240 template <BOOST_VARIANT_ENUM_PARAMS(class T)>
241 BaseIArchive&
242 operator>> (BaseIArchive& ar, const SerializationIItem<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >& item)
243 {
244  // start a new archive section
245  IPSDK_START_ARCHIVE_SECTION(ar, item._itemName);
246 
247  // start a new archive section
248  IPSDK_START_ARCHIVE_SECTION(ar, "variant");
249 
250  // serialize variant subtype
251  int which;
252  ar >> IPSDK_MAKE_IITEM_VALUE(which);
253 
254  // check for which validity
255  typedef typename boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>::types types;
256  if(which >= boost::mpl::size<types>::value)
257  reportVariantSerializationError(which, BOOST_PP_STRINGIZE((BOOST_VARIANT_ENUM_PARAMS(T))));
258 
259  // load variant value
260  VariantLoadManager<types>::load(ar, which, item._value);
261 
262  // end archive section
263  IPSDK_END_ARCHIVE_SECTION("variant");
264 
265  // end archive section
266  IPSDK_END_ARCHIVE_SECTION(item._itemName);
267 
268  return ar;
269 }
270 
272 struct VariantSaveVisitor : boost::static_visitor<>
273 {
274  VariantSaveVisitor(BaseOArchive& ar) :
275  _ar(ar)
276  {}
277 
278  template<class T>
279  void operator()(T const & value) const
280  {
281  _ar << IPSDK_MAKE_OITEM_VALUE(value);
282  }
283 
284 private:
285  BaseOArchive& _ar;
286 };
288 
289 template <BOOST_VARIANT_ENUM_PARAMS(class T)>
290 BaseOArchive&
291 operator<< (BaseOArchive& ar, const SerializationOItem<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >& item)
292 {
293  // start a new archive section
294  IPSDK_START_ARCHIVE_SECTION(ar, item._itemName);
295 
296  // start a new archive section
297  IPSDK_START_ARCHIVE_SECTION(ar, "variant");
298 
299  // serialize variant subtype
300  const int which = item._value.which();
301  ar << IPSDK_MAKE_OITEM_VALUE(which);
302 
303  // serialization of variant value
304  VariantSaveVisitor visitor(ar);
305  item._value.apply_visitor(visitor);
306 
307  // end archive section
308  IPSDK_END_ARCHIVE_SECTION("variant");
309 
310  // end archive section
311  IPSDK_END_ARCHIVE_SECTION(item._itemName);
312 
313  return ar;
314 }
315 
317 
320 
321 } // end of namespace ipsdk
322 
323 #endif // __IPSDKSERIALIZATION_BOOSTSERIALIZATIONOPERATORS_H__
Main namespace for IPSDK library.
Definition: AlgorithmFunctionEfficiency.h:22
#define IPSDK_END_ARCHIVE_SECTION(unUsed)
macro allowing to end an archive section
Definition: ArchiveSectionMacros.h:29
Serialization operators for basic types.
#define IPSDK_MAKE_IITEM_VALUE(value)
macro for SerializationIItem creation
Definition: SerializationIItemMacros.h:26
boost::shared_ptr< const BaseSerializationObject > SerializationObjectConstPtr
shared pointer to a const base serialization object
Definition: SerializationTypes.h:32
IPSDK_FORCEINLINE ipsdk::simd::IS2Pack< instructionSet, T >::Type load(const T *pT)
load function; loads a simd pack from a buffer in memory
Definition: load.h:37
Macro set allowing to handle input object serialization.
Macro set allowing to handle input object serialization.
virtual void read(const SerializationIItem< BaseSerializationObject > &item)
read an object derived of base class
IPSDKBASEPROCESSING_API RulePtr none()
function allowing to define an empty rule (always true)
#define IPSDK_MAKE_OITEM_VALUE(value)
macro for SerializationOItem creation
Definition: SerializationOItemMacros.h:26
IPSDKGEOMETRY_API bool apply(const BaseGeometryTransform2d &transform, BaseGeometryEntity2d &entity)
function allowing to apply in situ a given transformation on an entity
Base class for serialization input archives.
Definition: BaseIArchive.h:36
boost::shared_ptr< BaseSerializationObject > SerializationObjectPtr
shared pointer to a base serialization object
Definition: SerializationTypes.h:23
#define IPSDK_START_ARCHIVE_SECTION(ar, sectionName)
macro allowing to star an archive section
Definition: ArchiveSectionMacros.h:23
Item used to format inputs during serialization.
Definition: DataItemNodeHdrMacrosDetails.h:42
IPSDKBASESHAPEANALYSIS_API ipsdk::BaseIArchive & operator>>(ipsdk::BaseIArchive &ar, const ipsdk::SerializationIItem< ipsdk::shape::analysis::MeasureId > &item)
serialization of ipsdk::shape::analysis::MeasureId structure
Definition of import/export macro for library.
#define IPSDKSERIALIZATION_API
Import/Export macro for library IPSDKUtil.
Definition: IPSDKSerializationExports.h:27