uLib-0.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ObjectProps.h
Go to the documentation of this file.
1 /*//////////////////////////////////////////////////////////////////////////////
2 // CMT Cosmic Muon Tomography project //////////////////////////////////////////
4 
5  Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
6  All rights reserved
7 
8  Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
9 
10  ------------------------------------------------------------------
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 3.0 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library.
23 
25 
26 
27 
28 #ifndef U_OBJECTPROPS_H
29 #define U_OBJECTPROPS_H
30 
31 
32 
33 #include <boost/algorithm/string/replace.hpp>
34 
35 #include <Core/Mpl.h>
36 #include <Core/Types.h>
37 
38 
39 
43 // MACROS //
44 
45 
46 #define ULIB_props() \
47  public: struct ObjectProps; \
48  virtual void init_properties(); \
49  inline struct ObjectProps &p() { /* static const unsigned int offset = props_offset(this); */ \
50  /* NON FUNZIA! return * (struct ObjectProps *)(reinterpret_cast<char*>(props())+offset); */ \
51  return *props()->ptr<ObjectProps>(); } \
52  typedef uLib::mpl::bool_<true>::type propable_trait; \
53  public: struct DLL_PUBLIC ObjectProps
54 
56 
57 #define default(vlaue)
58 
59 #define $$ p()
60 
61 #define $(_name) props_ref<_name>()
62 
63 #define $_init() \
64  if(props(this)) return; \
65  props_new(this); \
67 
68 
69 
73 
74 
75 namespace boost {
76 namespace serialization {
77 class access;
78 }
79 }
80 
81 
82 namespace uLib {
83 
84 namespace detail {
85 
86 struct ObjectProps {
87 
89  BOOST_MPL_HAS_XXX_TRAIT_DEF(propable_trait)
90 
91 
92  template <class T>
93  struct IsA : has_propable_trait<T> {};
94 
96  template <class T>
97  struct props_type {
98  typedef typename T::ObjectProps type;
99  };
100 
101  template <typename T>
102  struct get_props {
103 
106 
108  typedef typename mpl::filter_view< CFTypeList, IsA<mpl::_> >::type FilteredCFTypeList;
109 
111  typedef typename mpl::transform_view< FilteredCFTypeList, props_type<mpl::_> >::type type;
112 
113 
114  // qui sotto ho un problema che ho temporaneamente tamponato //
115  // ovvero ho usato child_first_impl per ottenere la lista delle basi //
116  // vorrei farlo facendo un pop_back ma non va forse perche il tipo //
117  // non e' corretto. //
118 
121 
123  typedef typename mpl::filter_view< CFBaseList, IsA<mpl::_> >::type FilteredCFBaseList;
124  };
125 
126 
127 
128  // TODO: convert to pp const value,
129  // (non so se sia possibile con il dinamic casting intanto funziona cosi' )
130  template <typename T1, typename T2>
131  static unsigned int measure_offset(T1 base, T2 derived) {
132  return reinterpret_cast<char*>(derived) - reinterpret_cast<char*>(base);
133  }
134 
135  struct initializer {
136 
137  template <class _ObjectT>
139  _ObjectT *o;
140  lambda_init_object(_ObjectT *o) : o(o) {}
141  template<class T> void operator()(T) {
142  o->T::init_properties();
143  }
144  };
145 
146 
151  template <class T>
152  static void init_object_baselist(T *o) {
154  mpl::for_each<CFBaseList>(lambda_init_object<T>(o));
155  }
156 
161  template <class T>
162  static
163  typename boost::enable_if<mpl::not_<IsA<T> >,void>::type
164  init_object(T *o) {
165  ; // do nothing //
166  }
167 
172  template <class T>
173  static
174  typename boost::enable_if<IsA<T>,void>::type
175  init_object(T *o) {
176  o->init_properties();
177  }
178 
179  };
180 
181  template <class ThisClass, class Archive>
183  ThisClass & m_object;
184  Archive & m_ar;
185  serialize_baselist(ThisClass &o, Archive &ar) : m_object(o), m_ar(ar) {}
186  template <class T> void operator()(T &o) {
187  // T is taken fron get_props<BaseList>::FilteredPList types to get
188  // type_info_name that is the type name defined by Type macro
189  typedef typename props_type<T>::type PType;
191  boost::algorithm::replace_all(name,"::","_");
192  m_ar & boost::serialization::make_nvp(
193  name.c_str() ,
194  boost::serialization::base_object<PType>(m_object));
195  }
196  };
197 
198 };
199 
200 } // detail
201 
202 
203 
204 
205 
207  virtual ~ObjectPropsBase() {}
208  virtual ObjectPropsBase *copy() = 0;
209 
210  template <class T> inline T *ptr() { return dynamic_cast<T*>(this); }
211 
212  template<class ArchiveT> void serialize(ArchiveT &ar, const unsigned int version) {}
213 };
214 
215 
216 
217 template <class T>
220  ULIB_MPL_INHERIT_NOFOLD_SEQ(typename uLib::detail::ObjectProps::get_props<T>::type)
221 {
225  ObjectPropsBase *copy() { return new ThisClass(*this); }
226 
227  template<class ArchiveT> void serialize(ArchiveT &ar, const unsigned int version) {
228  boost::serialization::void_cast_register<ThisClass,ObjectPropsBase>();
229  mpl::for_each<FilteredCFTypeList>(detail::ObjectProps::serialize_baselist<ThisClass,ArchiveT>(*this,ar));
230  }
231 };
232 
233 
235 
236  ObjectPropsBase *m_props;
239 public:
240  ObjectPropable() : m_props(NULL) {}
241  ObjectPropable(const ObjectPropable &c) { if(c.m_props) m_props = c.m_props->copy(); else m_props = NULL; }
242  ~ObjectPropable() { if(m_props) delete m_props; }
243 
244 
245  template <class T> inline typename T::ObjectProps& props_ref() const { if(m_props) return *m_props->ptr<typename T::ObjectProps>(); else exit(1); }
246  template <class T> inline typename T::ObjectProps* props(T *ptr = NULL) const { if(m_props) return m_props->ptr<typename T::ObjectProps>(); else return NULL; }
247 protected:
248  ObjectPropsBase *props() const { return m_props; }
249  template <class T> inline void props_new(T* ptr = NULL) { if(!m_props) m_props = new ObjectPropsImpl<T>; }
250 
252  template <class T> inline unsigned int props_offset(T *ptr) const
253  { if(m_props) return detail::ObjectProps::measure_offset(m_props,m_props->ptr<T>()); else return -1; }
254 
255 
256  template<class ArchiveT> void serialize(ArchiveT &ar, const unsigned int version) {
257  if(m_props) ar & boost::serialization::make_nvp("properties",m_props);
258  }
259 
260 public:
266  virtual void init_properties() {}
267 
268 };
269 
270 } // uLib
271 
272 
273 
274 
275 
276 
277 
278 #endif // U_OBJECTPROPS_H
~ObjectPropable()
Definition: ObjectProps.h:242
T * ptr()
Definition: ObjectProps.h:210
lambda_init_object(_ObjectT *o)
Definition: ObjectProps.h:140
#define properties()
Definition: ObjectProps.h:55
unsigned int props_offset(T *ptr) const
Definition: ObjectProps.h:252
T::ObjectProps * props(T *ptr=NULL) const
Definition: ObjectProps.h:246
static unsigned int measure_offset(T1 base, T2 derived)
Definition: ObjectProps.h:131
void props_new(T *ptr=NULL)
Definition: ObjectProps.h:249
#define default(vlaue)
Definition: ObjectProps.h:57
Archive & m_ar
Definition: ObjectProps.h:184
ObjectPropable()
Definition: ObjectProps.h:240
uLib::detail::ObjectProps::get_props< T >::FilteredCFTypeList FilteredCFTypeList
Definition: ObjectProps.h:224
T::ObjectProps type
Definition: ObjectProps.h:98
ThisClass & m_object
Definition: ObjectProps.h:183
virtual ~ObjectPropsBase()
Definition: ObjectProps.h:207
void serialize(ArchiveT &ar, const unsigned int version)
Definition: ObjectProps.h:212
virtual void init_properties()
Definition: ObjectProps.h:266
#define ULIB_props()
Definition: ObjectProps.h:46
mpl::transform_view< FilteredCFTypeList, props_type< mpl::_ > >::type type
Definition: ObjectProps.h:111
ObjectPropsBase * copy()
Definition: ObjectProps.h:225
detail::TypeIntrospection::child_first< T >::type CFTypeList
Definition: ObjectProps.h:105
uLib::detail::ObjectProps::get_props< T >::type CFList
Definition: ObjectProps.h:223
Definition: ObjectProps.h:234
detail::TypeIntrospection::child_first_impl< T >::Childs CFBaseList
Definition: ObjectProps.h:120
mpl::filter_view< CFBaseList, IsA< mpl::_ > >::type FilteredCFBaseList
Definition: ObjectProps.h:123
void operator()(T &o)
Definition: ObjectProps.h:186
mpl::filter_view< CFTypeList, IsA< mpl::_ > >::type FilteredCFTypeList
Definition: ObjectProps.h:108
void operator()(T)
Definition: ObjectProps.h:141
Definition: ObjectProps.h:97
ObjectPropable(const ObjectPropable &c)
Definition: ObjectProps.h:241
void serialize(ArchiveT &ar, const unsigned int version)
Definition: ObjectProps.h:227
Definition: ObjectProps.h:93
mpl::fold< CFListSeq, mpl::vector<>, mpl::copy< mpl::_1, mpl::back_inserter< mpl::_2 > > >::type Childs
Definition: Types.h:110
serialize_baselist(ThisClass &o, Archive &ar)
Definition: ObjectProps.h:185
static void init_object_baselist(T *o)
Definition: ObjectProps.h:152
ObjectPropsBase * props() const
Definition: ObjectProps.h:248
Definition: ObjectProps.h:206
Definition: ObjectProps.h:102
#define ULIB_MPL_INHERIT_NOFOLD_SEQ(_TypeList)
Definition: Mpl.h:116
T::ObjectProps & props_ref() const
Definition: ObjectProps.h:245
virtual ObjectPropsBase * copy()=0
ObjectPropsImpl< T > ThisClass
Definition: ObjectProps.h:222
#define _name
Definition: ObjectProps.h:61
_ObjectT * o
Definition: ObjectProps.h:139
static boost::enable_if< mpl::not_< IsA< T > >, void >::type init_object(T *o)
Definition: ObjectProps.h:164
static boost::enable_if< IsA< T >, void >::type init_object(T *o)
Definition: ObjectProps.h:175
friend class boost::serialization::access
Definition: ObjectProps.h:238
Definition: ObjectProps.h:135
#define p()
Definition: ObjectProps.h:59
void serialize(ArchiveT &ar, const unsigned int version)
Definition: ObjectProps.h:256
Definition: ObjectProps.h:218
#define DLL_PUBLIC
Definition: Macros.h:62
Definition: ObjectProps.h:86