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