OgreStringInterface.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __StringInterface_H__
  25. #define __StringInterface_H__
  26. #include "OgrePrerequisites.h"
  27. #include "OgreString.h"
  28. #include "OgreCommon.h"
  29. namespace Ogre {
  30. /** \addtogroup Core
  31. * @{
  32. */
  33. /** \addtogroup General
  34. * @{
  35. */
  36. /// List of parameter types available
  37. enum ParameterType
  38. {
  39. PT_BOOL,
  40. PT_REAL,
  41. PT_INT,
  42. PT_UNSIGNED_INT,
  43. PT_SHORT,
  44. PT_UNSIGNED_SHORT,
  45. PT_LONG,
  46. PT_UNSIGNED_LONG,
  47. PT_STRING,
  48. PT_VECTOR3,
  49. PT_MATRIX3,
  50. PT_MATRIX4,
  51. PT_QUATERNION,
  52. PT_COLOURVALUE
  53. };
  54. /// Definition of a parameter supported by a StringInterface class, for introspection
  55. class _OgreExport ParameterDef
  56. {
  57. public:
  58. String name;
  59. String description;
  60. ParameterType paramType;
  61. ParameterDef(const String& newName, const String& newDescription, ParameterType newType)
  62. : name(newName), description(newDescription), paramType(newType) {}
  63. };
  64. typedef vector<ParameterDef>::type ParameterList;
  65. /** Abstract class which is command object which gets/sets parameters.*/
  66. class _OgreExport ParamCommand
  67. {
  68. public:
  69. virtual String doGet(const void* target) const = 0;
  70. virtual void doSet(void* target, const String& val) = 0;
  71. virtual ~ParamCommand() { }
  72. };
  73. typedef map<String, ParamCommand* >::type ParamCommandMap;
  74. /** Class to hold a dictionary of parameters for a single class. */
  75. class _OgreExport ParamDictionary
  76. {
  77. friend class StringInterface;
  78. protected:
  79. /// Definitions of parameters
  80. ParameterList mParamDefs;
  81. /// Command objects to get/set
  82. ParamCommandMap mParamCommands;
  83. /** Retrieves the parameter command object for a named parameter. */
  84. ParamCommand* getParamCommand(const String& name)
  85. {
  86. ParamCommandMap::iterator i = mParamCommands.find(name);
  87. if (i != mParamCommands.end())
  88. {
  89. return i->second;
  90. }
  91. else
  92. {
  93. return 0;
  94. }
  95. }
  96. const ParamCommand* getParamCommand(const String& name) const
  97. {
  98. ParamCommandMap::const_iterator i = mParamCommands.find(name);
  99. if (i != mParamCommands.end())
  100. {
  101. return i->second;
  102. }
  103. else
  104. {
  105. return 0;
  106. }
  107. }
  108. public:
  109. ParamDictionary() {}
  110. /** Method for adding a parameter definition for this class.
  111. @param paramDef A ParameterDef object defining the parameter
  112. @param paramCmd Pointer to a ParamCommand subclass to handle the getting / setting of this parameter.
  113. NB this class will not destroy this on shutdown, please ensure you do
  114. */
  115. void addParameter(const ParameterDef& paramDef, ParamCommand* paramCmd)
  116. {
  117. mParamDefs.push_back(paramDef);
  118. mParamCommands[paramDef.name] = paramCmd;
  119. }
  120. /** Retrieves a list of parameters valid for this object.
  121. @returns
  122. A reference to a static list of ParameterDef objects.
  123. */
  124. const ParameterList& getParameters(void) const
  125. {
  126. return mParamDefs;
  127. }
  128. };
  129. typedef map<String, ParamDictionary>::type ParamDictionaryMap;
  130. /** Class defining the common interface which classes can use to
  131. present a reflection-style, self-defining parameter set to callers.
  132. @remarks
  133. This class also holds a static map of class name to parameter dictionaries
  134. for each subclass to use. See ParamDictionary for details.
  135. @remarks
  136. In order to use this class, each subclass must call createParamDictionary in their constructors
  137. which will create a parameter dictionary for the class if it does not exist yet.
  138. */
  139. class _OgreExport StringInterface
  140. {
  141. private:
  142. OGRE_STATIC_MUTEX( msDictionaryMutex )
  143. /// Dictionary of parameters
  144. static ParamDictionaryMap msDictionary;
  145. /// Class name for this instance to be used as a lookup (must be initialised by subclasses)
  146. String mParamDictName;
  147. ParamDictionary* mParamDict;
  148. protected:
  149. /** Internal method for creating a parameter dictionary for the class, if it does not already exist.
  150. @remarks
  151. This method will check to see if a parameter dictionary exist for this class yet,
  152. and if not will create one. NB you must supply the name of the class (RTTI is not
  153. used or performance).
  154. @param
  155. className the name of the class using the dictionary
  156. @returns
  157. true if a new dictionary was created, false if it was already there
  158. */
  159. bool createParamDictionary(const String& className)
  160. {
  161. OGRE_LOCK_MUTEX( msDictionaryMutex )
  162. ParamDictionaryMap::iterator it = msDictionary.find(className);
  163. if ( it == msDictionary.end() )
  164. {
  165. mParamDict = &msDictionary.insert( std::make_pair( className, ParamDictionary() ) ).first->second;
  166. mParamDictName = className;
  167. return true;
  168. }
  169. else
  170. {
  171. mParamDict = &it->second;
  172. mParamDictName = className;
  173. return false;
  174. }
  175. }
  176. public:
  177. StringInterface() : mParamDict(NULL) { }
  178. /** Virtual destructor, see Effective C++ */
  179. virtual ~StringInterface() {}
  180. /** Retrieves the parameter dictionary for this class.
  181. @remarks
  182. Only valid to call this after createParamDictionary.
  183. @returns
  184. Pointer to ParamDictionary shared by all instances of this class
  185. which you can add parameters to, retrieve parameters etc.
  186. */
  187. ParamDictionary* getParamDictionary(void)
  188. {
  189. return mParamDict;
  190. }
  191. const ParamDictionary* getParamDictionary(void) const
  192. {
  193. return mParamDict;
  194. }
  195. /** Retrieves a list of parameters valid for this object.
  196. @returns
  197. A reference to a static list of ParameterDef objects.
  198. */
  199. const ParameterList& getParameters(void) const;
  200. /** Generic parameter setting method.
  201. @remarks
  202. Call this method with the name of a parameter and a string version of the value
  203. to set. The implementor will convert the string to a native type internally.
  204. If in doubt, check the parameter definition in the list returned from
  205. StringInterface::getParameters.
  206. @param
  207. name The name of the parameter to set
  208. @param
  209. value String value. Must be in the right format for the type specified in the parameter definition.
  210. See the StringConverter class for more information.
  211. @returns
  212. true if set was successful, false otherwise (NB no exceptions thrown - tolerant method)
  213. */
  214. virtual bool setParameter(const String& name, const String& value);
  215. /** Generic multiple parameter setting method.
  216. @remarks
  217. Call this method with a list of name / value pairs
  218. to set. The implementor will convert the string to a native type internally.
  219. If in doubt, check the parameter definition in the list returned from
  220. StringInterface::getParameters.
  221. @param
  222. paramList Name/value pair list
  223. */
  224. virtual void setParameterList(const NameValuePairList& paramList);
  225. /** Generic parameter retrieval method.
  226. @remarks
  227. Call this method with the name of a parameter to retrieve a string-format value of
  228. the parameter in question. If in doubt, check the parameter definition in the
  229. list returned from getParameters for the type of this parameter. If you
  230. like you can use StringConverter to convert this string back into a native type.
  231. @param
  232. name The name of the parameter to get
  233. @returns
  234. String value of parameter, blank if not found
  235. */
  236. virtual String getParameter(const String& name) const
  237. {
  238. // Get dictionary
  239. const ParamDictionary* dict = getParamDictionary();
  240. if (dict)
  241. {
  242. // Look up command object
  243. const ParamCommand* cmd = dict->getParamCommand(name);
  244. if (cmd)
  245. {
  246. return cmd->doGet(this);
  247. }
  248. }
  249. // Fallback
  250. return "";
  251. }
  252. /** Method for copying this object's parameters to another object.
  253. @remarks
  254. This method takes the values of all the object's parameters and tries to set the
  255. same values on the destination object. This provides a completely type independent
  256. way to copy parameters to other objects. Note that because of the String manipulation
  257. involved, this should not be regarded as an efficient process and should be saved for
  258. times outside of the rendering loop.
  259. @par
  260. Any unrecognised parameters will be ignored as with setParameter method.
  261. @param dest Pointer to object to have it's parameters set the same as this object.
  262. */
  263. virtual void copyParametersTo(StringInterface* dest) const
  264. {
  265. // Get dictionary
  266. const ParamDictionary* dict = getParamDictionary();
  267. if (dict)
  268. {
  269. // Iterate through own parameters
  270. ParameterList::const_iterator i;
  271. for (i = dict->mParamDefs.begin();
  272. i != dict->mParamDefs.end(); ++i)
  273. {
  274. dest->setParameter(i->name, getParameter(i->name));
  275. }
  276. }
  277. }
  278. /** Cleans up the static 'msDictionary' required to reset Ogre,
  279. otherwise the containers are left with invalid pointers, which will lead to a crash
  280. as soon as one of the ResourceManager implementers (e.g. MaterialManager) initializes.*/
  281. static void cleanupDictionary () ;
  282. };
  283. /** @} */
  284. /** @} */
  285. }
  286. #endif