inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_params 00005 #define inlib_params 00006 00007 // used in OpenPAW to handle OPT and SET. 00008 // used in Lib/Session to handle parameters. 00009 00010 #include <list> 00011 #include <string> 00012 #include <vector> 00013 00014 namespace inlib { 00015 00016 template <class K,class V> 00017 class param { 00018 public: 00019 param( 00020 int a_ategory 00021 ,const K& a_key 00022 ,const std::string& a_label 00023 ,const V& a_value 00024 ,const V& a_default 00025 ,const V& a_alternative 00026 ) 00027 :m_category(a_ategory) 00028 ,m_key(a_key) 00029 ,m_label(a_label) 00030 ,m_value(a_value) 00031 ,m_default(a_default) 00032 ,m_alternative(a_alternative) 00033 ,m_touched(false){ 00034 } 00035 virtual ~param(){} 00036 public: 00037 param(const param& aFrom) 00038 :m_category(aFrom.m_category) 00039 ,m_key(aFrom.m_key) 00040 ,m_label(aFrom.m_label) 00041 ,m_value(aFrom.m_value) 00042 ,m_default(aFrom.m_default) 00043 ,m_alternative(aFrom.m_alternative) 00044 ,m_touched(false){ 00045 } 00046 param& operator =(const param& aFrom){ 00047 m_category = aFrom.m_category; 00048 m_key = aFrom.m_key; 00049 m_label = aFrom.m_label; 00050 m_value = aFrom.m_value; 00051 m_default = aFrom.m_default; 00052 m_alternative = aFrom.m_alternative; 00053 m_touched = false; 00054 return *this; 00055 } 00056 public: 00057 int category() const { return m_category;} 00058 bool touched() const { return m_touched;} 00059 const K& key() const { return m_key;} 00060 const std::string& label() const { return m_label;} 00061 const V& value() const { return m_value;} 00062 const V& default_value() const { return m_default;} 00063 const V& alternative() const { return m_alternative;} 00064 public: 00065 int m_category; 00066 K m_key; 00067 std::string m_label; 00068 V m_value; 00069 V m_default; 00070 V m_alternative; 00071 bool m_touched; 00072 }; 00073 00074 00075 template <class K,class V> 00076 class params { 00077 public: 00078 typedef std::list< param<K,V> > list_t; 00079 typedef typename list_t::iterator it_t; 00080 typedef typename list_t::const_iterator const_it_t; 00081 public: 00082 params(const K& a_key_not_found,V a_value_not_found) 00083 :m_key_not_found(a_key_not_found),m_value_not_found(a_value_not_found){} 00084 virtual ~params(){} 00085 00086 params(const params& a_from) 00087 :m_key_not_found(a_from.m_key_not_found) 00088 ,m_value_not_found(a_from.m_value_not_found) 00089 ,m_list(a_from.m_list) 00090 {} 00091 00092 params& operator=(const params& a_from) { 00093 m_key_not_found = a_from.m_key_not_found; 00094 m_value_not_found = a_from.m_value_not_found; 00095 m_list = a_from.m_list; 00096 return *this; 00097 } 00098 public: 00099 void add(int a_ategory,const K& a_key,const std::string& a_label,const V& a_value,const V& a_default,const V& a_alternative) { 00100 m_list.push_back 00101 (param<K,V>(a_ategory,a_key,a_label,a_value,a_default,a_alternative)); 00102 } 00103 00104 void remove(const K& a_key) { 00105 it_t it; 00106 for(it=m_list.begin();it!=m_list.end();++it) { 00107 if(a_key==(*it).m_key) { 00108 //m_list.remove(*it); //Need a operator==(param,param) 00109 m_list.erase(it); 00110 return; 00111 } 00112 } 00113 } 00114 00115 void set_value(const K& a_key,V a_value) { 00116 it_t it; 00117 for(it=m_list.begin();it!=m_list.end();++it) { 00118 if(a_key==(*it).m_key) { 00119 (*it).m_value = a_value; 00120 (*it).m_touched = true; 00121 return; 00122 } 00123 } 00124 } 00125 00126 void set_all_to_default() { 00127 it_t it; 00128 for(it=m_list.begin();it!=m_list.end();++it) { 00129 (*it).m_value = (*it).m_default; 00130 (*it).m_touched = false; 00131 } 00132 } 00133 00134 void set_category_to_default(int a_ategory) { 00135 it_t it; 00136 for(it=m_list.begin();it!=m_list.end();++it) { 00137 if((*it).m_category==a_ategory) { 00138 (*it).m_value = (*it).m_default; 00139 } 00140 } 00141 } 00142 00143 void set_to_default(const K& a_key) { 00144 it_t it; 00145 for(it=m_list.begin();it!=m_list.end();++it) { 00146 if(a_key==(*it).m_key) { 00147 (*it).m_value = (*it).m_default; 00148 } 00149 } 00150 } 00151 00152 std::string label(const K& a_key) const { 00153 const_it_t it; 00154 for(it=m_list.begin();it!=m_list.end();++it) { 00155 if(a_key==(*it).m_key) { 00156 return (*it).m_label; 00157 } 00158 } 00159 return ""; 00160 } 00161 00162 bool category(const K& a_key,int& a_ategory) const { 00163 const_it_t it; 00164 for(it=m_list.begin();it!=m_list.end();++it) { 00165 if(a_key==(*it).m_key) { 00166 a_ategory = (*it).m_category; 00167 return true; 00168 } 00169 } 00170 return false; 00171 } 00172 00173 bool touched(const K& a_key,bool& a_touched) const { 00174 const_it_t it; 00175 for(it=m_list.begin();it!=m_list.end();++it) { 00176 if(a_key==(*it).m_key) { 00177 a_touched = (*it).m_touched; 00178 return true; 00179 } 00180 } 00181 a_touched = false; 00182 return false; 00183 } 00184 00185 V value(const K& a_key) const { 00186 const_it_t it; 00187 for(it=m_list.begin();it!=m_list.end();++it) { 00188 if(a_key==(*it).m_key) { 00189 return (*it).m_value; 00190 } 00191 } 00192 return m_value_not_found; 00193 } 00194 00195 V default_value(const K& a_key) const { 00196 const_it_t it; 00197 for(it=m_list.begin();it!=m_list.end();++it) { 00198 if(a_key==(*it).m_key) { 00199 return (*it).m_default; 00200 } 00201 } 00202 return m_value_not_found; 00203 } 00204 00205 const K& key(V a_value) const { 00206 const_it_t it; 00207 for(it=m_list.begin();it!=m_list.end();++it) { 00208 if(a_value==(*it).m_value) { //Need operator==(V,V) 00209 return (*it).m_key; 00210 } 00211 } 00212 return m_key_not_found; 00213 } 00214 00215 const K& key_boolean(V a_value) const { 00216 const_it_t it; 00217 for(it=m_list.begin();it!=m_list.end();++it) { 00218 if( (a_value==(*it).m_default) || 00219 (a_value==(*it).m_alternative) ) { //Need operator==(V,V) 00220 return (*it).m_key; 00221 } 00222 } 00223 return m_key_not_found; 00224 } 00225 00226 bool is_key(const K& a_key) const { 00227 const_it_t it; 00228 for(it=m_list.begin();it!=m_list.end();++it) { 00229 if(a_key==(*it).m_key) { 00230 return true; 00231 } 00232 } 00233 return false; 00234 } 00235 00236 const list_t& list() const { return m_list;} 00237 00238 const K& key_not_found() const { return m_key_not_found;} 00239 00240 V value_not_found() const { return m_value_not_found;} 00241 00242 std::vector<K> keys() const { 00243 std::vector<K> ks; 00244 const_it_t it; 00245 for(it=m_list.begin();it!=m_list.end();++it) { 00246 ks.push_back((*it).m_key); 00247 } 00248 return ks; 00249 } 00250 00251 protected: 00252 K m_key_not_found; 00253 V m_value_not_found; 00254 list_t m_list; 00255 }; 00256 00257 } 00258 00259 #endif 00260 00261 00262