Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/sg/bmf 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_bmf
0005 #define tools_sg_bmf
0006 
0007 // mf for multiple field.
0008 
0009 // bmf is intended to have no implementation of :
0010 //   virtual bool write(io::iwbuf&)
0011 //   virtual bool read(io::irbuf&)
0012 
0013 #include "field"
0014 
0015 #include "../vdata"
0016 
0017 namespace tools {
0018 namespace sg {
0019 
0020 template <class T>
0021 class bmf : public field {
0022   typedef field parent;
0023 public:
0024   static const std::string& s_class() {
0025     //we do not use stype(T()).
0026     static const std::string s_v("tools::sg::bmf");
0027     return s_v;
0028   }
0029 //static bool is_a(const std::string& a_class) {return rcmp(a_class,s_class());}
0030   virtual void* cast(const std::string& a_class) const {
0031     if(void* p = cmp_cast< bmf<T> >(this,a_class)) {return p;}
0032     return parent::cast(a_class);
0033   }
0034   virtual const std::string& s_cls() const {return s_class();}
0035 public:
0036   bmf(){}
0037   bmf(const T& a_value) {m_values.push_back(a_value);}
0038   bmf(const std::vector<T>& a_v) {m_values = a_v;}
0039   virtual ~bmf(){m_values.clear();}
0040 public:
0041   bmf(const bmf& a_from):parent(a_from),m_values(a_from.m_values){}
0042   bmf& operator=(const bmf& a_from){
0043     parent::operator=(a_from);
0044     if(a_from.m_values!=m_values) m_touched = true;
0045     m_values = a_from.m_values;
0046     return *this;
0047   }
0048 public:
0049   bmf& operator=(const std::vector<T>& a_from){
0050     if(a_from!=m_values) m_touched = true;
0051     m_values = a_from;
0052     return *this;
0053   }
0054   bool operator==(const bmf& a_from) const {
0055     return m_values==a_from.m_values;
0056   }
0057   bool operator!=(const bmf& a_from) const {
0058     return !operator==(a_from);
0059   }
0060   const T& operator[](size_t a_index) const {
0061     //WARNING : no check is done on a_index.
0062     return m_values[a_index];
0063   }
0064   T& operator[](size_t a_index) {
0065     //WARNING : no check is done on a_index.
0066     return m_values[a_index];
0067   }
0068 public:
0069   size_t size() const {return m_values.size();}
0070   bool empty() const {return m_values.empty();}
0071   const std::vector<T>& values() const {return m_values;}
0072   std::vector<T>& values() {return m_values;}
0073   void add(const T& a_value) {
0074     m_values.push_back(a_value);
0075     m_touched = true;
0076   }
0077   void add(const std::vector<T>& a_vals) {
0078     if(a_vals.empty()) return;
0079     typedef typename std::vector<T>::const_iterator const_it_t;
0080     for(const_it_t it=a_vals.begin();it!=a_vals.end();++it){
0081       m_values.push_back(*it);
0082     }
0083     m_touched = true;
0084   }
0085   void add_allocated(size_t& a_pos,const T& a_1,const T& a_2,const T& a_3) { //used in sg::plotter.
0086     std::vector<T>& v = m_values;
0087     v[a_pos] = a_1;a_pos++;
0088     v[a_pos] = a_2;a_pos++;
0089     v[a_pos] = a_3;a_pos++;
0090     m_touched = true;
0091   }
0092   typedef typename std::vector<T>::iterator it_t;
0093   void insert(const it_t& a_it,const T& a_value) {
0094     m_values.insert(a_it,a_value);
0095     m_touched = true;
0096   }
0097   bool set_value(size_t a_index,const T& a_value) {
0098     if(a_index>=m_values.size()) return false;
0099     if(m_values[a_index]!=a_value) m_touched = true;
0100     m_values[a_index] = a_value;
0101     return true;
0102   }
0103   bool get_value(size_t a_index,T& a_value) {
0104     if(a_index>=m_values.size()) {a_value=T();return false;}
0105     a_value = m_values[a_index];
0106     return true;
0107   }
0108   void clear() {
0109     if(m_values.size()) m_touched = true;
0110     m_values.clear();
0111   }
0112 
0113   void set_values(const std::vector<T>& a_values) {
0114     if(a_values!=m_values) m_touched = true;
0115     m_values = a_values;
0116   }
0117   void set_value(const T& a_value) { //used in ArcheryTune.
0118     bool to_resize = m_values.size()==1?false:true;
0119     bool is_eq = ( (m_values.size()>=1) && (m_values[0]==a_value) ) ? true : false;
0120     if(to_resize) m_values.resize(1);
0121     if(to_resize || !is_eq) m_touched = true;
0122     m_values[0] = a_value;
0123   }
0124 
0125 public: //for iv2sg
0126   //bool setValues(size_t a_index,size_t a_num,const T* a_vs) {
0127   //  for(size_t index=0;index<a_num;index++) {
0128   //    if(!set1Value(a_index+index,a_vs[index])) return false;
0129   //  }
0130   //  return true;
0131   //}
0132 
0133   bool setValues(size_t a_index,size_t a_num,const T* a_vs) {
0134     //  012345678
0135     //    234
0136     if((a_index+a_num)>=m_values.size()) m_values.resize(a_index+a_num);
0137     for(size_t index=0;index<a_num;index++) {
0138       if(a_vs[index]!=m_values[a_index+index]) m_touched = true;
0139       m_values[a_index+index] = a_vs[index];
0140     }
0141     return true;
0142   }
0143 
0144   bool set1Value(size_t a_index,const T& a_value) {
0145     if(a_index>=m_values.size()) m_values.resize(a_index+1);
0146     if(m_values[a_index]!=a_value) m_touched = true;
0147     m_values[a_index] = a_value;
0148     return true;
0149   }
0150   bool setValue(const T& a_value) {set_value(a_value);return true;}
0151 
0152   bmf& operator=(const T& a_v){
0153     if(!setValue(a_v)) {}
0154     return *this;
0155   }
0156   size_t getNum() const {return m_values.size();}
0157   T* getValues(size_t a_start) { //for gopaw.
0158     if(a_start>=(m_values.size()+1)) return 0;
0159     T* data = vec_data(m_values);
0160     return data+a_start;
0161   }
0162 protected:
0163   std::vector<T> m_values;
0164 };
0165 
0166 }}
0167 
0168 #endif