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 #include "Util/log.H"
00040
00041 #ifndef HAVE_BOOST_SERIALIZATION
00042
00043 int main(int argc, const char **argv)
00044 {
00045 LFATAL("You need Boost::Serialization installed on your system for this program to work");
00046 return 1;
00047 }
00048
00049 #else
00050
00051
00052 #include <boost/archive/text_oarchive.hpp>
00053 #include <boost/archive/text_iarchive.hpp>
00054 #include <boost/serialization/access.hpp>
00055 #include <boost/serialization/config.hpp>
00056 #include <boost/serialization/vector.hpp>
00057
00058 #include <fstream>
00059
00060 typedef unsigned char byte;
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 class SimModule {
00073 };
00074
00075
00076
00077
00078 class SimEvent {
00079 public:
00080 SimEvent(SimModule* src = 0) { }
00081
00082 virtual ~SimEvent() { }
00083
00084 inline std::string toString() const { return std::string("define me as in old SimEvent def"); }
00085
00086 private:
00087 friend class boost::serialization::access;
00088 template<class Archive> inline void serialize(Archive& ar, const unsigned int version) { }
00089 };
00090
00091 BOOST_CLASS_VERSION(SimEvent, 0);
00092
00093
00094
00095 class MySimEvent : public SimEvent {
00096 public:
00097 MySimEvent(SimModule* src = 0) { }
00098
00099 MySimEvent(SimModule* src__, const int idata__, const float fdata__) :
00100 SimEvent(src__), idata_(idata__), fdata_(fdata__) { }
00101
00102 virtual ~MySimEvent() { }
00103
00104 int& idata() { return idata_; }
00105 float& fdata() { return fdata_; }
00106
00107 const int& idata() const { return idata_; }
00108 const float& fdata() const { return fdata_; }
00109
00110 private:
00111 friend class boost::serialization::access;
00112
00113 template<class Archive> void serialize(Archive& ar, const unsigned int version)
00114 {
00115
00116 ar & boost::serialization::base_object<SimEvent>(*this);
00117
00118
00119 ar & idata_;
00120 ar & fdata_;
00121 }
00122
00123 int idata_;
00124 float fdata_;
00125 };
00126
00127 BOOST_CLASS_VERSION(MySimEvent, 0);
00128
00129
00130
00131
00132 class DerSimEvent : public MySimEvent {
00133 public:
00134 DerSimEvent(SimModule* src = 0) { }
00135
00136 DerSimEvent(SimModule* src__, const int idata__, const float fdata__,
00137 const std::string& sdata__, const std::vector<byte>& vdata__) :
00138 MySimEvent(src__, idata__, fdata__), sdata_(sdata__), vdata_(vdata__) { }
00139
00140 virtual ~DerSimEvent() { }
00141
00142 std::string& sdata() { return sdata_; }
00143 std::vector<byte>& vdata() { return vdata_; }
00144
00145 const std::string& sdata() const { return sdata_; }
00146 const std::vector<byte>& vdata() const { return vdata_; }
00147
00148 private:
00149 friend class boost::serialization::access;
00150
00151 template<class Archive> void serialize(Archive& ar, const unsigned int version)
00152 {
00153
00154 ar & boost::serialization::base_object<MySimEvent>(*this);
00155
00156
00157 ar & sdata_;
00158 ar & vdata_;
00159 }
00160
00161 std::string sdata_;
00162 std::vector<byte> vdata_;
00163 };
00164
00165 BOOST_CLASS_VERSION(DerSimEvent, 0);
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 #include <boost/preprocessor/seq.hpp>
00178 #include <boost/preprocessor/arithmetic/add.hpp>
00179 #include <boost/preprocessor/comparison/equal.hpp>
00180 #include <boost/preprocessor/punctuation/comma.hpp>
00181 #include <boost/preprocessor/control/if.hpp>
00182 #include <boost/preprocessor/punctuation/paren.hpp>
00183 #include <boost/preprocessor/facilities/empty.hpp>
00184
00185
00186
00187
00188 #define INVT_PP_SIMEVENT_ROOT_CLASS SimEvent
00189
00190
00191 #define INVT_PP_SIMEVENT_VAR(V) BOOST_PP_CAT(V, _)
00192
00193
00194 #define INVT_PP_SIMEVENT_ELEM_TYP(v) BOOST_PP_TUPLE_ELEM(2, 0, v)
00195 #define INVT_PP_SIMEVENT_ELEM_VAR(v) BOOST_PP_TUPLE_ELEM(2, 1, v)
00196
00197
00198 #define INVT_PP_SIMEVENT_DECLARE(r,x,val) INVT_PP_SIMEVENT_ELEM_TYP(val) \
00199 INVT_PP_SIMEVENT_VAR(INVT_PP_SIMEVENT_ELEM_VAR(val)); \
00200
00201
00202
00203 #define INVT_PP_SIMEVENT_ARCHIVE(r,x,val) ar & INVT_PP_SIMEVENT_VAR(INVT_PP_SIMEVENT_ELEM_VAR(val));
00204
00205
00206 #define INVT_PP_SIMEVENT_ACCESS(r,x,val) \
00207 inline INVT_PP_SIMEVENT_ELEM_TYP(val) & INVT_PP_SIMEVENT_ELEM_VAR(val)() \
00208 { return INVT_PP_SIMEVENT_VAR(INVT_PP_SIMEVENT_ELEM_VAR(val)); } \
00209
00210
00211
00212 #define INVT_PP_SIMEVENT_ACCESS_CONST(r,x,val) \
00213 inline const INVT_PP_SIMEVENT_ELEM_TYP(val)& INVT_PP_SIMEVENT_ELEM_VAR(val)() const \
00214 { return INVT_PP_SIMEVENT_VAR(INVT_PP_SIMEVENT_ELEM_VAR(val)); } \
00215
00216
00217
00218 #define INVT_PP_SIMEVENT_EMPTY_IMPL { }
00219
00220
00221 #define INVT_PP_SIMEVENT_DEFAULT_CONSTRUCTOR(CLASSNAME, BASECLASSNAME, CONSTR) \
00222 BOOST_PP_IF(BOOST_PP_EQUAL(CONSTR, 0), BOOST_PP_EMPTY(), \
00223 CLASSNAME(SimModule* invt_pp_simevent_source = 0) \
00224 BOOST_PP_IF(BOOST_PP_EQUAL(CONSTR, 1), ;, \
00225 : BASECLASSNAME(invt_pp_simevent_source) INVT_PP_SIMEVENT_EMPTY_IMPL) ) \
00226
00227
00228
00229 #define INVT_PP_SIMEVENT_DESTRUCTOR(CLASSNAME, DESTR) \
00230 BOOST_PP_IF(BOOST_PP_EQUAL(DESTR, 0), BOOST_PP_EMPTY(), \
00231 BOOST_PP_IF(BOOST_PP_EQUAL(DESTR, 1), virtual ~CLASSNAME();, \
00232 virtual inline ~CLASSNAME() INVT_PP_SIMEVENT_EMPTY_IMPL) ) \
00233
00234
00235
00236 #define INVT_PP_SIMEVENT_SERIALIZATION_FRIEND friend class boost::serialization::access;
00237
00238
00239 #define INVT_PP_SIMEVENT_SERIALIZE_FUNC(BASECLASSNAME, V) \
00240 template<class Archive> inline void serialize(Archive& ar, const unsigned int version) \
00241 { \
00242 ar & boost::serialization::base_object<BASECLASSNAME>(*this); \
00243 BOOST_PP_SEQ_FOR_EACH(INVT_PP_SIMEVENT_ARCHIVE,,V) \
00244 } \
00245
00246
00247
00248 #define INVT_PP_SIMEVENT_TOSTRING_DECL(BASECLASSNAME, TOSTR) \
00249 BOOST_PP_IF(BOOST_PP_EQUAL(TOSTR, 0), BOOST_PP_EMPTY(), \
00250 virtual std::string toString() const \
00251 BOOST_PP_IF(BOOST_PP_EQUAL(TOSTR, 1), ;, { return BASECLASSNAME::toString(); } ) )
00252
00253
00254
00255 #define INVT_PP_SIMEVENT_CLASSDECL(CLASSNAME, BASECLASSNAME, CLASSDECL, SERVERSION) \
00256 BOOST_PP_IF(BOOST_PP_EQUAL(CLASSDECL, 0), BOOST_PP_EMPTY(), \
00257 class CLASSNAME; \
00258 BOOST_CLASS_VERSION(CLASSNAME, SERVERSION); \
00259 class CLASSNAME : public BASECLASSNAME { ) \
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 #define SIMEVENT_CLASS_DECLARATION_INTERNAL(CLASSNAME, BASECLASSNAME, V, CLASSDECL, CONSTR, DESTR, TOSTR, SERVERSION) \
00273 INVT_PP_SIMEVENT_CLASSDECL(CLASSNAME, BASECLASSNAME, CLASSDECL, SERVERSION) \
00274 \
00275 public: \
00276 INVT_PP_SIMEVENT_DEFAULT_CONSTRUCTOR(CLASSNAME, BASECLASSNAME, CONSTR) \
00277 INVT_PP_SIMEVENT_DESTRUCTOR(CLASSNAME, DESTR) \
00278 BOOST_PP_SEQ_FOR_EACH(INVT_PP_SIMEVENT_ACCESS,, V) \
00279 BOOST_PP_SEQ_FOR_EACH(INVT_PP_SIMEVENT_ACCESS_CONST,, V) \
00280 INVT_PP_SIMEVENT_TOSTRING_DECL(BASECLASSNAME, TOSTR) \
00281 \
00282 private: \
00283 BOOST_PP_SEQ_FOR_EACH(INVT_PP_SIMEVENT_DECLARE,, V) \
00284 INVT_PP_SIMEVENT_SERIALIZATION_FRIEND \
00285 INVT_PP_SIMEVENT_SERIALIZE_FUNC(BASECLASSNAME, V) \
00286
00287
00288
00289 #define SIMEVENT_CLASS_DECLARATION_TRIVIAL(CLASSNAME) \
00290 SIMEVENT_CLASS_DECLARATION_INTERNAL(CLASSNAME, INVT_PP_SIMEVENT_ROOT_CLASS, , 1, 2, 2, 2, 0)
00291
00292 #define SIMEVENT_CLASS_DECLARATION_SIMPLE(CLASSNAME, V) \
00293 SIMEVENT_CLASS_DECLARATION_INTERNAL(CLASSNAME, INVT_PP_SIMEVENT_ROOT_CLASS, V, 1, 2, 2, 1, 0)
00294
00295 #define SIMEVENT_CLASS_DECLARATION_BARE(CLASSNAME, BASECLASSNAME, V) \
00296 SIMEVENT_CLASS_DECLARATION_INTERNAL(CLASSNAME, BASECLASSNAME, V, 1, 0, 0, 0, 0)
00297
00298 #define SIMEVENT_CLASS_DECLARATION(CLASSNAME, BASECLASSNAME, V, CONSTR, DESTR, TOSTR, SERVERSION) \
00299 SIMEVENT_CLASS_DECLARATION_INTERNAL(CLASSNAME, BASECLASSNAME, V, 1, CONSTR, DESTR, TOSTR, SERVERSION) \
00300
00301
00302
00303 #define SIMEVENT_CLASS_DEFINITION_TRIVIAL(CLASSNAME) \
00304 SIMEVENT_CLASS_DECLARATION_INTERNAL(CLASSNAME, INVT_PP_SIMEVENT_ROOT_CLASS, , 0, 2, 2, 2, 0)
00305
00306 #define SIMEVENT_CLASS_DEFINITION_SIMPLE(CLASSNAME, V) \
00307 SIMEVENT_CLASS_DECLARATION_INTERNAL(CLASSNAME, INVT_PP_SIMEVENT_ROOT_CLASS, V, 0, 2, 2, 1, 0)
00308
00309 #define SIMEVENT_CLASS_DEFINITION_BARE(CLASSNAME, BASECLASSNAME, V) \
00310 SIMEVENT_CLASS_DECLARATION_INTERNAL(CLASSNAME, BASECLASSNAME, V, 0, 0, 0, 0, 0)
00311
00312 #define SIMEVENT_CLASS_DEFINITION(CLASSNAME, BASECLASSNAME, V, CONSTR, DESTR, TOSTR, SERVERSION) \
00313 SIMEVENT_CLASS_DECLARATION_INTERNAL(CLASSNAME, BASECLASSNAME, V, 0, CONSTR, DESTR, TOSTR, SERVERSION) \
00314
00315
00316
00317
00318
00319
00320
00321
00322 SIMEVENT_CLASS_DECLARATION_TRIVIAL(TrivSimEvent)
00323
00324
00325
00326
00327
00328 };
00329
00330
00331 SIMEVENT_CLASS_DECLARATION_SIMPLE( MySimEvent2,
00332 (( int, idata ))
00333 (( float, fdata )) )
00334 public:
00335
00336 MySimEvent2(SimModule* src__, const int idata__, const float fdata__) :
00337 SimEvent(src__), idata_(idata__), fdata_(fdata__) { }
00338
00339
00340
00341
00342
00343
00344
00345
00346 };
00347
00348
00349 std::string MySimEvent2::toString() const
00350 { return SimEvent::toString() + "MySimEvent2 Message"; }
00351
00352
00353 class DerSimEvent2 : public MySimEvent2 {
00354
00355
00356 SIMEVENT_CLASS_DEFINITION_BARE( DerSimEvent2, MySimEvent2,
00357 (( std::string, sdata ))
00358 (( std::vector<byte>, vdata )) )
00359
00360 public:
00361
00362 DerSimEvent2(SimModule* src__, const int idata__, const float fdata__,
00363 const std::string& sdata__, const std::vector<byte>& vdata__) :
00364 MySimEvent2(src__, idata__, fdata__), sdata_(sdata__), vdata_(vdata__) { }
00365
00366
00367
00368
00369
00370
00371
00372
00373 DerSimEvent2(SimModule* src = 0) : MySimEvent2(src) { }
00374
00375
00376 virtual ~DerSimEvent2() { }
00377
00378
00379 std::string toString() const;
00380 };
00381
00382
00383 std::string DerSimEvent2::toString() const
00384 { return MySimEvent2::toString() + "DerSimEvent2 Message"; }
00385
00386
00387
00388
00389
00390
00391
00392
00393 #if 0
00394 # define MYSIMEVENT MySimEvent
00395 # define DERSIMEVENT DerSimEvent
00396 #else
00397 # define MYSIMEVENT MySimEvent2
00398 # define DERSIMEVENT DerSimEvent2
00399 #endif
00400
00401 int main(int argc, const char **argv)
00402 {
00403
00404 std::ofstream ofs("test-serialization.object");
00405
00406
00407 SimModule *sm = 0;
00408 std::vector<byte> v; v.push_back(byte(3)); v.push_back(byte(42));
00409 std::string str("serialization inferno");
00410 const DERSIMEVENT s(sm, 5, 10.0F, str, v);
00411
00412 LINFO("original data: %d %f '%s' [%d %d]", s.idata(), s.fdata(), s.sdata().c_str(), s.vdata()[0], s.vdata()[1]);
00413
00414
00415 {
00416 boost::archive::text_oarchive oa(ofs);
00417
00418 oa << s;
00419
00420 }
00421
00422
00423 DERSIMEVENT ss(sm);
00424
00425 LINFO("new object before load: %d %f '%s' []", ss.idata(), ss.fdata(), ss.sdata().c_str());
00426
00427 {
00428
00429 std::ifstream ifs("test-serialization.object");
00430 boost::archive::text_iarchive ia(ifs);
00431
00432 ia >> ss;
00433
00434 }
00435
00436 LINFO("re-loaded data: %d %f '%s' [%d %d]", s.idata(), s.fdata(), s.sdata().c_str(), s.vdata()[0], s.vdata()[1]);
00437
00438 return 0;
00439 }
00440
00441
00442 #endif
00443
00444
00445
00446
00447
00448
00449