My Project
HeaderBase.hh
00001 /*
00002 $Id: HeaderBase.hh 2247 2011-04-27 05:20:41Z jisuzuki $
00003 */
00004 
00005 
00006 #ifndef HEADERBASE
00007 #define HEADERBASE
00008 
00009 #include <map>
00010 #include <boost/serialization/serialization.hpp>
00011 #include <boost/serialization/map.hpp>
00012 #include <boost/serialization/string.hpp>
00013 #include <napi.h>
00014 
00015 #include "Header.hh"
00016 #include "Map.hh"
00017 #include "SplitString.hh"
00018 #include "ReadWriteTextFile.hh"
00019 #include "CppToPython.hh"
00020 #include "StringTools.hh"
00021 
00023 
00075 class HeaderBase
00076 {
00077 private:
00078   std::map<string, int> _keymap;
00084   friend class boost::serialization::access;
00088   template <class Archive>
00089   void serialize(Archive &ar, const unsigned int ver);
00090 
00091   //friend class WriteNeXusFile;        // to aboid re-declaration
00095   template <class Write>
00096   void NXwrite(Write &W) const ;
00097 
00098   friend class ReadNeXusFile;
00102   template <class Read>
00103   void NXread(Read &R) ;
00104 
00105 protected:
00106   Map< Int4 >              *Int4Map;
00107   Map< Double >            *DoubleMap;
00108   Map< string >            *StringMap;
00109   Map< vector< Int4 >  >  *Int4VectorMap;
00110   Map< vector< Double > > *DoubleVectorMap;
00111   Map< vector< string > > *StringVectorMap;
00112   void MakeMaps();
00113 //  CppToPython *PyConverter;
00114 
00115 public:
00116   HeaderBase();
00119   //HeaderBase( string DumpedString );
00120   /* Constructor, the argument is a string
00121    which should be generated by "DumpToString()".
00122    The new object constructed by itself has the same information
00123    as the contents of the argument.*/
00124 
00125  ~HeaderBase();
00130   HeaderBase( const HeaderBase &ob );
00132   //HeaderBase &operator=( HeaderBase &ob );
00133   HeaderBase operator=( const HeaderBase &ob );
00134   
00135   void Add( string Key, Int4   value );
00137   void Add( string Key, Double value );
00139   void Add( string Key, string value );
00141   void Add( string Key, vector<Int4>   value );
00143   void Add( string Key, vector<Double> value );
00145   void Add( string Key, vector<string> value );
00148   void AddHeader( string Key, Int4   value ){ Add( Key, value ); }
00150   void AddHeader( string Key, Double value ){ Add( Key, value ); }
00152   void AddHeader( string Key, string value ){ Add( Key, value ); }
00154   void AddHeader( string Key, vector<Int4>   value ){ Add( Key, value ); }
00156   void AddHeader( string Key, vector<Double> value ){ Add( Key, value ); }
00158   void AddHeader( string Key, vector<string> value ){ Add( Key, value ); }
00161   void add( string Key, Int4   value ){ Add( Key, value ); }
00163   void add( string Key, Double value ){ Add( Key, value ); }
00165   void add( string Key, string value ){ Add( Key, value ); }
00167   void add( string Key, vector<Int4>   value ){ Add( Key, value ); }
00169   void add( string Key, vector<Double> value ){ Add( Key, value ); }
00171   void add( string Key, vector<string> value ){ Add( Key, value ); }
00174   void AddInt4List( string Key, PyObject *List );
00175   void AddDoubleList( string Key, PyObject *List );
00176   void AddStringList( string Key, PyObject *List );
00177   
00178   bool Empty(void) {return _keymap.empty(); }
00179   
00180   void Erase( string Key );
00188   void erase( string Key ){ Erase( Key ); }
00191   void clear();
00195   void Clear(){ clear(); }
00198   void OverWrite( string Key, Int4   value ){Erase( Key ); Add( Key, value );}
00200   void OverWrite( string Key, Double value ){Erase( Key ); Add( Key, value );}
00202   void OverWrite( string Key, string value ){Erase( Key ); Add( Key, value );}
00204   void OverWrite( string Key, vector<Int4>   value ){Erase( Key ); Add( Key, value );}
00206   void OverWrite( string Key, vector<Double> value ){Erase( Key ); Add( Key, value );}
00208   void OverWrite( string Key, vector<string> value ){Erase( Key ); Add( Key, value );}
00211   void Dump();
00213   PyObject* PyDump();
00217   void dump(){ Dump(); }
00220   void DumpHeader(){ Dump(); }
00221 
00222   void Search( string Key );
00226   UInt4 CheckKey( string Key ) {return (_keymap[Key] > 0) ? 1 : 0;}
00234   Int4 PutInt4( string Key );
00236   Double PutDouble( string Key );
00238   string PutString( string Key );
00240   vector<Int4>   PutInt4Vector( string Key );
00242   vector<Double> PutDoubleVector( string Key );
00244   vector<string> PutStringVector( string Key );
00247   Int4   PutInt4(   UInt4 Index ){ return Int4Map->Put(Index); }
00248   Double PutDouble( UInt4 Index ){ return DoubleMap->Put(Index); }
00249   string PutString( UInt4 Index ){ return StringMap->Put(Index); }
00250   vector<Int4>   PutInt4Vector(   UInt4 Index ){ return Int4VectorMap->Put(Index); }
00251   vector<Double> PutDoubleVector( UInt4 Index ){ return DoubleVectorMap->Put(Index); }
00252   vector<string> PutStringVector( UInt4 Index ){ return StringVectorMap->Put(Index); }
00253 
00254   string DumpToString();
00261   void InputString( string s );
00265   void InputFile( string FileName );
00266 
00267   Int4   i( string Key ){ return PutInt4( Key ); }
00269   Double d( string Key ){ return PutDouble( Key ); }
00271   Float  f( string Key){ return (Double)( d( Key ) ); }
00273   string s( string Key ){ return PutString( Key ); }
00275   vector<Int4>   iv( string Key ){ return PutInt4Vector( Key ); }
00277   vector<Double> dv( string Key ){ return PutDoubleVector( Key ); }
00279   vector<string> sv( string Key ){ return PutStringVector( Key ); }
00282   friend class WriteNeXusFile;
00287   friend class NeutronExchanger;
00292   UInt4 PutKeyLocation( string Key ){ return _keymap[Key]; };
00296 };
00297  
00299 template <class Archive>
00300 void HeaderBase::
00301 serialize(Archive &ar, const unsigned int ver) {
00302   ar & boost::serialization::make_nvp("Keymap", _keymap);
00303 
00304   ar & boost::serialization::make_nvp("Int4Map",      *Int4Map);
00305   ar & boost::serialization::make_nvp("DoubleMap",    *DoubleMap);
00306   ar & boost::serialization::make_nvp("StringMap",    *StringMap);
00307   ar & boost::serialization::make_nvp("Int4Vector",   *Int4VectorMap);
00308   ar & boost::serialization::make_nvp("DoubleVector", *DoubleVectorMap);
00309   ar & boost::serialization::make_nvp("StringVector", *StringVectorMap);
00310 
00311 }
00312 
00314 template <class Write>
00315 void HeaderBase::
00316 NXwrite(Write &W) const {
00317         W.WriteData("_Keymap", _keymap);
00318 
00319   for (std::map<std::string, int>::const_iterator p=_keymap.begin(),
00320         end=_keymap.end();p!=end;++p) {
00321     switch (p->second) {
00322     case 1:
00323       W.WriteData(p->first, (*Int4Map)[p->first]);
00324       break;
00325     case 2:
00326       W.WriteData(p->first, (*DoubleMap)[p->first]);
00327       break;
00328     case 3:
00329       W.WriteData(p->first, (*StringMap)[p->first]);
00330       break;
00331     case 4:
00332       W.WriteData(p->first, (*Int4VectorMap)[p->first]);
00333       break;
00334     case 5:
00335       W.WriteData(p->first, (*DoubleVectorMap)[p->first]);
00336       break;
00337     case 6:
00338       W.WriteData(p->first, (*StringVectorMap)[p->first]);
00339       break;
00340     }
00341   }
00342 
00343 }
00345 template <class Read>
00346 void HeaderBase::
00347 NXread(Read &R) {
00348   R.ReadData("_Keymap", _keymap);
00349 
00350   for (std::map<std::string, int>::const_iterator p=_keymap.begin(),
00351         end=_keymap.end();p!=end;++p) {
00352     switch (p->second) {
00353     case 1:
00354       R.ReadData(p->first, (*Int4Map)[p->first]);
00355       break;
00356     case 2:
00357       R.ReadData(p->first, (*DoubleMap)[p->first]);
00358       break;
00359     case 3:
00360       R.ReadData(p->first, (*StringMap)[p->first]);
00361       break;
00362     case 4:
00363       R.ReadData(p->first, (*Int4VectorMap)[p->first]);
00364       break;
00365     case 5:
00366       R.ReadData(p->first, (*DoubleVectorMap)[p->first]);
00367       break;
00368     case 6:
00369       R.ReadData(p->first, (*StringVectorMap)[p->first]);
00370       break;
00371     }
00372   }
00373 }
00374 #endif 
 All Classes Functions Variables Friends