Bayeux  3.4.1
Core Foundation library for SuperNEMO
i_serializable.h
Go to the documentation of this file.
1 /*
3  * Copyright (C) 2011-2012 Francois Mauger <mauger@lpccaen.in2p3.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21 
22 #ifndef DATATOOLS_I_SERIALIZABLE_H
23 #define DATATOOLS_I_SERIALIZABLE_H
24 
25 // Standard Library:
26 #include <string>
27 #include <typeinfo>
28 #include <memory>
29 
30 // Third Party:
31 // - Boost:
32 #include <boost/serialization/access.hpp>
33 #include <boost/type_traits.hpp>
34 #include <boost/utility/enable_if.hpp>
35 #include <boost/mpl/has_xxx.hpp>
36 // #include <boost/scoped_ptr.hpp>
37 
38 // This project:
42 #include <datatools/utils.h>
43 #ifndef Q_MOC_RUN
45 #endif // Q_MOC_RUN
46 
47 // Main namespace of the datatools library
48 namespace datatools {
49 
52  {
53 
54  public:
55 
58 
60  virtual ~i_serializable();
61 
63  virtual const std::string & get_serial_tag() const = 0;
64 
67 
68 
70 
71 #ifndef Q_MOC_RUN
73 #endif // Q_MOC_RUN
74 
75  };
76 
77 } // end of namespace datatools
78 
79 #ifndef Q_MOC_RUN
80 // Activate reflection layer for the 'datatools::i_serializable' class.
82 #endif // Q_MOC_RUN
83 
84 /******************
85  * Helpful macros *
86  ******************/
87 
88 namespace datatools {
89  template <class T>
90  const std::string & serial_tag();
91 }
92 
93 namespace datatools {
94  template <class T>
95  const std::string & backward_serial_tag(int i = 0);
96 }
97 
99 BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_bsts, backward_serial_tag_support, false)
100 
101 #define DATATOOLS_SERIALIZATION_EXT_SERIAL_TAG_DECLARATION(ClassName) \
102  namespace datatools { \
103  template <> \
104  const std::string & serial_tag< ClassName >(); \
105  } \
106 
107 
108 
119 #define DATATOOLS_SERIALIZATION_SERIAL_TAG_DECLARATION() \
120  public: \
121  static const std::string SERIAL_TAG; \
122  static const std::string & serial_tag(); \
123  virtual const std::string& get_serial_tag() const; \
124 
125 
126 
137 #define DATATOOLS_SERIALIZATION_SERIAL_TAG_IMPLEMENTATION(ClassName,ClassSerialTag) \
138  const std::string ClassName::SERIAL_TAG = ClassSerialTag; \
139  const std::string& ClassName::serial_tag() { \
140  static const std::string _stag(ClassSerialTag); \
141  return _stag; \
142  } \
143  const std::string& ClassName::get_serial_tag() const { \
144  return ClassName::serial_tag(); \
145  } \
146 
147 
148 
149 #define DATATOOLS_SERIALIZATION_EXT_SERIAL_TAG_IMPLEMENTATION(ClassName,ClassSerialTag) \
150  namespace datatools { \
151  template <> \
152  const std::string & serial_tag<ClassName> (){ \
153  static std::unique_ptr<std::string> _serial_tag; \
154  if ( !_serial_tag){ \
155  _serial_tag.reset(new std::string(ClassSerialTag)); \
156  } \
157  return *_serial_tag.get(); \
158  } \
159  } \
160 
161 
162 
174 #define DATATOOLS_SERIALIZATION_BACKWARD_SERIAL_TAG_SUPPORT() \
175  public: \
176  struct backward_serial_tag_support {}; \
177 
178 
179 
180 #define DATATOOLS_SERIALIZATION_EXT_BACKWARD_SERIAL_TAG_DECLARATION(ClassName) \
181  namespace datatools { \
182  template <> \
183  const std::string & backward_serial_tag< ClassName > (int); \
184  } \
185 
186 
187 
188 #define DATATOOLS_SERIALIZATION_EXT_BACKWARD_SERIAL_TAG_IMPLEMENTATION(ClassName,ClassBackwardSerialTag) \
189  namespace datatools { \
190  template <> \
191  const std::string & backward_serial_tag<ClassName> (int /*i*/){ \
192  static std::unique_ptr<std::string> _backward_serial_tag; \
193  if ( !_backward_serial_tag){ \
194  _backward_serial_tag.reset (new std::string(ClassBackwardSerialTag)); \
195  } \
196  return *_backward_serial_tag.get (); \
197  } \
198  } \
199 
200 
201 
219 #define DATATOOLS_SERIALIZATION_I_SERIALIZABLE_BASE_OBJECT_NVP \
220  boost::serialization::make_nvp("datatool__i_serializable", \
221  boost::serialization::base_object<datatools::i_serializable>(*this)) \
222  \
223 
224 
225 
248 #define DATATOOLS_SERIALIZATION_OLD_I_SERIALIZABLE_BASE_OBJECT_NVP \
249  boost::serialization::make_nvp("datatools__serialization__i_serializable", \
250  boost::serialization::base_object<datatools::i_serializable>(*this)) \
251  \
252 
253 
254 
266 #define DATATOOLS_SERIALIZATION_DECLARATION() \
267  public: \
268  DATATOOLS_SERIALIZATION_SERIAL_TAG_DECLARATION() \
269  BOOST_SERIALIZATION_BASIC_DECLARATION () \
270 
271 
293 #define DATATOOLS_SERIALIZATION_SERIALIZE_IMPLEMENT_HEADER(ClassName,ArchiveId,VersionId) \
294  template<class Archive> \
295  void ClassName::serialize(Archive & ArchiveId, const unsigned int VersionId) \
296 
297 
298 
310 #define DATATOOLS_SERIALIZATION_FACTORY_INTERFACE(ClassName) \
311  DATATOOLS_FACTORY_SYSTEM_AUTO_REGISTRATION_INTERFACE(::datatools::i_serializable,ClassName) \
312 
313 
314 
328 #define DATATOOLS_SERIALIZATION_FACTORY_IMPLEMENTATION(ClassName,ClassSerialTag) \
329  DATATOOLS_FACTORY_SYSTEM_AUTO_REGISTRATION_IMPLEMENTATION(::datatools::i_serializable,ClassName,ClassSerialTag) \
330 
331 
332 
348 #define DATATOOLS_SERIALIZATION_IMPLEMENTATION(ClassName,ClassSerialTag) \
349  DATATOOLS_SERIALIZATION_SERIAL_TAG_IMPLEMENTATION(ClassName,ClassSerialTag) \
350 
351 
352 
371 #define DATATOOLS_SERIALIZATION_DECLARATION_ADVANCED(ClassName) \
372  DATATOOLS_SERIALIZATION_DECLARATION() \
373  DATATOOLS_SERIALIZATION_FACTORY_INTERFACE(ClassName) \
374 
375 
376 
398 #define DATATOOLS_SERIALIZATION_IMPLEMENTATION_ADVANCED(ClassName,ClassSerialTag) \
399  DATATOOLS_SERIALIZATION_IMPLEMENTATION (ClassName,ClassSerialTag) \
400  DATATOOLS_SERIALIZATION_FACTORY_IMPLEMENTATION (ClassName,ClassSerialTag) \
401 
402 
403 
404 // Utilities for serial tag checking :
405 namespace datatools {
406 
407  template <typename T>
408  bool check_serial_tag(const std::string stag_,
409  const std::string alt_tag_ = "",
410  typename boost::disable_if< has_bsts<T> >::type* dummy_ = 0)
411  {
412  if(!dummy_) dummy_ = 0;
413  if (stag_ == T::serial_tag()) return true;
414  if (! alt_tag_.empty()){
415  if (stag_ == alt_tag_) return true;
416  }
417  return false;
418  }
419 
420  template <typename T>
421  bool check_serial_tag(const std::string stag_,
422  const std::string alt_tag_ = "",
423  typename boost::enable_if< has_bsts<T> >::type* dummy_ = 0)
424  {
425  if(!dummy_) dummy_ = 0;
426  if (stag_ == T::serial_tag()) return true;
427  if (stag_ == ::datatools::backward_serial_tag<T> (0)) return true;
428  if (! alt_tag_.empty()) {
429  if (stag_ == alt_tag_) return true;
430  }
431  return false;
432  }
433 
434 }
435 
436 #endif // DATATOOLS_I_SERIALIZABLE_H
437 
438 // Local Variables: --
439 // mode: c++ --
440 // c-file-style: "gnu" --
441 // tab-width: 2 --
442 // End: --
#define DR_CLASS_INIT(Introspectable)
Inform Camp that class Introspectable exists and trigger the automatic registration of dedicated refl...
Definition: reflection_interface.h:149
Base abstract class of all serializable (and possibly introspectable) classes.
Definition: i_serializable.h:51
bool check_serial_tag(const std::string stag_, const std::string alt_tag_="", typename boost::disable_if< has_bsts< T > >::type *dummy_=0)
Definition: i_serializable.h:408
i_serializable()
Default constructor.
virtual ~i_serializable()
Destructor.
virtual const std::string & get_serial_tag() const =0
Return the serialization string identifier of the class.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
Describe the datatools API configuration.
#define DR_CLASS_RTTI()
Declare Camp RTTI within class declaration.
Definition: reflection_interface.h:46
#define DATATOOLS_FACTORY_SYSTEM_REGISTER_INTERFACE(BaseType)
Declaration of a system (allocator/functor) factory register as a static member of a base class and s...
Definition: factory_macros.h:52
#define BOOST_SERIALIZATION_BASIC_DECLARATION()
Definition: serialization_macros.h:62
const std::string & backward_serial_tag(int i=0)
const std::string & serial_tag()