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