inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/sg/mf
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_sg_mf
00005 #define inlib_sg_mf
00006 
00007 // mf for multiple field.
00008 
00009 #include "field"
00010 
00011 #include "../cstr"
00012 #include "../vdata"
00013 
00014 namespace inlib {
00015 namespace sg {
00016 
00017 template <class T>
00018 class bmf : public field {
00019   // bmf is intended to have no implementation of :
00020   //   virtual bool write(io::iwbuf&)  
00021   //   virtual bool read(io::irbuf&)  
00022 public:
00023   bmf(){}
00024   //bmf(const T& a_value){m_values.push_back(a_value);}
00025   virtual ~bmf(){m_values.clear();}
00026 public:
00027   bmf(const bmf& a_from):field(a_from),m_values(a_from.m_values){}
00028   bmf& operator=(const bmf& a_from){
00029     field::operator=(a_from);
00030     if(a_from.m_values!=m_values) m_touched = true;
00031     m_values = a_from.m_values;
00032     return *this;
00033   }
00034 public:
00035   bmf& operator=(const std::vector<T>& a_from){
00036     if(a_from!=m_values) m_touched = true;
00037     m_values = a_from;
00038     return *this;
00039   }
00040   bool operator==(const bmf& a_from) const {
00041     return m_values==a_from.m_values;
00042   }
00043   bool operator!=(const bmf& a_from) const { 
00044     return !operator==(a_from);
00045   }
00046   const T& operator[](unsigned int a_index) const {
00047     //WARNING : no check is done on a_index.
00048     return m_values[a_index];    
00049   }
00050   T& operator[](unsigned int a_index) {
00051     //WARNING : no check is done on a_index.
00052     return m_values[a_index];    
00053   }
00054 public:
00055   //unsigned int number() const {return m_values.size();}
00056   unsigned int size() const {return m_values.size();}
00057   bool empty() const {return m_values.empty();}
00058   const std::vector<T>& values() const {return m_values;}
00059   std::vector<T>& values() {return m_values;}
00060   void add(const T& a_value) {
00061     m_values.push_back(a_value);
00062     m_touched = true;
00063   }
00064   typedef typename std::vector<T>::iterator it_t;
00065   void insert(const it_t& a_it,const T& a_value) {
00066     m_values.insert(a_it,a_value);
00067     m_touched = true;
00068   }
00069   bool set_value(unsigned int a_index,const T& a_value) {
00070     if(a_index>=m_values.size()) return false;
00071     if(m_values[a_index]!=a_value) m_touched = true;
00072     m_values[a_index] = a_value;
00073     return true;
00074   }
00075   bool get_value(unsigned int a_index,T& a_value) {
00076     if(a_index>=m_values.size()) {a_value=T();return false;}
00077     a_value = m_values[a_index];
00078     return true;
00079   }
00080   void clear() {
00081     if(m_values.size()) m_touched = true;
00082     m_values.clear();
00083   }
00084   //void set(const std::vector<T>& a_values) {
00085   //  if(a_values!=m_values) m_touched = true;
00086   //  m_values = a_values;
00087   //}
00088 protected:
00089   std::vector<T> m_values;
00090 };
00091 
00092 }}
00093 
00094 #include "../io/iwbuf"
00095 #include "../io/irbuf"
00096 
00097 namespace inlib {
00098 namespace sg {
00099 
00100 template <class T>
00101 class mf : public bmf<T> {
00102 public:
00103   virtual bool write(io::iwbuf& a_buffer) {
00104     const std::vector<T>& vec = bmf<T>::m_values;
00105     return a_buffer.write_vec(vec.size(),vec_data(vec));
00106   }
00107   virtual bool read(io::irbuf& a_buffer) {
00108     std::vector<T>& vec = bmf<T>::m_values;
00109     return a_buffer.read_std_vec(vec);
00110   }
00111   virtual bool dump(std::ostream& a_out) {
00112     const std::vector<T>& vec = bmf<T>::m_values;
00113     a_out << "size : " << vec.size() << std::endl;
00114     typedef typename std::vector<T>::const_iterator cit_t;
00115     for(cit_t it=vec.begin();it!=vec.end();++it) {
00116       a_out << "  " << (*it) << std::endl;
00117     }
00118     return true;
00119   }
00120 public:
00121   mf(){}
00122 public:
00123   mf(const mf& a_from)
00124   : bmf<T>(a_from)
00125   {}
00126   mf& operator=(const mf& a_from){
00127     //typedef typename bmf<T>::iterator bmf_t;
00128     bmf<T>::operator=(a_from);
00129     return *this;
00130   }
00131 public:
00132   mf& operator=(const std::vector<T>& a_from){
00133     bmf<T>::operator=(a_from);
00134     return *this;
00135   }
00136 };
00137 
00138 class mf_string : public bmf<std::string> {
00139 public:
00140   virtual bool write(io::iwbuf& a_buffer) {
00141     return a_buffer.write_vec(m_values);
00142   }
00143   virtual bool read(io::irbuf& a_buffer) {
00144     std::vector<std::string>& vec = bmf<std::string>::m_values;
00145     return a_buffer.read_vec(vec);
00146   }
00147   virtual bool dump(std::ostream& a_out) {
00148     const std::vector<std::string>& vec = bmf<std::string>::m_values;
00149     a_out << "size : " << vec.size() << std::endl;
00150     std::vector<std::string>::const_iterator it;
00151     for(it=vec.begin();it!=vec.end();++it) {
00152       a_out << "  \"" << (*it) << "\"" << std::endl;
00153     }
00154     return true;
00155   }
00156 public:
00157   mf_string(): bmf<std::string>(){}
00158   virtual ~mf_string(){}
00159 public:
00160   mf_string(const mf_string& a_from)
00161   : bmf<std::string>(a_from){}
00162   mf_string& operator=(const mf_string& a_from){
00163     bmf<std::string>::operator=(a_from);
00164     return *this;
00165   }
00166 public:
00167   mf_string& operator=(const std::vector<std::string>& a_value){
00168     bmf<std::string>::operator=(a_value);
00169     return *this;
00170   }
00171 };
00172 
00173 //exa exlib::sg::entries.mf_vec<entry_type>
00174 
00175 template <class T>
00176 class mf_enum : public bmf<T> {
00177 public:
00178   virtual bool write(io::iwbuf& a_buffer) {
00179     const std::vector<T>& vec = bmf<T>::m_values;
00180     std::vector<int> v; //an enum can be negative.
00181     typedef typename std::vector<T>::const_iterator cit_t;
00182     for(cit_t it=vec.begin();it!=vec.end();++it) v.push_back(*it);
00183     return a_buffer.write_vec(v.size(),vec_data(v));
00184   }
00185   virtual bool read(io::irbuf& a_buffer) {
00186     std::vector<int> v; //an enum can be negative.
00187     if(!a_buffer.read_std_vec(v)) return false;
00188     std::vector<T>& vec = bmf<T>::m_values;
00189     vec.clear();
00190     std::vector<int>::const_iterator it;
00191     for(it=v.begin();it!=v.end();++it) vec.push_back((T)(*it));
00192     return true;
00193   }
00194   virtual bool dump(std::ostream& a_out) {
00195     const std::vector<T>& vec = bmf<T>::m_values;
00196     a_out << "size : " << vec.size() << std::endl;
00197     typedef typename std::vector<T>::const_iterator cit_t;
00198     for(cit_t it=vec.begin();it!=vec.end();++it) {
00199       a_out << "  " << (*it) << std::endl;
00200     }
00201     return true;
00202   }
00203 public:
00204   mf_enum(): bmf<T>(){}
00205   virtual ~mf_enum(){}
00206 public:
00207   mf_enum(const mf_enum& a_from)
00208   : bmf<T>(a_from){}
00209   mf_enum& operator=(const mf_enum& a_from){
00210     bmf<T>::operator=(a_from);
00211     return *this;
00212   }
00213 };
00214 
00215 //exa mf_vec<colorf,float>
00216 //exa mf_vec<inlib::vecs,std::string>
00217 
00218 template <class T,class TT> 
00219 class mf_vec : public bmf<T> {
00220 public:
00221   virtual bool write(io::iwbuf& a_buffer) {
00222     const std::vector<T>& vec = bmf<T>::m_values;
00223     typedef typename std::vector<TT> vec_t;
00224     std::vector<vec_t> vec_vec;
00225     typedef typename std::vector<T>::const_iterator cit_t;
00226     for(cit_t it=vec.begin();it!=vec.end();++it) {
00227       vec_vec.push_back((*it).vector());
00228     }
00229     return a_buffer.write_std_vec_vec(vec_vec);
00230   }
00231   virtual bool read(io::irbuf& a_buffer) {
00232     std::vector<T>& vec = bmf<T>::m_values;
00233     vec.clear();
00234     typedef typename std::vector<TT> vec_t;
00235     std::vector<vec_t> vec_vec;
00236     if(!a_buffer.read_std_vec_vec(vec_vec)) return false;
00237     typedef typename std::vector<vec_t>::iterator it_t;
00238     for(it_t it=vec_vec.begin();it!=vec_vec.end();++it) {
00239       T x;
00240       if(!x.fill(*it)) {vec.clear();return false;}
00241       vec.push_back(x);
00242     }
00243     return true;
00244   }
00245   virtual bool dump(std::ostream& a_out) {
00246     const std::vector<T>& vec = bmf<T>::m_values;
00247     a_out << "size : " << vec.size() << std::endl;
00248     typedef typename std::vector<T>::const_iterator cit_t;
00249     for(cit_t it=vec.begin();it!=vec.end();++it) {
00250       a_out << "  " << (*it) << std::endl;
00251     }
00252     return true;
00253   }
00254 public:
00255   mf_vec(): bmf<T>(){}
00256   virtual ~mf_vec(){}
00257 public:
00258   mf_vec(const mf_vec& a_from)
00259   : bmf<T>(a_from){}
00260   mf_vec& operator=(const mf_vec& a_from){
00261     bmf<T>::operator=(a_from);
00262     return *this;
00263   }
00264 };
00265 
00266 template <class T>
00267 class mf_std_vec : public bmf< std::vector<T> > {
00268 public:
00269   virtual bool write(io::iwbuf& a_buffer) {
00270     //used in exlib/sg/text_freetype::unitext
00271     const std::vector< std::vector<T> >& vec = bmf< std::vector<T> >::m_values;
00272     return a_buffer.write_std_vec_vec(vec);
00273   }
00274   virtual bool read(io::irbuf& a_buffer) {
00275     std::vector< std::vector<T> >& vec = bmf< std::vector<T> >::m_values;
00276     return a_buffer.read_std_vec_vec(vec);
00277   }
00278   virtual bool dump(std::ostream& a_out) {
00279     const std::vector< std::vector<T> >& vec = bmf< std::vector<T> >::m_values;
00280     a_out << "size : " << vec.size() << std::endl;
00281     typedef typename std::vector< std::vector<T> >::const_iterator cit_t;
00282     for(cit_t it=vec.begin();it!=vec.end();++it) {
00283       //a_out << "  " << (*it) << std::endl;
00284     }
00285     return true;
00286   }
00287 public:
00288   mf_std_vec(): bmf< std::vector<T> >(){}
00289   virtual ~mf_std_vec(){}
00290 public:
00291   mf_std_vec(const mf_std_vec& a_from)
00292   : bmf< std::vector<T> >(a_from){}
00293   mf_std_vec& operator=(const mf_std_vec& a_from){
00294     bmf< std::vector<T> >::operator=(a_from);
00295     return *this;
00296   }
00297 };
00298 
00299 }}
00300 
00301 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines