CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/IOPool/Common/interface/CustomStreamer.h

Go to the documentation of this file.
00001 #ifndef IOPool_Common_CustomStreamer_h
00002 #define IOPool_Common_CustomStreamer_h
00003 
00004 #include <string>
00005 #include "TROOT.h"
00006 #include "TClassStreamer.h"
00007 #include "TClassRef.h"
00008 #include "FWCore/Utilities/interface/TypeID.h"
00009 class TBuffer;
00010 
00011 namespace edm {
00012   template <typename T>
00013   class CustomStreamer : public TClassStreamer {
00014   public:
00015     typedef T element_type;
00016     CustomStreamer();
00017     void operator() (TBuffer &R__b, void *objp);
00018   private:
00019     std::string className_;
00020     TClassRef cl_;
00021   };
00022 
00023   template <typename T>
00024   CustomStreamer<T>::CustomStreamer() :
00025     className_(TypeID(typeid(T)).className()),
00026     cl_(className_.c_str())
00027   {}
00028 
00029   template <typename T>
00030   void
00031   CustomStreamer<T>::operator()(TBuffer &R__b, void *objp) {
00032     if (R__b.IsReading()) {
00033       cl_->ReadBuffer(R__b, objp);
00034     } else {
00035       cl_->WriteBuffer(R__b, objp);
00036     }
00037   }
00038 
00039   template <typename T>
00040   void
00041   SetCustomStreamer() {
00042     TClass *cl = gROOT->GetClass(TypeID(typeid(T)).className().c_str());
00043     if (cl->GetStreamer() == 0) {
00044       cl->AdoptStreamer(new CustomStreamer<T>());
00045     }
00046   }
00047 
00048   template <typename T>
00049   void
00050   SetCustomStreamer(T const&) {
00051     TClass *cl = gROOT->GetClass(TypeID(typeid(T)).className().c_str());
00052     if (cl->GetStreamer() == 0) {
00053       cl->AdoptStreamer(new CustomStreamer<T>());
00054     }
00055   }
00056 }
00057 
00058 #endif