Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:40

0001 
0002 #include <string>
0003 #include <iostream>
0004 #include <fstream>
0005 #include <utility>
0006 #include <stdexcept>
0007 
0008 #include "CondFormats/Serialization/interface/Serializable.h"
0009 #include "CondFormats/Serialization/interface/Archive.h"
0010 #include "CondFormats/Serialization/interface/Equal.h"
0011 #include "CondFormats/Serialization/interface/Test.h"
0012 
0013 #include "CondFormats/RunInfo/src/headers.h"
0014 
0015 // The compiler knows our default-constructed objects' members
0016 // may not be initialized when we serialize them.
0017 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
0018 
0019 template <typename T>
0020 void toXML(std::string filename = "") {
0021   if (filename == "") {
0022     filename = std::string(typeid(T).name()) + ".xml";
0023   }
0024 
0025   // C++ does not allow to construct const objects
0026   // of non-POD types without user-provided default constructor
0027   // (since it would be uninitialized), so we always create
0028   // a non-const object.
0029   T originalObject;
0030   const T& originalObjectRef = originalObject;
0031   {
0032     std::ofstream ofs(filename);
0033     //cond::serialization::OutputArchiveXML oa(ofs);
0034     boost::archive::xml_oarchive oa(ofs);
0035     std::cout << "Serializing " << typeid(T).name() << " ..." << std::endl;
0036     oa& boost::serialization::make_nvp("cmsCondPayload", originalObjectRef);
0037   }
0038 }
0039 
0040 template <typename T>
0041 T fromXML(std::string filename = "") {
0042   if (filename == "") {
0043     filename = std::string(typeid(T).name()) + ".xml";
0044   }
0045 
0046   // C++ does not allow to construct const objects
0047   // of non-POD types without user-provided default constructor
0048   // (since it would be uninitialized), so we always create
0049   // a non-const object.
0050   T originalObject;
0051   {
0052     std::cout << "going to read from " << filename << std::endl;
0053     std::ifstream ifs(filename);
0054     try {
0055       // cond::serialization::InputArchiveXML ia( ifs );
0056       boost::archive::xml_iarchive ia(ifs);
0057       std::cout << "Deserializing " << typeid(T).name() << " ..." << std::endl;
0058       ia& boost::serialization::make_nvp("cmsCondPayload", originalObject);
0059     } catch (const boost::archive::archive_exception& ae) {
0060       std::cout << "** boost::archive load exception"
0061                 << ": " << __FILE__ << ":" << __LINE__ << ":" << __func__ << ": " << ae.what() << "\n"
0062                 << std::endl;
0063     }
0064   }
0065   return originalObject;
0066 }
0067 
0068 class Simple {
0069 public:
0070   Simple() : my_i(42), my_f(42.) { /* nop */ }
0071 
0072   // COND_SERIALIZABLE;
0073   template <class Archive>
0074   void serialize(Archive& ar, const unsigned int version);
0075 
0076 private:
0077   int my_i;
0078   float my_f;
0079 };
0080 
0081 template <class Archive>
0082 void Simple::serialize(Archive& ar, const unsigned int version) {
0083   ar& BOOST_SERIALIZATION_NVP(my_i);
0084   ar& BOOST_SERIALIZATION_NVP(my_f);
0085 }
0086 
0087 int main(int, char**) {
0088   toXML<Simple>();
0089   fromXML<Simple>();
0090 
0091   std::cout << "Simple done" << std::endl;
0092 
0093   RunInfo ri = fromXML<RunInfo>("src/CondFormats/RunInfo/test/RunInfo-000fe8.xml");
0094   std::cout << "Found info for run " << ri.m_run << std::endl;
0095   std::cout << "        started at " << ri.m_start_time_str << std::endl;
0096 
0097   return 0;
0098 }