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  if( demangledName( typeid(T) )!= payloadTypeName )
32  throwException(std::string("Type mismatch, target object is type \"")+payloadTypeName+"\"",
33  "createPayload" );
34  return new T;
35  }
36 
37  // Archives for the streaming based on ROOT.
38 
39  // output
41  public:
42  explicit RootOutputArchive( std::ostream& destination );
43 
44  template <typename T>
46  private:
47  // type and ptr of the object to stream
48  void write( const std::type_info& sourceType, const void* sourceInstance);
49  private:
50  // here is where the write function will write on...
51  std::ostream& m_buffer;
52  };
53 
54  template <typename T> inline RootOutputArchive& RootOutputArchive::operator<<( const T& instance ){
55  write( typeid(T), &instance );
56  return *this;
57  }
58 
59  // input
61  public:
62  explicit RootInputArchive( std::istream& source );
63 
64  virtual ~RootInputArchive();
65 
66  template <typename T>
68  private:
69  // type and ptr of the object to restore
70  void read( const std::type_info& destinationType, void* destinationInstance);
71  private:
72  // copy of the input stream. is referenced by the TBufferFile.
74  TBufferFile* m_streamer = nullptr;
75  };
76 
77  template <typename T> inline RootInputArchive& RootInputArchive::operator>>( T& instance ){
78  read( typeid(T), &instance );
79  return *this;
80  }
81 
84 
85  // call for the serialization. Setting packingOnly = TRUE the data will stay in the original memory layout
86  // ( no serialization in this case ). This option is used by the ORA backend - will be dropped after the changeover
87  template <typename T> Binary serialize( const T& payload, bool packingOnly = false ){
88  Binary ret;
89  if( !packingOnly ){
90  // save data to buffer
91  std::ostringstream buffer;
92  CondOutputArchive oa( buffer );
93  oa << payload;
94  //TODO: avoid (2!!) copies
95  ret.copy( buffer.str() );
96  } else {
97  ret = Binary( payload );
98  }
99  return ret;
100  }
101 
102  // generates an instance of T from the binary serialized data. With unpackingOnly = true the memory is already storing the object in the final
103  // format. Only a cast is required in this case - Used by the ORA backed, will be dropped in the future.
104  template <typename T> boost::shared_ptr<T> deserialize( const std::string& payloadType, const Binary& payloadData, bool unpackingOnly = false){
105  // for the moment we fail if types don't match... later we will check for base types...
106  boost::shared_ptr<T> payload;
107  if( !unpackingOnly ){
108  std::stringbuf sbuf;
109  sbuf.pubsetbuf( static_cast<char*>(const_cast<void*>(payloadData.data())), payloadData.size() );
110 
111  std::istream buffer( &sbuf );
112  CondInputArchive ia(buffer);
113  payload.reset( createPayload<T>(payloadType) );
114  ia >> (*payload);
115  } else {
116  payload = boost::static_pointer_cast<T>(payloadData.share());
117  }
118  return payload;
119  }
120 
121 }
122 #endif
std::ostream & m_buffer
Definition: Serialization.h:51
static PFTauRenderPlugin instance
void read(const std::type_info &destinationType, void *destinationInstance)
TBufferFile * m_streamer
Definition: Serialization.h:74
size_t size() const
Definition: Binary.cc:61
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:46
RootInputArchive(std::istream &source)
RootOutputArchive(std::ostream &destination)
const void * data() const
Definition: Binary.cc:52
RootOutputArchive & operator<<(const T &instance)
Definition: Serialization.h:54
void throwException(std::string const &message, std::string const &methodName)
Definition: Exception.cc:17
RootInputArchive & operator>>(T &instance)
Definition: Serialization.h:77
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:87
RootInputArchive CondInputArchive
Definition: Serialization.h:82
RootOutputArchive CondOutputArchive
Definition: Serialization.h:83
long double T
static std::string const source
Definition: EdmProvDump.cc:43
boost::shared_ptr< void > share() const
Definition: Binary.cc:66