Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

OgreStringInterface.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2005 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 
00026 #ifndef __StringInterface_H__
00027 #define __StringInterface_H__
00028 
00029 #include "OgrePrerequisites.h"
00030 #include "OgreString.h"
00031 #include "OgreCommon.h"
00032 
00033 namespace Ogre {
00034 
00035 
00037     enum ParameterType
00038     {
00039         PT_BOOL,
00040         PT_REAL,
00041         PT_INT,
00042         PT_UNSIGNED_INT,
00043         PT_SHORT,
00044         PT_UNSIGNED_SHORT,
00045         PT_LONG,
00046         PT_UNSIGNED_LONG,
00047         PT_STRING,
00048         PT_VECTOR3,
00049         PT_MATRIX3,
00050         PT_MATRIX4,
00051         PT_QUATERNION,
00052         PT_COLOURVALUE
00053     };
00054 
00056     class _OgreExport ParameterDef
00057     {
00058     public:
00059         String name;
00060         String description;
00061         ParameterType paramType;
00062         ParameterDef(const String& newName, const String& newDescription, ParameterType newType)
00063             : name(newName), description(newDescription), paramType(newType) {}
00064     };
00065     typedef std::vector<ParameterDef> ParameterList;
00066 
00068     class _OgreExport ParamCommand
00069     {
00070     public:
00071         virtual String doGet(const void* target) const = 0;
00072         virtual void doSet(void* target, const String& val) = 0;
00073     };
00074     typedef std::map<String, ParamCommand* > ParamCommandMap;
00075 
00077     class _OgreExport ParamDictionary
00078     {
00079         friend class StringInterface;
00080     protected:
00082         ParameterList mParamDefs;
00083 
00085         ParamCommandMap mParamCommands;
00086 
00088         ParamCommand* getParamCommand(const String& name)
00089         {
00090             ParamCommandMap::iterator i = mParamCommands.find(name);
00091             if (i != mParamCommands.end())
00092             {
00093                 return i->second;
00094             }
00095             else
00096             {
00097                 return 0;
00098             }
00099         }
00100 
00101         const ParamCommand* getParamCommand(const String& name) const
00102         {
00103             ParamCommandMap::const_iterator i = mParamCommands.find(name);
00104             if (i != mParamCommands.end())
00105             {
00106                 return i->second;
00107             }
00108             else
00109             {
00110                 return 0;
00111             }
00112         }
00113     public:
00114         ParamDictionary()  {}
00121         void addParameter(const ParameterDef& paramDef, ParamCommand* paramCmd)
00122         {
00123             mParamDefs.push_back(paramDef);
00124             mParamCommands[paramDef.name] = paramCmd;
00125         }
00131         const ParameterList& getParameters(void) const
00132         {
00133             return mParamDefs;
00134         }
00135 
00136 
00137 
00138     };
00139     typedef std::map<String, ParamDictionary> ParamDictionaryMap;
00140     
00150     class _OgreExport StringInterface 
00151     {
00152     protected:
00153 
00155         static ParamDictionaryMap msDictionary;
00156 
00158         String mParamDictName;
00159 
00170         bool createParamDictionary(const String& className)
00171         {
00172             mParamDictName = className;
00173             if (msDictionary.find(className) == msDictionary.end())
00174             {
00175                 msDictionary[className] = ParamDictionary();
00176                 return true;
00177             }
00178             return false;
00179 
00180         }
00181 
00182     public:
00183 
00185         virtual ~StringInterface() {}
00186 
00194         ParamDictionary* getParamDictionary(void)
00195         {
00196             ParamDictionaryMap::iterator i = msDictionary.find(mParamDictName);
00197             if (i != msDictionary.end())
00198             {
00199                 return &(i->second);
00200             }
00201             else
00202             {
00203                 return 0;
00204             }
00205         }
00206 
00207         const ParamDictionary* getParamDictionary(void) const
00208         {
00209             ParamDictionaryMap::const_iterator i = msDictionary.find(mParamDictName);
00210             if (i != msDictionary.end())
00211             {
00212                 return &(i->second);
00213             }
00214             else
00215             {
00216                 return 0;
00217             }
00218         }
00219 
00225         const ParameterList& getParameters(void) const;
00226 
00241         virtual bool setParameter(const String& name, const String& value);
00251         virtual void setParameterList(const NameValuePairList& paramList);
00263         virtual String getParameter(const String& name) const
00264         {
00265             // Get dictionary
00266             const ParamDictionary* dict = getParamDictionary();
00267 
00268             if (dict)
00269             {
00270                 // Look up command object
00271                 const ParamCommand* cmd = dict->getParamCommand(name);
00272 
00273                 if (cmd)
00274                 {
00275                     return cmd->doGet(this);
00276                 }
00277             }
00278 
00279             // Fallback
00280             return "";
00281         }
00294         virtual void copyParametersTo(StringInterface* dest) const
00295         {
00296             // Get dictionary
00297             const ParamDictionary* dict = getParamDictionary();
00298 
00299             if (dict)
00300             {
00301                 // Iterate through own parameters
00302                 ParameterList::const_iterator i;
00303             
00304                 for (i = dict->mParamDefs.begin(); 
00305                 i != dict->mParamDefs.end(); ++i)
00306                 {
00307                     dest->setParameter(i->name, getParameter(i->name));
00308                 }
00309             }
00310 
00311 
00312         }
00313 
00317         static void cleanupDictionary () ;
00318 
00319     };
00320 
00321 
00322 
00323 }
00324 
00325 #endif
00326 

Copyright © 2000-2005 by The OGRE Team
Last modified Wed Feb 23 00:19:14 2005