Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/sg/mf is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_sg_mf
0005 #define tools_sg_mf
0006 
0007 // mf for multiple field.
0008 
0009 #include "bmf"
0010 
0011 #include "../stype"
0012 #include "../io/iwbuf"
0013 #include "../io/irbuf"
0014 #include "../HEADER"
0015 
0016 namespace tools {
0017 namespace sg {
0018 
0019 template <class T>
0020 class mf : public bmf<T> {
0021   typedef bmf<T> parent;
0022 public:
0023   static const std::string& s_class() {
0024     static const std::string s_v("tools::sg::mf<"+stype(T())+">");
0025     return s_v;
0026   }
0027   virtual void* cast(const std::string& a_class) const {
0028     if(void* p = cmp_cast< mf<T> >(this,a_class)) {return p;}
0029     return parent::cast(a_class);
0030   }
0031   virtual const std::string& s_cls() const {return s_class();}
0032 public:
0033   virtual bool write(io::iwbuf& a_buffer) {
0034     const std::vector<T>& vec = parent::m_values;
0035     return a_buffer.write_vec((uint32)vec.size(),vec.data());
0036   }
0037   virtual bool read(io::irbuf& a_buffer) {
0038     std::vector<T>& vec = parent::m_values;
0039     return a_buffer.read_std_vec(vec);
0040   }
0041   virtual bool dump(std::ostream& a_out) {
0042     const std::vector<T>& vec = parent::m_values;
0043     a_out << "size : " << vec.size() << std::endl;
0044     typedef typename std::vector<T>::const_iterator cit_t;
0045     for(cit_t it=vec.begin();it!=vec.end();++it) {
0046       a_out << "  " << (*it) << std::endl;
0047     }
0048     return true;
0049   }
0050   virtual bool s_value(std::string& a_s) const {a_s.clear();return false;}
0051   virtual bool s2value(const std::string&) {return false;}
0052 public:
0053   mf(){}
0054   mf(const T& a_v):parent(a_v){}
0055   mf(const std::vector<T>& a_v):parent(a_v){}
0056   virtual ~mf(){}
0057 public:
0058   mf(const mf& a_from):parent(a_from){}
0059   mf& operator=(const mf& a_from){
0060     parent::operator=(a_from);
0061     return *this;
0062   }
0063 public:
0064   mf& operator=(const std::vector<T>& a_from){
0065     parent::operator=(a_from);
0066     return *this;
0067   }
0068   mf& operator=(const T& a_v){
0069     parent::operator=(a_v);
0070     return *this;
0071   }
0072 };
0073 
0074 class mf_string : public bmf<std::string> {
0075   TOOLS_HEADER(mf_string,tools::sg::mf_string,bmf<std::string>)
0076 public:
0077   virtual bool write(io::iwbuf& a_buffer) {
0078     return a_buffer.write_vec(m_values);
0079   }
0080   virtual bool read(io::irbuf& a_buffer) {
0081     std::vector<std::string>& vec = parent::m_values;
0082     return a_buffer.read_vec(vec);
0083   }
0084   virtual bool dump(std::ostream& a_out) {
0085     const std::vector<std::string>& vec = parent::m_values;
0086     a_out << "size : " << vec.size() << std::endl;
0087     std::vector<std::string>::const_iterator it;
0088     for(it=vec.begin();it!=vec.end();++it) {
0089       a_out << "  \"" << (*it) << "\"" << std::endl;
0090     }
0091     return true;
0092   }
0093   virtual bool s_value(std::string& a_s) const {a_s.clear();return false;}
0094   virtual bool s2value(const std::string&) {return false;}
0095 public:
0096   mf_string():parent(){}
0097   mf_string(const std::string& a_v):parent(a_v){}
0098   mf_string(const std::vector<std::string>& a_v):parent(a_v){}
0099   virtual ~mf_string(){}
0100 public:
0101   mf_string(const mf_string& a_from):parent(a_from){}
0102   mf_string& operator=(const mf_string& a_from){
0103     parent::operator=(a_from);
0104     return *this;
0105   }
0106 public:
0107   mf_string& operator=(const std::vector<std::string>& a_value){
0108     parent::operator=(a_value);
0109     return *this;
0110   }
0111   mf_string& operator=(const char* a_cstr){
0112     parent::operator=(a_cstr);
0113     return *this;
0114   }
0115 };
0116 
0117 template <class T>
0118 class mf_enum : public bmf<T> {
0119   typedef bmf<T> parent;
0120 public:
0121   static const std::string& s_class() {
0122     static const std::string s_v("tools::sg::mf_enum");
0123     return s_v;
0124   }
0125   virtual void* cast(const std::string& a_class) const {
0126     if(void* p = cmp_cast< mf_enum<T> >(this,a_class)) {return p;}
0127     return parent::cast(a_class);
0128   }
0129   virtual const std::string& s_cls() const {return s_class();}
0130 public:
0131   virtual bool write(io::iwbuf& a_buffer) {
0132     const std::vector<T>& vec = parent::m_values;
0133     std::vector<int16> v; //an enum can be negative.
0134     typedef typename std::vector<T>::const_iterator cit_t;
0135     for(cit_t it=vec.begin();it!=vec.end();++it) v.push_back(*it);
0136     return a_buffer.write_vec((uint32)v.size(),v.data());
0137   }
0138   virtual bool read(io::irbuf& a_buffer) {
0139     std::vector<int16> v; //an enum can be negative.
0140     if(!a_buffer.read_std_vec(v)) return false;
0141     std::vector<T>& vec = parent::m_values;
0142     vec.clear();
0143     std::vector<int16>::const_iterator it;
0144     for(it=v.begin();it!=v.end();++it) vec.push_back((T)(*it));
0145     return true;
0146   }
0147   virtual bool dump(std::ostream& a_out) {
0148     const std::vector<T>& vec = parent::m_values;
0149     a_out << "size : " << vec.size() << std::endl;
0150     typedef typename std::vector<T>::const_iterator cit_t;
0151     for(cit_t it=vec.begin();it!=vec.end();++it) {
0152       a_out << "  " << (*it) << std::endl;
0153     }
0154     return true;
0155   }
0156   virtual bool s_value(std::string& a_s) const {a_s.clear();return false;}
0157   virtual bool s2value(const std::string&) {return false;}
0158 public:
0159   mf_enum():parent(){}
0160   mf_enum(const T& a_v):parent(a_v){}
0161   mf_enum(const std::vector<T>& a_v):parent(a_v){}
0162   virtual ~mf_enum(){}
0163 public:
0164   mf_enum(const mf_enum& a_from):parent(a_from){}
0165   mf_enum& operator=(const mf_enum& a_from){
0166     parent::operator=(a_from);
0167     return *this;
0168   }
0169 };
0170 
0171 //exa mf_vec<colorf,float>
0172 
0173 ///////////////////////////////////////////////////////////
0174 //the three below funcs are for :
0175 //  mf_vec< std::vector<std::string> ,std::string> opts;
0176 ///////////////////////////////////////////////////////////
0177 inline std::ostream& operator<<(std::ostream& a_out,const std::vector<std::string>&) {
0178   //for mf_vec::dump.
0179   return a_out;
0180 }
0181 
0182 inline bool set_from_vec(std::vector<std::string>&,const std::vector<std::string>&) {
0183   //for mf_vec::read(io::irbuf&)
0184   return false;
0185 }
0186 
0187 inline const std::string* get_data(const std::vector<std::string>& a_v) {
0188   return a_v.data();
0189 }
0190 ///////////////////////////////////////////////////////////
0191 ///////////////////////////////////////////////////////////
0192 ///////////////////////////////////////////////////////////
0193 
0194 
0195 template <class T,class TT>
0196 class mf_vec : public bmf<T> {
0197   typedef bmf<T> parent;
0198 public:
0199   static const std::string& s_class() {
0200     static const std::string s_v("tools::sg::mf_vec<"+stype(T())+","+stype(TT())+">");
0201     return s_v;
0202   }
0203   virtual void* cast(const std::string& a_class) const {
0204     if(void* p = cmp_cast< mf_vec<T,TT> >(this,a_class)) {return p;}
0205     return parent::cast(a_class);
0206   }
0207   virtual const std::string& s_cls() const {return s_class();}
0208 public:
0209   virtual bool write(io::iwbuf& a_buffer) {
0210     const std::vector<T>& vec = parent::m_values;
0211     typedef typename std::vector<TT> vec_t;
0212     std::vector<vec_t> vec_vec;
0213     typedef typename std::vector<T>::const_iterator cit_t;
0214     for(cit_t it=vec.begin();it!=vec.end();++it) {
0215 
0216       const T& v = (*it);
0217       size_t num = v.size();
0218       const TT* d = get_data(v);
0219 
0220       std::vector<TT> std_vec(num);
0221       for(size_t i=0;i<num;i++) std_vec[i] = d[i];
0222 
0223       vec_vec.push_back(std_vec);
0224     }
0225     return a_buffer.write_std_vec_vec(vec_vec);
0226   }
0227   virtual bool read(io::irbuf& a_buffer) {
0228     std::vector<T>& vec = parent::m_values;
0229     vec.clear();
0230     typedef typename std::vector<TT> vec_t;
0231     std::vector<vec_t> vec_vec;
0232     if(!a_buffer.read_std_vec_vec(vec_vec)) return false;
0233     typedef typename std::vector<vec_t>::iterator _it_t;
0234     for(_it_t it=vec_vec.begin();it!=vec_vec.end();++it) {
0235       T x;
0236       if(!set_from_vec(x,*it)) {vec.clear();return false;}
0237       vec.push_back(x);
0238     }
0239     return true;
0240   }
0241   virtual bool dump(std::ostream& a_out) {
0242     const std::vector<T>& vec = parent::m_values;
0243     a_out << "size : " << vec.size() << std::endl;
0244     typedef typename std::vector<T>::const_iterator cit_t;
0245     for(cit_t it=vec.begin();it!=vec.end();++it) {
0246       a_out << "  " << (*it) << std::endl;
0247     }
0248     return true;
0249   }
0250   virtual bool s_value(std::string& a_s) const {a_s.clear();return false;}
0251   virtual bool s2value(const std::string&) {return false;}
0252 public:
0253   mf_vec():parent(){}
0254   mf_vec(const T& a_v):parent(a_v){}
0255   mf_vec(const std::vector<T>& a_v):parent(a_v){}
0256   virtual ~mf_vec(){}
0257 public:
0258   mf_vec(const mf_vec& a_from):parent(a_from){}
0259   mf_vec& operator=(const mf_vec& a_from){
0260     parent::operator=(a_from);
0261     return *this;
0262   }
0263 };
0264 
0265 template <class T>
0266 class mf_std_vec : public bmf< std::vector<T> > {
0267   typedef bmf< std::vector<T> > parent;
0268 public:
0269   static const std::string& s_class() {
0270     static const std::string s_v("tools::sg::mf_std_vec<"+stype(T())+">");
0271     return s_v;
0272   }
0273   virtual void* cast(const std::string& a_class) const {
0274     if(void* p = cmp_cast< mf_std_vec<T> >(this,a_class)) {return p;}
0275     return parent::cast(a_class);
0276   }
0277   virtual const std::string& s_cls() const {return s_class();}
0278 public:
0279   virtual bool write(io::iwbuf& a_buffer) {
0280     //used in exlib/sg/text_freetype::unitext
0281     const std::vector< std::vector<T> >& vec = parent::m_values;
0282     return a_buffer.write_std_vec_vec(vec);
0283   }
0284   virtual bool read(io::irbuf& a_buffer) {
0285     std::vector< std::vector<T> >& vec = parent::m_values;
0286     return a_buffer.read_std_vec_vec(vec);
0287   }
0288   virtual bool dump(std::ostream& a_out) {
0289     const std::vector< std::vector<T> >& vec = parent::m_values;
0290     a_out << "size : " << vec.size() << std::endl;
0291     typedef typename std::vector< std::vector<T> >::const_iterator cit_t;
0292     for(cit_t it=vec.begin();it!=vec.end();++it) {
0293     }
0294     return true;
0295   }
0296   virtual bool s_value(std::string& a_s) const {a_s.clear();return false;}
0297   virtual bool s2value(const std::string&) {return false;}
0298 public:
0299   mf_std_vec():parent(){}
0300   mf_std_vec(const T& a_v):parent(a_v){}
0301   mf_std_vec(const std::vector<T>& a_v):parent(a_v){}
0302   virtual ~mf_std_vec(){}
0303 public:
0304   mf_std_vec(const mf_std_vec& a_from):parent(a_from){}
0305   mf_std_vec& operator=(const mf_std_vec& a_from){
0306     parent::operator=(a_from);
0307     return *this;
0308   }
0309 };
0310 
0311 }}
0312 
0313 #endif