Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_sg_sf_vec 5 #define tools_sg_sf_vec 6 7 #include "sf" 8 9 #include "../words" 10 11 namespace tools { 12 namespace sg { 13 14 template <class T,class TT> //exa sf_vec<colorf,float> 15 class sf_vec : public bsf<T> { 16 typedef bsf<T> parent; 17 public: 18 static const std::string& s_class() { 19 static const std::string s_v("tools::sg::sf_vec<"+stype(T())+","+stype(TT())+">"); 20 return s_v; 21 } 22 virtual void* cast(const std::string& a_class) const { 23 if(void* p = cmp_cast< sf_vec<T,TT> >(this,a_class)) {return p;} 24 return parent::cast(a_class); 25 } 26 virtual const std::string& s_cls() const {return s_class();} 27 public: 28 virtual bool write(io::iwbuf& a_buffer) { 29 const T& vec = parent::m_value; 30 const TT* d = get_data(vec); 31 return a_buffer.write_vec(vec.size(),d); 32 } 33 virtual bool read(io::irbuf& a_buffer) { 34 T& vec = parent::m_value; 35 uint32 n; 36 TT* v; 37 if(!a_buffer.read_vec(n,v)) { 38 //::printf("debug : sf_vec::read : failed(0).\n"); 39 return false; 40 } 41 if(n!=vec.size()) { 42 delete [] v; 43 #ifdef TOOLS_MEM 44 mem::decrement(s_new().c_str()); 45 #endif 46 //::printf("debug : sf_vec::read : failed(1) : %d (expected %d).\n", 47 // n,vec.size()); 48 return false; 49 } 50 for(uint32 index=0;index<n;index++) vec[index] = v[index]; 51 delete [] v; 52 #ifdef TOOLS_MEM 53 mem::decrement(s_new().c_str()); 54 #endif 55 return true; 56 } 57 virtual bool dump(std::ostream& a_out) { 58 a_out << parent::m_value << std::endl; 59 return true; 60 } 61 virtual bool s_value(std::string& a_s) const { 62 a_s.clear(); 63 const T& vec = parent::m_value; 64 for(size_t index=0;index<vec.size();index++) { 65 if(index) a_s += ' '; 66 std::ostringstream strm; 67 strm << vec[index]; 68 a_s += strm.str(); 69 } 70 return true; 71 } 72 virtual bool s2value(const std::string& a_s) { 73 std::vector<std::string> ws; 74 words(a_s," ",false,ws); 75 T& vec = parent::m_value; 76 if(ws.size()!=vec.size()) return false; 77 T old_vec = vec; 78 for(size_t index=0;index<vec.size();index++) { 79 std::istringstream strm(ws[index].c_str()); 80 TT v; 81 strm >> v; 82 if(strm.fail()) { 83 vec = old_vec; 84 return false; 85 } 86 if(vec[index]!=v) parent::m_touched = true; 87 vec[index] = v; 88 } 89 return true; 90 } 91 public: 92 sf_vec():parent(){} 93 sf_vec(const T& a_value):parent(a_value){} 94 virtual ~sf_vec(){} 95 public: 96 sf_vec(const sf_vec& a_from) 97 :parent(a_from){} 98 sf_vec& operator=(const sf_vec& a_from){ 99 parent::operator=(a_from); 100 return *this; 101 } 102 public: 103 sf_vec& operator=(const T& a_value){ 104 parent::operator=(a_value); 105 return *this; 106 } 107 sf_vec& operator+=(const T& a_value) { 108 parent::value(parent::value()+a_value); 109 return *this; 110 } 111 }; 112 113 }} 114 115 #endif