Warning, /include/Geant4/tools/sg/sf_vec 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_sf_vec
0005 #define tools_sg_sf_vec
0006
0007 #include "sf"
0008
0009 #include "../words"
0010
0011 namespace tools {
0012 namespace sg {
0013
0014 template <class T,class TT> //exa sf_vec<colorf,float>
0015 class sf_vec : public bsf<T> {
0016 typedef bsf<T> parent;
0017 public:
0018 static const std::string& s_class() {
0019 static const std::string s_v("tools::sg::sf_vec<"+stype(T())+","+stype(TT())+">");
0020 return s_v;
0021 }
0022 virtual void* cast(const std::string& a_class) const {
0023 if(void* p = cmp_cast< sf_vec<T,TT> >(this,a_class)) {return p;}
0024 return parent::cast(a_class);
0025 }
0026 virtual const std::string& s_cls() const {return s_class();}
0027 public:
0028 virtual bool write(io::iwbuf& a_buffer) {
0029 const T& vec = parent::m_value;
0030 const TT* d = get_data(vec);
0031 return a_buffer.write_vec(vec.size(),d);
0032 }
0033 virtual bool read(io::irbuf& a_buffer) {
0034 T& vec = parent::m_value;
0035 uint32 n;
0036 TT* v;
0037 if(!a_buffer.read_vec(n,v)) {
0038 //::printf("debug : sf_vec::read : failed(0).\n");
0039 return false;
0040 }
0041 if(n!=vec.size()) {
0042 delete [] v;
0043 #ifdef TOOLS_MEM
0044 mem::decrement(s_new().c_str());
0045 #endif
0046 //::printf("debug : sf_vec::read : failed(1) : %d (expected %d).\n",
0047 // n,vec.size());
0048 return false;
0049 }
0050 for(uint32 index=0;index<n;index++) vec[index] = v[index];
0051 delete [] v;
0052 #ifdef TOOLS_MEM
0053 mem::decrement(s_new().c_str());
0054 #endif
0055 return true;
0056 }
0057 virtual bool dump(std::ostream& a_out) {
0058 a_out << parent::m_value << std::endl;
0059 return true;
0060 }
0061 virtual bool s_value(std::string& a_s) const {
0062 a_s.clear();
0063 const T& vec = parent::m_value;
0064 for(size_t index=0;index<vec.size();index++) {
0065 if(index) a_s += ' ';
0066 std::ostringstream strm;
0067 strm << vec[index];
0068 a_s += strm.str();
0069 }
0070 return true;
0071 }
0072 virtual bool s2value(const std::string& a_s) {
0073 std::vector<std::string> ws;
0074 words(a_s," ",false,ws);
0075 T& vec = parent::m_value;
0076 if(ws.size()!=vec.size()) return false;
0077 T old_vec = vec;
0078 for(size_t index=0;index<vec.size();index++) {
0079 std::istringstream strm(ws[index].c_str());
0080 TT v;
0081 strm >> v;
0082 if(strm.fail()) {
0083 vec = old_vec;
0084 return false;
0085 }
0086 if(vec[index]!=v) parent::m_touched = true;
0087 vec[index] = v;
0088 }
0089 return true;
0090 }
0091 public:
0092 sf_vec():parent(){}
0093 sf_vec(const T& a_value):parent(a_value){}
0094 virtual ~sf_vec(){}
0095 public:
0096 sf_vec(const sf_vec& a_from)
0097 :parent(a_from){}
0098 sf_vec& operator=(const sf_vec& a_from){
0099 parent::operator=(a_from);
0100 return *this;
0101 }
0102 public:
0103 sf_vec& operator=(const T& a_value){
0104 parent::operator=(a_value);
0105 return *this;
0106 }
0107 sf_vec& operator+=(const T& a_value) {
0108 parent::value(parent::value()+a_value);
0109 return *this;
0110 }
0111 };
0112
0113 }}
0114
0115 #endif