Caelum
TypeDescriptor.h
1 /*
2 This file is part of Caelum.
3 See http://www.ogre3d.org/wiki/index.php/Caelum
4 
5 Copyright (c) 2008 Caelum team. See Contributors.txt for details.
6 
7 Caelum is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Caelum is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with Caelum. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef CAELUM__TYPE_DESCRIPTOR_H
22 #define CAELUM__TYPE_DESCRIPTOR_H
23 
24 #include "CaelumPrerequisites.h"
25 
26 #if CAELUM_TYPE_DESCRIPTORS
27 
28 #include <typeinfo>
29 
30 namespace Caelum
31 {
32  class ValuePropertyDescriptor;
33 
48  class CAELUM_EXPORT TypeDescriptor
49  {
50  public:
51  virtual ~TypeDescriptor() {};
52 
53  typedef std::map<String, const ValuePropertyDescriptor*> PropertyMap;
54 
59  virtual const ValuePropertyDescriptor* getPropertyDescriptor (const Ogre::String& name) const = 0;
60 
64  virtual const std::vector<String> getPropertyNames () const = 0;
65 
69  virtual const PropertyMap getFullPropertyMap () const = 0;
70  };
71 
83  class CAELUM_EXPORT ValuePropertyDescriptor
84  {
85  public:
86  virtual ~ValuePropertyDescriptor() {};
87 
94  virtual bool canGetValue () const = 0;
95 
97  virtual bool canSetValue () const = 0;
98 
104  virtual const Ogre::Any getValue (const void* target) const = 0;
105 
111  virtual void setValue (void* target, const Ogre::Any& value) const = 0;
112 
114  virtual const std::type_info& getValueTypeId () const = 0;
115 
123  return false;
124  }
125  };
126 
129  template<typename ValueT>
131  {
132  public:
134  virtual const ValueT getValueTyped (const void* target) const = 0;
136  virtual void setValueTyped (void* target, const ValueT& value) const = 0;
137 
138  private:
139  virtual const Ogre::Any getValue (const void* target) const {
140  return Ogre::Any(this->getValueTyped (target));
141  }
142 
143  virtual void setValue (void* target, const Ogre::Any& value) const {
144  this->setValueTyped (target, Ogre::any_cast<ValueT>(value));
145  }
146 
147  virtual const std::type_info& getValueTypeId () const {
148  return typeid(ValueT);
149  }
150 
151  virtual bool implementsTypedValuePropertyDescriptor () const {
152  return true;
153  }
154  };
155 
158  template <class TargetT, typename ParamT, typename InParamT = const ParamT&, typename OutParamT = const ParamT>
160  {
161  public:
162  typedef void(TargetT::*SetFunc)(InParamT);
163  typedef OutParamT(TargetT::*GetFunc)(void) const;
164 
165  private:
166  GetFunc mGetFunc;
167  SetFunc mSetFunc;
168 
169  public:
170  AccesorPropertyDescriptor (GetFunc getFunc, SetFunc setFunc)
171  {
172  mGetFunc = getFunc;
173  mSetFunc = setFunc;
174  }
175 
176  virtual bool canGetValue () const {
177  return mGetFunc != 0;
178  }
179 
180  virtual bool canSetValue () const {
181  return mSetFunc != 0;
182  }
183 
184  virtual const ParamT getValueTyped(const void* target) const
185  {
186  const TargetT* typedTarget = reinterpret_cast<const TargetT*>(target);
187  return (typedTarget->*mGetFunc)();
188  }
189 
190  virtual void setValueTyped(void* target, const ParamT& value) const
191  {
192  TargetT* typedTarget = reinterpret_cast<TargetT*>(target);
193  (typedTarget->*mSetFunc)(value);
194  }
195  };
196 
205  {
206  public:
208  virtual ~DefaultTypeDescriptor ();
209 
215  inline PropertyMap& getPropertyMap () { return mPropertyMap; }
216 
218  void add (const Ogre::String& name, const ValuePropertyDescriptor* descriptor);
219 
221  void clear ();
222 
224  virtual const ValuePropertyDescriptor* getPropertyDescriptor (const Ogre::String& name) const;
225 
227  virtual const std::vector<String> getPropertyNames () const;
228 
230  virtual const PropertyMap getFullPropertyMap () const;
231 
232  private:
233  void deleteAllPropertyDescriptors ();
234 
235  PropertyMap mPropertyMap;
236  };
237 
249  {
250  private:
251  void load();
252  void unload();
253 
254  public:
257 
258  DefaultTypeDescriptor* CaelumSystemTypeDescriptor;
259  DefaultTypeDescriptor* PointStarfieldTypeDescriptor;
260  DefaultTypeDescriptor* BaseSkyLightTypeDescriptor;
261  DefaultTypeDescriptor* GroundFogTypeDescriptor;
262  DefaultTypeDescriptor* PrecipitationTypeDescriptor;
263  DefaultTypeDescriptor* DepthComposerTypeDescriptor;
264  DefaultTypeDescriptor* FlatCloudLayerTypeDescriptor;
265  DefaultTypeDescriptor* SkyDomeTypeDescriptor;
266  };
267 }
268 
269 #endif // CAELUM_TYPE_DESCRIPTORS
270 
271 #endif // CAELUM__TYPE_DESCRIPTOR_H
Basic property descriptor interface.
Abstract interface for a type descriptor.
Default implementation of a TypeDescriptor.
ValuePropertyDescriptor implementation based on function pointers to get/set methods.
virtual void setValueTyped(void *target, const ParamT &value) const
Set the property&#39;s value.
Standard type descriptors for caelum components.
PropertyMap & getPropertyMap()
Direct access to the internal property map.
virtual bool canGetValue() const
If the value of the property can be read (true means write-only).
Variant of ValuePropertyDescriptor which allows faster typed get/set methods.
virtual const ParamT getValueTyped(const void *target) const
Get the property&#39;s value.
Caelum namespace.
Definition: Astronomy.cpp:24
virtual bool implementsTypedValuePropertyDescriptor() const
Check if this class also implements TypedValuePropertyDescriptor.
virtual bool canSetValue() const
If the value of the property can be set (false means read-only).