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         virtual ~ParamCommand() { }
00075     };
00076     typedef std::map<String, ParamCommand* > ParamCommandMap;
00077 
00079     class _OgreExport ParamDictionary
00080     {
00081         friend class StringInterface;
00082     protected:
00084         ParameterList mParamDefs;
00085 
00087         ParamCommandMap mParamCommands;
00088 
00090         ParamCommand* getParamCommand(const String& name)
00091         {
00092             ParamCommandMap::iterator i = mParamCommands.find(name);
00093             if (i != mParamCommands.end())
00094             {
00095                 return i->second;
00096             }
00097             else
00098             {
00099                 return 0;
00100             }
00101         }
00102 
00103         const ParamCommand* getParamCommand(const String& name) const
00104         {
00105             ParamCommandMap::const_iterator i = mParamCommands.find(name);
00106             if (i != mParamCommands.end())
00107             {
00108                 return i->second;
00109             }
00110             else
00111             {
00112                 return 0;
00113             }
00114         }
00115     public:
00116         ParamDictionary()  {}
00123         void addParameter(const ParameterDef& paramDef, ParamCommand* paramCmd)
00124         {
00125             mParamDefs.push_back(paramDef);
00126             mParamCommands[paramDef.name] = paramCmd;
00127         }
00133         const ParameterList& getParameters(void) const
00134         {
00135             return mParamDefs;
00136         }
00137 
00138 
00139 
00140     };
00141     typedef std::map<String, ParamDictionary> ParamDictionaryMap;
00142     
00152     class _OgreExport StringInterface 
00153     {
00154     protected:
00155 
00157         static ParamDictionaryMap msDictionary;
00158 
00160         String mParamDictName;
00161 
00172         bool createParamDictionary(const String& className)
00173         {
00174             mParamDictName = className;
00175             if (msDictionary.find(className) == msDictionary.end())
00176             {
00177                 msDictionary[className] = ParamDictionary();
00178                 return true;
00179             }
00180             return false;
00181 
00182         }
00183 
00184     public:
00185 
00187         virtual ~StringInterface() {}
00188 
00196         ParamDictionary* getParamDictionary(void)
00197         {
00198             ParamDictionaryMap::iterator i = msDictionary.find(mParamDictName);
00199             if (i != msDictionary.end())
00200             {
00201                 return &(i->second);
00202             }
00203             else
00204             {
00205                 return 0;
00206             }
00207         }
00208 
00209         const ParamDictionary* getParamDictionary(void) const
00210         {
00211             ParamDictionaryMap::const_iterator i = msDictionary.find(mParamDictName);
00212             if (i != msDictionary.end())
00213             {
00214                 return &(i->second);
00215             }
00216             else
00217             {
00218                 return 0;
00219             }
00220         }
00221 
00227         const ParameterList& getParameters(void) const;
00228 
00243         virtual bool setParameter(const String& name, const String& value);
00253         virtual void setParameterList(const NameValuePairList& paramList);
00265         virtual String getParameter(const String& name) const
00266         {
00267             // Get dictionary
00268             const ParamDictionary* dict = getParamDictionary();
00269 
00270             if (dict)
00271             {
00272                 // Look up command object
00273                 const ParamCommand* cmd = dict->getParamCommand(name);
00274 
00275                 if (cmd)
00276                 {
00277                     return cmd->doGet(this);
00278                 }
00279             }
00280 
00281             // Fallback
00282             return "";
00283         }
00296         virtual void copyParametersTo(StringInterface* dest) const
00297         {
00298             // Get dictionary
00299             const ParamDictionary* dict = getParamDictionary();
00300 
00301             if (dict)
00302             {
00303                 // Iterate through own parameters
00304                 ParameterList::const_iterator i;
00305             
00306                 for (i = dict->mParamDefs.begin(); 
00307                 i != dict->mParamDefs.end(); ++i)
00308                 {
00309                     dest->setParameter(i->name, getParameter(i->name));
00310                 }
00311             }
00312 
00313 
00314         }
00315 
00319         static void cleanupDictionary () ;
00320 
00321     };
00322 
00323 
00324 
00325 }
00326 
00327 #endif
00328 

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Mar 12 14:37:50 2006