emcOMAsciiT.h

Go to the documentation of this file.
00001 #ifndef __EMCOMASCIIT_H__
00002 #define __EMCOMASCIIT_H__
00003 
00004 #ifndef __EMCOBJECTMANAGER_H__
00005 #include "emcObjectManager.h"
00006 #endif
00007 #ifndef __EMCMANAGEABLE_H__
00008 #include "emcManageable.h"
00009 #endif
00010 
00020 template<class T>
00021 class emcOMAsciiT : public emcObjectManager
00022 {
00023 public:
00024 
00025   typedef bool (*ReaderFcn)(T& object, int code);
00026   typedef bool (*WriterFcn)(const T& object, int code);
00027 
00028   emcOMAsciiT(const char* name, const char* title, 
00029               ReaderFcn reader,
00030               WriterFcn writer=0);
00031 
00032   virtual ~emcOMAsciiT();
00033 
00034   virtual bool CanRead(const emcManageable& object) const;
00035 
00036   virtual bool CanWrite(const emcManageable& object) const;
00037 
00038   using emcObjectManager::Read;
00039 
00041   virtual bool Read(emcManageable& object,
00042                     const PHTimeStamp& time_stamp,
00043                     int id);
00044 
00045   virtual void Reset() {}
00046 
00048   virtual bool Write(const emcManageable& object,
00049                      const PHTimeStamp& tStart,
00050                      int id = -1);
00051 
00052 private:
00053 
00054   ReaderFcn fReader;
00055   WriterFcn fWriter;
00056 
00059   class changeName
00060   {
00061   public:
00062     changeName(const char* name)
00063     {
00064       name_ = emcManageable::GetStorageName(emcManageable::kFile_ASCII);
00065       name_ += ":";
00066       name_ += name;
00067     }
00068     
00069     const char* c_str() const
00070     {
00071       return name_.c_str();
00072     }
00073      
00074   private:
00075     std::string name_;
00076   };
00077 };
00078 
00079 //_____________________________________________________________________________
00080 //_____________________________________________________________________________
00081 //_____________________________________________________________________________
00082 
00083 #ifndef __EMCDATAMANAGER_H__
00084 #include "emcDataManager.h"
00085 #endif
00086 
00087 //_____________________________________________________________________________
00088 template<class T>
00089 emcOMAsciiT<T>::emcOMAsciiT(const char* name, const char* title, 
00090                             emcOMAsciiT<T>::ReaderFcn reader,
00091                             emcOMAsciiT<T>::WriterFcn writer)
00092   : emcObjectManager(changeName(name).c_str(),title),
00093     fReader(reader), fWriter(writer)
00094 {
00095 }
00096 
00097 //_____________________________________________________________________________
00098 template<class T>
00099 emcOMAsciiT<T>::~emcOMAsciiT()
00100 {
00101 }
00102 
00103 //_____________________________________________________________________________
00104 template<class T>
00105 bool
00106 emcOMAsciiT<T>::CanRead(const emcManageable& object) const
00107 {
00108   if ( object.GetSource() != emcManageable::kFile_ASCII )
00109     {
00110       return false;
00111     }
00112   const T* test = dynamic_cast<const T*>(&object);
00113   if ( test ) 
00114     {
00115       return true;
00116     }
00117   else
00118     {
00119       return false;
00120     }
00121 }
00122 
00123 //_____________________________________________________________________________
00124 template<class T>
00125 bool
00126 emcOMAsciiT<T>::CanWrite(const emcManageable& object) const
00127 {
00128   if ( object.GetDestination() != emcManageable::kFile_ASCII )
00129     {
00130       return false;
00131     }
00132   const T* test = dynamic_cast<const T*>(&object);
00133   if ( test ) 
00134     {
00135       return true;
00136     }
00137   else
00138     {
00139       return false;
00140     }
00141 }
00142 
00143 //_____________________________________________________________________________
00144 template<class T>
00145 bool 
00146 emcOMAsciiT<T>::Read(emcManageable& object,
00147                      const PHTimeStamp& /*time_stamp*/,
00148                      int id)
00149 {
00150   T& tobject = static_cast<T&>(object);
00151   return fReader(tobject,id);
00152 }
00153 
00154 //_____________________________________________________________________________
00155 template<class T>
00156 bool 
00157 emcOMAsciiT<T>::Write(const emcManageable& object,
00158                       const PHTimeStamp& /*time_stamp*/,
00159                       int id)
00160 {
00161   if ( fWriter )
00162     {
00163       const T& tobject = static_cast<const T&>(object);
00164       return fWriter(tobject,id);
00165     }
00166   return false;
00167 }
00168 
00169 #endif