Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/sg/field_desc 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_field_desc
0005 #define tools_sg_field_desc
0006 
0007 #include <string>
0008 #include <cstddef> //ptrdiff_t
0009 
0010 #include <vector>
0011 
0012 // fied_desc are used in static and then we do not master their destruction.
0013 //#ifdef TOOLS_MEM
0014 //#include "../mem"
0015 //#include "../S_STRING"
0016 //#endif
0017 
0018 namespace tools {
0019 namespace sg {
0020 
0021 class field_desc {
0022   //typedef int offset_t; //could be <0 ?
0023 //#ifdef TOOLS_MEM
0024 //  TOOLS_SCLASS(tools::sg::field_desc)
0025 //#endif
0026 public:
0027   typedef ptrdiff_t offset_t;
0028 public:
0029   field_desc():m_offset(0){ //touchy
0030 //#ifdef TOOLS_MEM
0031 //    mem::increment(s_class().c_str());
0032 //#endif
0033   }
0034   field_desc(const std::string& a_name,
0035              const std::string& a_class,
0036              offset_t a_offset,
0037              bool a_editable)
0038   :m_name(a_name)
0039   ,m_class(a_class)
0040   ,m_offset(a_offset)
0041   ,m_editable(a_editable)
0042   {
0043 //#ifdef TOOLS_MEM
0044 //    mem::increment(s_class().c_str());
0045 //#endif
0046   }
0047   virtual ~field_desc(){
0048 //#ifdef TOOLS_MEM
0049 //    mem::decrement(s_class().c_str());
0050 //#endif
0051   }
0052 public:
0053   field_desc(const field_desc& a_from)
0054   :m_name(a_from.m_name)
0055   ,m_class(a_from.m_class)
0056   ,m_offset(a_from.m_offset)
0057   ,m_editable(a_from.m_editable)
0058   ,m_enums(a_from.m_enums)
0059   ,m_opts(a_from.m_opts)
0060   {}
0061   field_desc& operator=(const field_desc& a_from){
0062     m_name = a_from.m_name;
0063     m_class = a_from.m_class;
0064     m_offset = a_from.m_offset;
0065     m_editable = a_from.m_editable;
0066     m_enums = a_from.m_enums;
0067     m_opts = a_from.m_opts;
0068     return *this;
0069   }
0070 public:
0071   const std::string& name() const {return m_name;}
0072   const std::string& cls() const {return m_class;}
0073   offset_t offset() const {return m_offset;}
0074 
0075   void add_enum(const std::string& a_key,int a_value) {m_enums.push_back(enum_t(a_key,a_value));}
0076   typedef std::pair<std::string,int> enum_t;
0077   const std::vector<enum_t>& enums() const {return m_enums;}
0078 
0079   void add_opt(const std::string& a_value) {m_opts.push_back(a_value);}
0080   const std::vector<std::string>& opts() const {return m_opts;}
0081 
0082   bool editable() const {return m_editable;}
0083 protected:
0084   std::string m_name;
0085   std::string m_class;
0086   offset_t m_offset;
0087   bool m_editable;
0088   std::vector<enum_t> m_enums;
0089   std::vector<std::string> m_opts;
0090 };
0091 
0092 }}
0093 
0094 #include <cstdarg>
0095 
0096 namespace tools {
0097 namespace sg {
0098 
0099 class field_desc_enums : public field_desc {
0100   typedef field_desc parent;
0101 public:
0102   field_desc_enums(const std::string& a_name,const std::string& a_class,offset_t a_offset,bool a_editable,size_t a_num,...)
0103   :parent(a_name,a_class,a_offset,a_editable)
0104   {
0105     va_list args;
0106     va_start(args,a_num);
0107     for(size_t index=0;index<a_num;index++) {
0108       char* _key = va_arg(args,char*);
0109       int _value = va_arg(args,int);
0110       m_enums.push_back(enum_t(_key,_value));
0111     }
0112     va_end(args);
0113   }
0114   virtual ~field_desc_enums() {}
0115 public:
0116   field_desc_enums(const field_desc_enums& a_from):parent(a_from) {}
0117   field_desc_enums& operator=(const field_desc_enums& a_from){parent::operator=(a_from);return *this;}
0118 };
0119 
0120 class field_desc_opts : public field_desc {
0121   typedef field_desc parent;
0122 public:
0123   field_desc_opts(const std::string& a_name,const std::string& a_class,offset_t a_offset,bool a_editable,size_t a_num,...)
0124   :parent(a_name,a_class,a_offset,a_editable)
0125   {
0126     va_list args;
0127     va_start(args,a_num);
0128     for(size_t index=0;index<a_num;index++) {
0129       char* _value = va_arg(args,char*);
0130       m_opts.push_back(_value);
0131     }
0132     va_end(args);
0133   }
0134   virtual ~field_desc_opts() {}
0135 public:
0136   field_desc_opts(const field_desc_opts& a_from):parent(a_from) {}
0137   field_desc_opts& operator=(const field_desc_opts& a_from){parent::operator=(a_from);return *this;}
0138 };
0139 
0140 }}
0141 
0142 #include <ostream>
0143 #include "../forit"
0144 
0145 namespace tools {
0146 namespace sg {
0147 
0148 class desc_fields : public std::vector<field_desc> {
0149   typedef std::vector<field_desc> parent;
0150 public:
0151   desc_fields(){}
0152   desc_fields(const desc_fields& a_parent,size_t a_num,...){
0153     parent::operator=(a_parent);
0154     va_list args;
0155     va_start(args,a_num);
0156     for(size_t index=0;index<a_num;index++) {
0157       field_desc* _fd = va_arg(args,field_desc*); //we get ownership.
0158       parent::push_back(*_fd);
0159       delete _fd;
0160     }
0161     va_end(args);
0162   }
0163   virtual ~desc_fields() {}
0164 public:
0165   desc_fields(const desc_fields& a_from):parent(a_from) {}
0166   desc_fields& operator=(const desc_fields& a_from){parent::operator=(a_from);return *this;}
0167 public:
0168   void dump(std::ostream& a_out) const {
0169     a_out << "num fields " << parent::size() << " :" << std::endl;
0170     tools_vforcit(field_desc,*this,it) {
0171       const field_desc& _fd = *it;
0172       a_out << "name " << _fd.name() << std::endl;
0173       a_out << "class " << _fd.cls() << std::endl;
0174       a_out << "offset " << _fd.offset() << std::endl;
0175       a_out << "editable " << (_fd.editable()?"yes":"no") << std::endl;
0176      {const std::vector<field_desc::enum_t>& _enums = _fd.enums();
0177       if(_enums.size()) {
0178         a_out << "num enums " << _enums.size() << " :" << std::endl;
0179         tools_vforcit(field_desc::enum_t,_enums,eit) {
0180           a_out << "key " << (*eit).first << ", value " << (*eit).second << std::endl;
0181         }
0182       }}
0183      {const std::vector<std::string>& _opts = _fd.opts();
0184       if(_opts.size()) {
0185         a_out << "num options " << _opts.size() << " :" << std::endl;
0186         tools_vforcit(std::string,_opts,oit) {
0187           a_out << " " << (*oit) << std::endl;
0188         }
0189       }}
0190     }
0191   }
0192 };
0193 
0194 }}
0195 
0196 #endif