Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

parameterlist.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2004 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: parameterlist.h,v 1.5 2004/12/19 14:06:42 rollmark Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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         // try to cast to Vector from a single value
00186         if (GetValueInternal<Vector,Vector>(iter,value))
00187             {
00188                 return true;
00189             }
00190 
00191         // a direct cast faild. try to construct a vector from
00192         // three consecutive values
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                 // iterator test is incremented within the
00208                 // call to GetValue()
00209             }
00210 
00211         if (i != ELEMENTS)
00212             {
00213                 // there were not enough components to build
00214                 // the vector
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

Generated on Thu Apr 6 15:25:39 2006 for rcssserver3d by  doxygen 1.4.4