CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Serialization.h
Go to the documentation of this file.
1 #ifndef CondCore_CondDB_Serialization_h
2 #define CondCore_CondDB_Serialization_h
3 //
4 // Package: CondDB
5 //
9 //
10 // Author: Giacomo Govi
11 // Created: October 2013
12 //
13 //
14 
18 //
19 #include <sstream>
20 #include <iostream>
21 //
22 // temporarely
23 #include <boost/shared_ptr.hpp>
24 
25 class TBufferFile;
26 
27 namespace cond {
28 
29  // default payload factory
30  template <typename T> T* createPayload( const std::string& payloadTypeName ){
31  std::string userTypeName = demangledName( typeid(T) );
32  if( userTypeName != payloadTypeName )
33  throwException(std::string("Type mismatch, user type: \""+userTypeName+"\", target type: \"")+payloadTypeName+"\"",
34  "createPayload" );
35  return new T;
36  }
37 
38  // Archives for the streaming based on ROOT.
39 
40  // output
42  public:
43  explicit RootOutputArchive( std::ostream& destination );
44 
45  template <typename T>
47  private:
48  // type and ptr of the object to stream
49  void write( const std::type_info& sourceType, const void* sourceInstance);
50  private:
51  // here is where the write function will write on...
52  std::ostream& m_buffer;
53  };
54 
55  template <typename T> inline RootOutputArchive& RootOutputArchive::operator<<( const T& instance ){
56  write( typeid(T), &instance );
57  return *this;
58  }
59 
60  // input
62  public:
63  explicit RootInputArchive( std::istream& source );
64 
65  virtual ~RootInputArchive();
66 
67  template <typename T>
69  private:
70  // type and ptr of the object to restore
71  void read( const std::type_info& destinationType, void* destinationInstance);
72  private:
73  // copy of the input stream. is referenced by the TBufferFile.
75  TBufferFile* m_streamer = nullptr;
76  };
77 
78  template <typename T> inline RootInputArchive& RootInputArchive::operator>>( T& instance ){
79  read( typeid(T), &instance );
80  return *this;
81  }
82 
85 
86  // call for the serialization. Setting packingOnly = TRUE the data will stay in the original memory layout
87  // ( no serialization in this case ). This option is used by the ORA backend - will be dropped after the changeover
88  template <typename T> Binary serialize( const T& payload, bool packingOnly = false ){
89  Binary ret;
90  if( !packingOnly ){
91  // save data to buffer
92  std::ostringstream buffer;
93  CondOutputArchive oa( buffer );
94  oa << payload;
95  //TODO: avoid (2!!) copies
96  ret.copy( buffer.str() );
97  } else {
98  ret = Binary( payload );
99  }
100  return ret;
101  }
102 
103  // generates an instance of T from the binary serialized data. With unpackingOnly = true the memory is already storing the object in the final
104  // format. Only a cast is required in this case - Used by the ORA backed, will be dropped in the future.
105  template <typename T> boost::shared_ptr<T> deserialize( const std::string& payloadType, const Binary& payloadData, bool unpackingOnly = false){
106  // for the moment we fail if types don't match... later we will check for base types...
107  boost::shared_ptr<T> payload;
108  if( !unpackingOnly ){
109  std::stringbuf sbuf;
110  sbuf.pubsetbuf( static_cast<char*>(const_cast<void*>(payloadData.data())), payloadData.size() );
111 
112  std::istream buffer( &sbuf );
113  CondInputArchive ia(buffer);
114  payload.reset( createPayload<T>(payloadType) );
115  ia >> (*payload);
116  } else {
117  payload = boost::static_pointer_cast<T>(payloadData.oraObject().makeShared());
118  }
119  return payload;
120  }
121 
122 }
123 #endif
std::ostream & m_buffer
Definition: Serialization.h:52
boost::shared_ptr< void > makeShared() const
Definition: Object.cc:73
static PFTauRenderPlugin instance
void read(const std::type_info &destinationType, void *destinationInstance)
TBufferFile * m_streamer
Definition: Serialization.h:75
size_t size() const
Definition: Binary.cc:57
std::string demangledName(const std::type_info &typeInfo)
Definition: ClassUtils.cc:82
void write(const std::type_info &sourceType, const void *sourceInstance)
T * createPayload(const std::string &payloadTypeName)
Definition: Serialization.h:30
void copy(const std::string &source)
Definition: Binary.cc:42
RootInputArchive(std::istream &source)
ora::Object oraObject() const
Definition: Binary.cc:62
RootOutputArchive(std::ostream &destination)
const void * data() const
Definition: Binary.cc:48
RootOutputArchive & operator<<(const T &instance)
Definition: Serialization.h:55
void throwException(std::string const &message, std::string const &methodName)
Definition: Exception.cc:17
RootInputArchive & operator>>(T &instance)
Definition: Serialization.h:78
boost::shared_ptr< T > deserialize(const std::string &payloadType, const Binary &payloadData, bool unpackingOnly=false)
Binary serialize(const T &payload, bool packingOnly=false)
Definition: Serialization.h:88
RootInputArchive CondInputArchive
Definition: Serialization.h:83
RootOutputArchive CondOutputArchive
Definition: Serialization.h:84
long double T
static std::string const source
Definition: EdmProvDump.cc:43