00001 /*!@file Component/ModelParamBase.H A tunable ModelComponent parameter base class */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Component/ModelParamBase.H $ 00035 // $Id: ModelParamBase.H 14376 2011-01-11 02:44:34Z pez $ 00036 // 00037 00038 #ifndef MODELPARAMBASE_H_DEFINED 00039 #define MODELPARAMBASE_H_DEFINED 00040 00041 #include "Component/ParamFlags.H" 00042 00043 #include <string> 00044 #include <typeinfo> 00045 #include <cstddef> 00046 00047 class ModelOptionDef; 00048 class ParamMap; 00049 00050 //! Base class for TRefHolder 00051 class RefHolder 00052 { 00053 public: 00054 RefHolder(); 00055 virtual ~RefHolder(); 00056 00057 virtual const std::type_info& type() const = 0; 00058 00059 private: 00060 RefHolder(const RefHolder&); // not implemented 00061 RefHolder& operator=(const RefHolder&); // not implemented 00062 }; 00063 00064 //! Template implementation of RefHolder 00065 /*! Essentially this class just provides a way to dynamic_cast to any 00066 type. We do this by wrapping the type in a TRefHolder, which is 00067 dynamic_cast-able by virtue of having a virtual function. Thus we 00068 can construct for example a TRefHolder<int>, pass it somewhere as 00069 a RefHolder, and then somewhen can later dynamic_cast it back to a 00070 TRefHolder<int> and get the original int& back out. */ 00071 template <class T> 00072 class TRefHolder : public RefHolder 00073 { 00074 public: 00075 TRefHolder(T& r) : ref(r) {} 00076 virtual ~TRefHolder() {} 00077 00078 virtual const std::type_info& type() const { return typeid(T); } 00079 00080 T& ref; 00081 }; 00082 00083 // ###################################################################### 00084 //! ModelParamBase is a persistent ModelComponent parameter base class 00085 /*! Various model parameter types can be derived from this base, see 00086 the NModelParam and OModelParam template class 00087 definitions. ModelParamBase is persistent in the sense that it can 00088 be saved/loaded into a ParamMap, which itself provides disk 00089 save/load capabilities. ModelParamBase is intended to be used as a 00090 data member in a ModelComponent (or derived) 00091 object. ModelComponent provides an interface by which 00092 ModelParamBase derivatives may also be tuned via command-line 00093 options. */ 00094 class ModelParamBase 00095 { 00096 public: 00097 // ###################################################################### 00098 /*! @name Constructors and Destructors */ 00099 //@{ 00100 00101 //! Constructor 00102 /*! @param flags Will be checked for ALLOW_ONLINE_CHANGES to set 00103 up our return value from allowsOnlineChanges() appropriately. */ 00104 ModelParamBase(const ParamFlag flags); 00105 00106 //! Destructor 00107 virtual ~ModelParamBase(); 00108 00109 //@} 00110 00111 // ###################################################################### 00112 /*! @name Basic access functions */ 00113 //@{ 00114 00115 //! get the parameter's name 00116 virtual std::string getName() const = 0; 00117 00118 //! get the parameter's name with spaces separating words 00119 /*! This function decodes the CamelCase string that is typically 00120 returned by getName() into space-separated words, using some 00121 heuristics to figure out where to place word boundaries. It is 00122 not guaranteed to be perfectly accurate in finding word 00123 boundaries; this result is only intended for user-interface 00124 display to the end user and should not be relied on for 00125 programmatic usage. */ 00126 virtual std::string getNameWithSpaces() const; 00127 00128 //! Get current value as a string (must be implemented by derived classes) 00129 virtual std::string getValString() const = 0; 00130 00131 //! Set the parameter value from a textual representation, notify client of the change 00132 /*! @return true if the change succeeded; false otherwise. */ 00133 virtual bool setValString(const std::string& textval) = 0; 00134 00135 //! Get the current value through a dynamically-typed RefHolder. 00136 virtual void getValGeneric(RefHolder& ref) const = 0; 00137 00138 //! Set the current value through a dynamically-typed RefHolder, notify client of the change 00139 /*! @return true if the change succeeded; false otherwise. */ 00140 virtual bool setValGeneric(const RefHolder& ref) = 0; 00141 00142 //! Check whether this param is allowed to have its value changed while the model is active 00143 bool allowsOnlineChanges() const { return itsFlags & ALLOW_ONLINE_CHANGES; } 00144 00145 //! Change the param's active/inactive setting 00146 void setInactive(bool v) { if (v) itsFlags |= MP_INACTIVE; else itsFlags &= ~MP_INACTIVE; } 00147 00148 //! Check the param's active/inactive setting 00149 bool getInactive() const { return itsFlags & MP_INACTIVE; } 00150 00151 //! Change the param's hidden/visible setting 00152 void setHidden(bool v) { if (v) itsFlags |= MP_HIDDEN; else itsFlags &= ~MP_HIDDEN; } 00153 00154 //! Check the param's hidden/visible setting 00155 bool getHidden() const { return itsFlags & MP_HIDDEN; } 00156 00157 //@} 00158 00159 // ###################################################################### 00160 /*! @name Input/Output functions */ 00161 //@{ 00162 00163 //! Print out our name and contents, mostly for debugging 00164 virtual void printout(std::ostream& s, 00165 const std::string& prefix = "") const = 0; 00166 00167 //! Write parameter value to ParamMap 00168 virtual void writeTo(ParamMap& pmap) const = 0; 00169 00170 //! Get parameter value from ParamMap, notify client of the value change 00171 /*! @param noerr will not generate an error message if the parameter 00172 does not exist in ParamMap. */ 00173 virtual void readFrom(const ParamMap& pmap, 00174 const bool noerr = true) = 0; 00175 00176 //@} 00177 00178 private: 00179 ModelParamBase(const ModelParamBase& m); //!< forbid copy-contruction 00180 ModelParamBase& operator=(const ModelParamBase& m); //!< forbid copy 00181 00182 ParamFlag itsFlags; 00183 }; 00184 00185 // ###################################################################### 00186 //! OptionedModelParam extends ModelParamBase for params associated with a ModelOptionDef 00187 class OptionedModelParam : public ModelParamBase 00188 { 00189 public: 00190 OptionedModelParam(const ParamFlag flags); 00191 virtual ~OptionedModelParam(); 00192 00193 virtual const ModelOptionDef* getOptionDef() const = 0; 00194 }; 00195 00196 #endif // !MODELPARAMBASE_H_DEFINED 00197 00198 // ###################################################################### 00199 /* So things look consistent in everyone's emacs... */ 00200 /* Local Variables: */ 00201 /* indent-tabs-mode: nil */ 00202 /* End: */