Crazy Eddie's GUI System  0.8.6
PropertySet.h
1 /***********************************************************************
2  created: 21/2/2004
3  author: Paul D Turner
4 
5  purpose: Defines interface for the PropertySet class
6 *************************************************************************/
7 /***************************************************************************
8  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files (the
12  * "Software"), to deal in the Software without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sublicense, and/or sell copies of the Software, and to
15  * permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  ***************************************************************************/
29 #ifndef _CEGUIPropertySet_h_
30 #define _CEGUIPropertySet_h_
31 
32 #include "CEGUI/Base.h"
33 #include "CEGUI/String.h"
34 #include "CEGUI/IteratorBase.h"
35 #include "CEGUI/Property.h"
36 #include "CEGUI/PropertyHelper.h"
37 #include "CEGUI/TypedProperty.h"
38 // not needed in this header but you are likely to use it if you include this,
39 // we also define the CEGUI_DEFINE_PROPERTY macro that relies on this here
40 #include "CEGUI/TplWindowProperty.h"
41 #include "CEGUI/Exceptions.h"
42 #include <map>
43 
44 #if defined(_MSC_VER)
45 # pragma warning(push)
46 # pragma warning(disable : 4251)
47 #endif
48 
49 // Start of CEGUI namespace section
50 namespace CEGUI
51 {
107 class CEGUIEXPORT PropertySet : public PropertyReceiver
108 {
109 public:
114  PropertySet(void) {}
115 
116 
121  virtual ~PropertySet(void) {}
122 
123 
138  void addProperty(Property* property);
139 
140 
148  void removeProperty(const String& name);
149 
150 
161  Property* getPropertyInstance(const String& name) const;
162 
163 
168  void clearProperties(void);
169 
170 
181  bool isPropertyPresent(const String& name) const;
182 
183 
196  const String& getPropertyHelp(const String& name) const;
197 
198 
211  String getProperty(const String& name) const;
212 
219  template<typename T>
221  {
222  PropertyRegistry::const_iterator pos = d_properties.find(name);
223 
224  if (pos == d_properties.end())
225  {
226  CEGUI_THROW(UnknownObjectException("There is no Property named '" + name + "' available in the set."));
227  }
228 
229  Property* baseProperty = pos->second;
230  TypedProperty<T>* typedProperty = dynamic_cast<TypedProperty<T>* >(baseProperty);
231 
232  if (typedProperty)
233  {
234  // yay, we can get native!
235  return typedProperty->getNative(this);
236  }
237  else
238  {
239  // fall back to string get
240  return PropertyHelper<T>::fromString(baseProperty->get(this));
241  }
242  }
243 
257  void setProperty(const String& name, const String& value);
258 
265  template<typename T>
266  void setProperty(const String& name, typename PropertyHelper<T>::pass_type value)
267  {
268  PropertyRegistry::iterator pos = d_properties.find(name);
269 
270  if (pos == d_properties.end())
271  {
272  CEGUI_THROW(UnknownObjectException("There is no Property named '" + name + "' available in the set."));
273  }
274 
275  Property* baseProperty = pos->second;
276  TypedProperty<T>* typedProperty = dynamic_cast<TypedProperty<T>* >(baseProperty);
277 
278  if (typedProperty)
279  {
280  // yay, we can set native!
281  typedProperty->setNative(this, value);
282  }
283  else
284  {
285  // fall back to string set
286  baseProperty->set(this, PropertyHelper<T>::toString(value));
287  }
288  }
289 
301  bool isPropertyDefault(const String& name) const;
302 
303 
314  String getPropertyDefault(const String& name) const;
315 
316 private:
317  typedef std::map<String, Property*, StringFastLessCompare
318  CEGUI_MAP_ALLOC(String, Property*)> PropertyRegistry;
319  PropertyRegistry d_properties;
320 
321 
322 public:
323  /*************************************************************************
324  Iterator stuff
325  *************************************************************************/
326  typedef ConstMapIterator<PropertyRegistry> PropertyIterator;
327 
333  PropertyIterator getPropertyIterator(void) const;
334 };
335 
352 #define CEGUI_DEFINE_PROPERTY(class_type, property_native_type, name, help, setter, getter, default_value)\
353 {\
354  static ::CEGUI::TplWindowProperty<class_type, property_native_type> sProperty(\
355  name, help, propertyOrigin, setter, getter, default_value);\
356  \
357  this->addProperty(&sProperty);\
358 }
359 
381 #define CEGUI_DEFINE_PROPERTY_NO_XML(class_type, property_native_type, name, help, setter, getter, default_value)\
382 {\
383  static ::CEGUI::TplWindowProperty<class_type, property_native_type> sProperty(\
384  name, help, propertyOrigin, setter, getter, default_value, false);\
385  \
386  this->addProperty(&sProperty);\
387 }
388 
389 } // End of CEGUI namespace section
390 
391 #if defined(_MSC_VER)
392 # pragma warning(pop)
393 #endif
394 
395 #endif // end of guard _CEGUIPropertySet_h_
Exception class used when a request was made for an unknown object.
Definition: Exceptions.h:246
Functor that can be used as comparator in a std::map with String keys. It's faster than using the def...
Definition: String.h:5579
Main namespace for Crazy Eddie's GUI Library.
Definition: arch_overview.dox:1
virtual String get(const PropertyReceiver *receiver) const =0
Return the current value of the Property as a String.
void setProperty(const String &name, typename PropertyHelper< T >::pass_type value)
Sets the current value of a Property.
Definition: PropertySet.h:266
Interface providing introspection capabilities.
Definition: PropertySet.h:107
base class for properties able to do native set/get
Definition: TypedProperty.h:49
virtual ~PropertySet(void)
Destructor for PropertySet objects.
Definition: PropertySet.h:121
virtual void setNative(PropertyReceiver *receiver, typename Helper::pass_type value)
native set method, sets the property given a native type
Definition: TypedProperty.h:80
PropertyHelper< T >::return_type getProperty(const String &name) const
Gets the current value of the specified Property.
Definition: PropertySet.h:220
Dummy base class to ensure correct casting of receivers.
Definition: Property.h:45
virtual Helper::safe_method_return_type getNative(const PropertyReceiver *receiver) const
native get method, returns the native type by copy
Definition: TypedProperty.h:92
PropertySet(void)
Constructs a new PropertySet object.
Definition: PropertySet.h:114
Helper class used to convert various data types to and from the format expected in Property strings...
Definition: ForwardRefs.h:84
iterator class for maps
Definition: IteratorBase.h:196
An abstract class that defines the interface to access object properties by name. ...
Definition: Property.h:60
String class used within the GUI system.
Definition: String.h:62
virtual void set(PropertyReceiver *receiver, const String &value)=0
Sets the value of the property.