Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound 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://ogre.sourceforge.net/
00006 
00007 Copyright © 2000-2002 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 
00032 namespace Ogre {
00033 
00034 
00036     enum ParameterType
00037     {
00038         PT_BOOL,
00039         PT_REAL,
00040         PT_INT,
00041         PT_UNSIGNED_INT,
00042         PT_SHORT,
00043         PT_UNSIGNED_SHORT,
00044         PT_LONG,
00045         PT_UNSIGNED_LONG,
00046         PT_STRING,
00047         PT_VECTOR3,
00048         PT_MATRIX3,
00049         PT_MATRIX4,
00050         PT_QUATERNION,
00051         PT_COLOURVALUE
00052     };
00053 
00055     class _OgreExport ParameterDef
00056     {
00057     public:
00058         String name;
00059         String description;
00060         ParameterType paramType;
00061         ParameterDef(const String& newName, const String& newDescription, ParameterType newType)
00062             : name(newName), description(newDescription), paramType(newType) {}
00063     };
00064     typedef std::vector<ParameterDef> ParameterList;
00065 
00067     class _OgreExport ParamCommand
00068     {
00069     public:
00070         virtual String doGet(void* target) = 0;
00071         virtual void doSet(void* target, const String& val) = 0;
00072     };
00073     typedef std::map<String, ParamCommand* > ParamCommandMap;
00074 
00076     class _OgreExport ParamDictionary
00077     {
00078         friend class StringInterface;
00079     protected:
00081         ParameterList mParamDefs;
00082 
00084         ParamCommandMap mParamCommands;
00085 
00087         ParamCommand* getParamCommand(const String& name)
00088         {
00089             ParamCommandMap::iterator i = mParamCommands.find(name);
00090             if (i != mParamCommands.end())
00091             {
00092                 return i->second;
00093             }
00094             else
00095             {
00096                 return 0;
00097             }
00098         }
00099     public:
00100         ParamDictionary()  {}
00107         void addParameter(const ParameterDef& paramDef, ParamCommand* paramCmd)
00108         {
00109             mParamDefs.push_back(paramDef);
00110             mParamCommands[paramDef.name] = paramCmd;
00111         }
00117         const ParameterList& getParameters(void)
00118         {
00119             return mParamDefs;
00120         }
00121 
00122 
00123 
00124     };
00125     typedef std::map<String, ParamDictionary> ParamDictionaryMap;
00126     
00136     class _OgreExport StringInterface 
00137     {
00138     protected:
00139 
00141         static ParamDictionaryMap msDictionary;
00142 
00144         String mParamDictName;
00145 
00156         bool createParamDictionary(const String& className)
00157         {
00158             mParamDictName = className;
00159             if (msDictionary.find(className) == msDictionary.end())
00160             {
00161                 msDictionary[className] = ParamDictionary();
00162                 return true;
00163             }
00164             return false;
00165 
00166         }
00167 
00168     public:
00169 
00171         virtual ~StringInterface() {}
00172 
00180         ParamDictionary* getParamDictionary(void)
00181         {
00182             ParamDictionaryMap::iterator i = msDictionary.find(mParamDictName);
00183             if (i != msDictionary.end())
00184             {
00185                 return &(i->second);
00186             }
00187             else
00188             {
00189                 return 0;
00190             }
00191         }
00192 
00198         const ParameterList& getParameters(void)
00199         {
00200             static ParameterList emptyList;
00201 
00202             ParamDictionary* dict = getParamDictionary();
00203             if (dict)
00204                 return dict->getParameters();
00205             else
00206                 return emptyList;
00207 
00208         };
00209 
00224         virtual bool setParameter(const String& name, const String& value);
00236         virtual String getParameter(const String& name)
00237         {
00238             // Get dictionary
00239             ParamDictionary* dict = getParamDictionary();
00240 
00241             if (dict)
00242             {
00243                 // Look up command object
00244                 ParamCommand* cmd = dict->getParamCommand(name);
00245 
00246                 if (cmd)
00247                 {
00248                     return cmd->doGet(this);
00249                 }
00250             }
00251 
00252             // Fallback
00253             return "";
00254         }
00267         virtual void copyParametersTo(StringInterface* dest)
00268         {
00269             // Get dictionary
00270             ParamDictionary* dict = getParamDictionary();
00271 
00272             if (dict)
00273             {
00274                 // Iterate through own parameters
00275                 ParameterList::iterator i;
00276             
00277                 for (i = dict->mParamDefs.begin(); 
00278                 i != dict->mParamDefs.end(); ++i)
00279                 {
00280                     dest->setParameter(i->name, getParameter(i->name));
00281                 }
00282             }
00283 
00284 
00285         }
00286             
00287 
00288     };
00289 
00290 
00291 
00292 }
00293 
00294 #endif
00295 

Copyright © 2002 by The OGRE Team