00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef ZEITGEIST_PARAMETERLIST_H
00023 #define ZEITGEIST_PARAMETERLIST_H
00024
00025 #include <boost/any.hpp>
00026 #include <vector>
00027 #include <string>
00028 #include <salt/vector.h>
00029
00030 namespace zeitgeist
00031 {
00032 class ParameterList;
00033
00038 class ParameterList
00039 {
00040 public:
00041 typedef std::vector<boost::any> TVector;
00042
00043 protected:
00044 TVector mList;
00045
00046 public:
00047 ParameterList();
00048 virtual ~ParameterList();
00049
00051 void AddValue(const boost::any& value);
00052
00058 ParameterList& AddList();
00059
00062 int GetSize() const;
00063
00065 bool IsEmpty() const;
00066
00068 void Clear();
00069
00071 void Pop_Front();
00072
00074 void Pop_Back();
00075
00078 TVector::const_iterator begin() const;
00079
00082 TVector::const_iterator end() const;
00083
00086 TVector::const_iterator operator[] (int n) const;
00087
00090 TVector::iterator operator[] (int n);
00091
00094 TVector::iterator begin();
00095
00098 TVector::iterator end();
00099
00122 template<typename T> bool
00123 AdvanceAnyValue(TVector::const_iterator& iter, T& value) const
00124 {
00125 return GetValueInternal<T,T>(iter,value);
00126 }
00127
00133 template<typename T> f_inline bool
00134 GetValue(const TVector::const_iterator& iter, T& value) const
00135 {
00136 TVector::const_iterator tmp = iter;
00137 return AdvanceValue(tmp,value);
00138 }
00139
00140 template<typename T> f_inline bool
00141 GetAnyValue(const TVector::const_iterator& iter, T& value) const
00142 {
00143 TVector::const_iterator tmp = iter;
00144 return AdvanceAnyValue(tmp,value);
00145 }
00146
00149 bool AdvanceValue(TVector::const_iterator& iter, std::string& value) const;
00150 bool AdvanceValue(TVector::const_iterator& iter, float& value) const;
00151 bool AdvanceValue(TVector::const_iterator& iter, double& value) const;
00152 bool AdvanceValue(TVector::const_iterator& iter, int& value) const;
00153 bool AdvanceValue(TVector::const_iterator& iter, bool& value) const;
00154 bool AdvanceValue(TVector::const_iterator& iter, unsigned int& value) const;
00155 bool AdvanceValue(TVector::const_iterator& iter, salt::Vector3f& value) const;
00156 bool AdvanceValue(TVector::const_iterator& iter, salt::Vector2f& value) const;
00157
00158 protected:
00179 template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline bool
00180 GetVectorValue(TVector::const_iterator& iter,
00181 salt::TVector<DATATYPE,ELEMENTS,TYPE>& value) const
00182 {
00183 typedef salt::TVector<DATATYPE,ELEMENTS,TYPE> Vector;
00184
00185
00186 if (GetValueInternal<Vector,Vector>(iter,value))
00187 {
00188 return true;
00189 }
00190
00191
00192
00193 TVector::const_iterator test = iter;
00194 Vector vec;
00195 int i=0;
00196
00197 while (
00198 (i<ELEMENTS) &&
00199 (test != mList.end())
00200 )
00201 {
00202 if (! AdvanceValue(test,vec[i]))
00203 {
00204 break;
00205 }
00206 ++i;
00207
00208
00209 }
00210
00211 if (i != ELEMENTS)
00212 {
00213
00214
00215 return false;
00216 }
00217
00218 value = vec;
00219 iter = test;
00220 return true;
00221 }
00222
00227 template<typename TYPE> f_inline bool
00228 ConvertStringValue(TVector::const_iterator& iter, TYPE& value) const
00229 {
00230 const boost::any& param = (*iter);
00231
00232 try
00233 {
00234 std::string str;
00235
00236 if (param.type() == typeid(std::string))
00237 {
00238 str = boost::any_cast<std::string>(param);
00239 } else if (param.type() == typeid(char*))
00240 {
00241 str = boost::any_cast<char*>(param);
00242 } else
00243 {
00244 return false;
00245 }
00246
00247 value = static_cast<TYPE>(atof(str.c_str()));
00248 ++iter;
00249 }
00250
00251 catch(const std::bad_cast&)
00252 {
00253 return false;
00254 }
00255
00256 return true;
00257 }
00258
00262 template<typename TFrom, typename TTo> f_inline bool
00263 GetValueInternal(TVector::const_iterator& iter, TTo& value) const
00264 {
00265 const boost::any& param = (*iter);
00266
00267 if (param.type() != typeid(TFrom))
00268 {
00269 return false;
00270 }
00271
00272 try
00273 {
00274 value = static_cast<TTo>(boost::any_cast<TFrom>(*iter));
00275 ++iter;
00276 return true;
00277 }
00278
00279 catch(const std::bad_cast&)
00280 {
00281 return false;
00282 }
00283 }
00284 };
00285
00286 }
00287 #endif // ZEITGEIST_PARAMETERLIST_H