Common/Util/Params.h

Go to the documentation of this file.
00001 //-------------------------------------------------------------------
00004 //  Copyright (c) 2007 Celeritive Technologies, Inc.
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License, or (at your option) any later version.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019 //
00020 //-------------------------------------------------------------------
00021 
00022 #pragma once
00023 #ifndef VOLUMILL_PARAMS_H
00024 #define VOLUMILL_PARAMS_H
00025 
00026 #include <map>
00027 #include <boost/mpl/vector.hpp>
00028 #include <boost/variant.hpp>
00029 #include <Util/ParamIds.h>
00030 
00031 namespace util
00032 {
00033    namespace detail
00034    {
00035       typedef boost::mpl::vector<int, double, std::string> ParamTypes;
00036    }
00037 
00038    typedef boost::make_variant_over<detail::ParamTypes>::type ParamType;
00039 
00040    class Parameterized
00041    {
00042       friend class boost::serialization::access;
00043       template<class Archive>
00044       void serialize(Archive & ar, const unsigned int /*version*/)
00045       {
00046          ar & boost::serialization::make_nvp ("params", m_params);
00047       }
00048    public:
00049       virtual ~Parameterized() {}
00050       virtual void setParam (ParamId paramId, const ParamType& param)
00051       {
00052          m_params[paramId] = param;
00053       }
00055       template<class T>
00056       const T& getParam (ParamId paramId) const
00057       {
00058          ParamTable::const_iterator it = m_params.find (paramId);
00059          VALIDATE (it != m_params.end());
00060          return boost::get<T> (it->second);
00061       }
00063       template<class T>
00064       bool getParam (ParamId paramId, T* pT) const
00065       {
00066          ParamTable::const_iterator it = m_params.find (paramId);
00067          if (it != m_params.end())
00068          {
00069             *pT = boost::get<T> (it->second);
00070             return true;
00071          }
00072          return false;
00073       }
00074       virtual void deleteParam (ParamId paramId)
00075       {
00076          ParamTable::iterator it = m_params.find (paramId);
00077          if (it != m_params.end())
00078             m_params.erase (it);
00079       }
00080       void copyParamTable (const Parameterized& other)
00081       {
00082          for (std::map<ParamId, ParamType>::const_iterator it = other.m_params.begin();
00083             it != other.m_params.end(); ++it)
00084             m_params[it->first] = it->second;
00085       }
00086       size_t getNumParams () const
00087       {
00088          return m_params.size();
00089       }
00090    protected:
00091       typedef std::map<ParamId, ParamType> ParamTable;
00092       ParamTable m_params;
00093    };
00094 }
00095 
00096 #endif

Generated on Tue Jan 29 21:37:56 2008 for VoluMill Universal Client by  doxygen 1.4.6