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-2006 Torus Knot Software Ltd 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 You may alternatively use this source under the terms of a specific version of 00025 the OGRE Unrestricted License provided you have obtained such a license from 00026 Torus Knot Software Ltd. 00027 ----------------------------------------------------------------------------- 00028 */ 00029 00030 #ifndef __StringInterface_H__ 00031 #define __StringInterface_H__ 00032 00033 #include "OgrePrerequisites.h" 00034 #include "OgreString.h" 00035 #include "OgreCommon.h" 00036 00037 namespace Ogre { 00038 00039 00041 enum ParameterType 00042 { 00043 PT_BOOL, 00044 PT_REAL, 00045 PT_INT, 00046 PT_UNSIGNED_INT, 00047 PT_SHORT, 00048 PT_UNSIGNED_SHORT, 00049 PT_LONG, 00050 PT_UNSIGNED_LONG, 00051 PT_STRING, 00052 PT_VECTOR3, 00053 PT_MATRIX3, 00054 PT_MATRIX4, 00055 PT_QUATERNION, 00056 PT_COLOURVALUE 00057 }; 00058 00060 class _OgreExport ParameterDef 00061 { 00062 public: 00063 String name; 00064 String description; 00065 ParameterType paramType; 00066 ParameterDef(const String& newName, const String& newDescription, ParameterType newType) 00067 : name(newName), description(newDescription), paramType(newType) {} 00068 }; 00069 typedef std::vector<ParameterDef> ParameterList; 00070 00072 class _OgreExport ParamCommand 00073 { 00074 public: 00075 virtual String doGet(const void* target) const = 0; 00076 virtual void doSet(void* target, const String& val) = 0; 00077 00078 virtual ~ParamCommand() { } 00079 }; 00080 typedef std::map<String, ParamCommand* > ParamCommandMap; 00081 00083 class _OgreExport ParamDictionary 00084 { 00085 friend class StringInterface; 00086 protected: 00088 ParameterList mParamDefs; 00089 00091 ParamCommandMap mParamCommands; 00092 00094 ParamCommand* getParamCommand(const String& name) 00095 { 00096 ParamCommandMap::iterator i = mParamCommands.find(name); 00097 if (i != mParamCommands.end()) 00098 { 00099 return i->second; 00100 } 00101 else 00102 { 00103 return 0; 00104 } 00105 } 00106 00107 const ParamCommand* getParamCommand(const String& name) const 00108 { 00109 ParamCommandMap::const_iterator i = mParamCommands.find(name); 00110 if (i != mParamCommands.end()) 00111 { 00112 return i->second; 00113 } 00114 else 00115 { 00116 return 0; 00117 } 00118 } 00119 public: 00120 ParamDictionary() {} 00127 void addParameter(const ParameterDef& paramDef, ParamCommand* paramCmd) 00128 { 00129 mParamDefs.push_back(paramDef); 00130 mParamCommands[paramDef.name] = paramCmd; 00131 } 00137 const ParameterList& getParameters(void) const 00138 { 00139 return mParamDefs; 00140 } 00141 00142 00143 00144 }; 00145 typedef std::map<String, ParamDictionary> ParamDictionaryMap; 00146 00156 class _OgreExport StringInterface 00157 { 00158 private: 00159 OGRE_STATIC_MUTEX( msDictionaryMutex ); 00160 00162 static ParamDictionaryMap msDictionary; 00163 00165 String mParamDictName; 00166 ParamDictionary* mParamDict; 00167 00168 protected: 00179 bool createParamDictionary(const String& className) 00180 { 00181 OGRE_LOCK_MUTEX( msDictionaryMutex ) 00182 00183 ParamDictionaryMap::iterator it = msDictionary.find(className); 00184 00185 if ( it == msDictionary.end() ) 00186 { 00187 mParamDict = &msDictionary.insert( std::make_pair( className, ParamDictionary() ) ).first->second; 00188 mParamDictName = className; 00189 return true; 00190 } 00191 else 00192 { 00193 mParamDict = &it->second; 00194 mParamDictName = className; 00195 return false; 00196 } 00197 } 00198 00199 public: 00200 StringInterface() : mParamDict(NULL) { } 00201 00203 virtual ~StringInterface() {} 00204 00212 ParamDictionary* getParamDictionary(void) 00213 { 00214 return mParamDict; 00215 } 00216 00217 const ParamDictionary* getParamDictionary(void) const 00218 { 00219 return mParamDict; 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 © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Sep 27 22:02:26 2009