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-2012 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028
    00029 #ifndef __StringInterface_H__
    00030 #define __StringInterface_H__
    00031
    00032 #include "OgrePrerequisites.h"
    00033 #include "OgreString.h"
    00034 #include "OgreCommon.h"
    00035
    00036 namespace Ogre {
    00037
    00045
    00046     enum ParameterType
    00047     {
    00048         PT_BOOL,
    00049         PT_REAL,
    00050         PT_INT,
    00051         PT_UNSIGNED_INT,
    00052         PT_SHORT,
    00053         PT_UNSIGNED_SHORT,
    00054         PT_LONG,
    00055         PT_UNSIGNED_LONG,
    00056         PT_STRING,
    00057         PT_VECTOR3,
    00058         PT_MATRIX3,
    00059         PT_MATRIX4,
    00060         PT_QUATERNION,
    00061         PT_COLOURVALUE
    00062     };
    00063
    00065     class _OgreExport ParameterDef
    00066     {
    00067     public:
    00068         String name;
    00069         String description;
    00070         ParameterType paramType;
    00071         ParameterDef(const String& newName, const String& newDescription, ParameterType newType)
    00072             : name(newName), description(newDescription), paramType(newType) {}
    00073     };
    00074     typedef vector<ParameterDef>::type ParameterList;
    00075
    00077     class _OgreExport ParamCommand
    00078     {
    00079     public:
    00080         virtual String doGet(const void* target) const = 0;
    00081         virtual void doSet(void* target, const String& val) = 0;
    00082
    00083         virtual ~ParamCommand() { }
    00084     };
    00085     typedef map<String, ParamCommand* >::type ParamCommandMap;
    00086
    00088     class _OgreExport ParamDictionary
    00089     {
    00090         friend class StringInterface;
    00091     protected:
    00093         ParameterList mParamDefs;
    00094
    00096         ParamCommandMap mParamCommands;
    00097
    00099         ParamCommand* getParamCommand(const String& name)
    00100         {
    00101             ParamCommandMap::iterator i = mParamCommands.find(name);
    00102             if (i != mParamCommands.end())
    00103             {
    00104                 return i->second;
    00105             }
    00106             else
    00107             {
    00108                 return 0;
    00109             }
    00110         }
    00111
    00112         const ParamCommand* getParamCommand(const String& name) const
    00113         {
    00114             ParamCommandMap::const_iterator i = mParamCommands.find(name);
    00115             if (i != mParamCommands.end())
    00116             {
    00117                 return i->second;
    00118             }
    00119             else
    00120             {
    00121                 return 0;
    00122             }
    00123         }
    00124     public:
    00125         ParamDictionary()  {}
    00132         void addParameter(const ParameterDef& paramDef, ParamCommand* paramCmd)
    00133         {
    00134             mParamDefs.push_back(paramDef);
    00135             mParamCommands[paramDef.name] = paramCmd;
    00136         }
    00142         const ParameterList& getParameters(void) const
    00143         {
    00144             return mParamDefs;
    00145         }
    00146
    00147
    00148
    00149     };
    00150     typedef map<String, ParamDictionary>::type ParamDictionaryMap;
    00151
    00161     class _OgreExport StringInterface
    00162     {
    00163     private:
    00164         OGRE_STATIC_MUTEX( msDictionaryMutex )
    00165
    00166
    00167         static ParamDictionaryMap msDictionary;
    00168
    00170         String mParamDictName;
    00171         ParamDictionary* mParamDict;
    00172
    00173     protected:
    00184         bool createParamDictionary(const String& className)
    00185         {
    00186             OGRE_LOCK_MUTEX( msDictionaryMutex )
    00187
    00188             ParamDictionaryMap::iterator it = msDictionary.find(className);
    00189
    00190             if ( it == msDictionary.end() )
    00191             {
    00192                 mParamDict = &msDictionary.insert( std::make_pair( className, ParamDictionary() ) ).first->second;
    00193                 mParamDictName = className;
    00194                 return true;
    00195             }
    00196             else
    00197             {
    00198                 mParamDict = &it->second;
    00199                 mParamDictName = className;
    00200                 return false;
    00201             }
    00202         }
    00203
    00204     public:
    00205         StringInterface() : mParamDict(NULL) { }
    00206
    00208         virtual ~StringInterface() {}
    00209
    00217         ParamDictionary* getParamDictionary(void)
    00218         {
    00219             return mParamDict;
    00220         }
    00221
    00222         const ParamDictionary* getParamDictionary(void) const
    00223         {
    00224             return mParamDict;
    00225         }
    00226
    00232         const ParameterList& getParameters(void) const;
    00233
    00248         virtual bool setParameter(const String& name, const String& value);
    00258         virtual void setParameterList(const NameValuePairList& paramList);
    00270         virtual String getParameter(const String& name) const
    00271         {
    00272             // Get dictionary
    00273             const ParamDictionary* dict = getParamDictionary();
    00274
    00275             if (dict)
    00276             {
    00277                 // Look up command object
    00278                 const ParamCommand* cmd = dict->getParamCommand(name);
    00279
    00280                 if (cmd)
    00281                 {
    00282                     return cmd->doGet(this);
    00283                 }
    00284             }
    00285
    00286             // Fallback
    00287             return "";
    00288         }
    00301         virtual void copyParametersTo(StringInterface* dest) const
    00302         {
    00303             // Get dictionary
    00304             const ParamDictionary* dict = getParamDictionary();
    00305
    00306             if (dict)
    00307             {
    00308                 // Iterate through own parameters
    00309                 ParameterList::const_iterator i;
    00310
    00311                 for (i = dict->mParamDefs.begin();
    00312                 i != dict->mParamDefs.end(); ++i)
    00313                 {
    00314                     dest->setParameter(i->name, getParameter(i->name));
    00315                 }
    00316             }
    00317
    00318
    00319         }
    00320
    00324         static void cleanupDictionary () ;
    00325
    00326     };
    00327
    00332 }
    00333
    00334 #endif
    00335

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun Sep 2 2012 07:27:24