Crazy Eddie's GUI System  0.8.4
PropertyDefinition.h
00001 /***********************************************************************
00002     created:    Sun Jun 26 2005
00003     author:     Paul D Turner <paul@cegui.org.uk>
00004 *************************************************************************/
00005 /***************************************************************************
00006  *   Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
00007  *
00008  *   Permission is hereby granted, free of charge, to any person obtaining
00009  *   a copy of this software and associated documentation files (the
00010  *   "Software"), to deal in the Software without restriction, including
00011  *   without limitation the rights to use, copy, modify, merge, publish,
00012  *   distribute, sublicense, and/or sell copies of the Software, and to
00013  *   permit persons to whom the Software is furnished to do so, subject to
00014  *   the following conditions:
00015  *
00016  *   The above copyright notice and this permission notice shall be
00017  *   included in all copies or substantial portions of the Software.
00018  *
00019  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00020  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00021  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00022  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00023  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00024  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00025  *   OTHER DEALINGS IN THE SOFTWARE.
00026  ***************************************************************************/
00027 #ifndef _CEGUIFalPropertyDefinition_h_
00028 #define _CEGUIFalPropertyDefinition_h_
00029 
00030 #include "CEGUI/falagard/FalagardPropertyBase.h"
00031 #include "CEGUI/falagard/XMLHandler.h"
00032 #include "CEGUI/Logger.h"
00033 
00034 namespace CEGUI
00035 {
00036 template <typename T>
00037 class PropertyDefinition : public FalagardPropertyBase<T>
00038 {
00039 public:
00040     typedef typename TypedProperty<T>::Helper Helper;
00041 
00042     //------------------------------------------------------------------------//
00043     PropertyDefinition(const String& name, const String& initialValue,
00044                        const String& help, const String& origin,
00045                        bool redrawOnWrite, bool layoutOnWrite,
00046                        const String& fireEvent, const String& eventNamespace) :
00047         FalagardPropertyBase<T>(name, help, initialValue, origin,
00048                                 redrawOnWrite, layoutOnWrite,
00049                                 fireEvent, eventNamespace),
00050                                 d_userStringName(name + PropertyDefinitionBase::UserStringNameSuffix)
00051     {
00052     }
00053 
00054     //------------------------------------------------------------------------//
00055     ~PropertyDefinition() {}
00056 
00057     //------------------------------------------------------------------------//
00058     void initialisePropertyReceiver(PropertyReceiver* receiver) const
00059     {
00060         setWindowUserString(static_cast<Window*>(receiver), this->d_default);
00061     }
00062 
00063     //------------------------------------------------------------------------//
00064     Property* clone() const
00065     {
00066         return CEGUI_NEW_AO PropertyDefinition<T>(*this);
00067     }
00068 
00069 protected:
00070     //------------------------------------------------------------------------//
00071     typename Helper::safe_method_return_type
00072     getNative_impl(const PropertyReceiver* receiver) const
00073     {
00074         const Window* const wnd = static_cast<const Window*>(receiver);
00075 
00076         // the try/catch is used instead of querying the existence of the user
00077         // string in order that for the 'usual' case - where the user string
00078         // exists - there is basically no additional overhead, and that any
00079         // overhead is incurred only for the initial creation of the user
00080         // string.
00081         // Maybe the only negative here is that an error gets logged, though
00082         // this can be treated as a 'soft' error.
00083         CEGUI_TRY
00084         {
00085             return Helper::fromString(wnd->getUserString(d_userStringName));
00086         }
00087         CEGUI_CATCH (UnknownObjectException&)
00088         {
00089             Logger::getSingleton().logEvent(
00090                 "PropertyDefiniton::get: Defining new user string: " +
00091                 d_userStringName);
00092 
00093             // HACK: FIXME: TODO: This const_cast is basically to allow the
00094             // above mentioned performance measure; the alternative would be
00095             // to just return d_default, and while technically correct, it
00096             // would be very slow.
00097             const_cast<Window*>(wnd)->
00098                 setUserString(d_userStringName, TypedProperty<T>::d_default);
00099 
00100             return Helper::fromString(TypedProperty<T>::d_default);
00101         }
00102     }
00103 
00104     //------------------------------------------------------------------------//
00105     void setNative_impl(PropertyReceiver* receiver,typename Helper::pass_type value)
00106     {
00107         setWindowUserString(static_cast<Window*>(receiver), Helper::toString(value));
00108         FalagardPropertyBase<T>::setNative_impl(receiver, value);
00109     }
00110 
00111     //------------------------------------------------------------------------//
00112     void setWindowUserString(Window* window, const String& value) const
00113     {
00114         window->setUserString(d_userStringName, value);
00115     }
00116 
00117     //------------------------------------------------------------------------//
00118     void writeDefinitionXMLElementType(XMLSerializer& xml_stream) const
00119     {
00120         xml_stream.openTag(Falagard_xmlHandler::PropertyDefinitionElement);
00121         writeDefinitionXMLAdditionalAttributes(xml_stream);
00122     }
00123     //------------------------------------------------------------------------//
00124     void writeDefinitionXMLAdditionalAttributes(XMLSerializer& xml_stream) const
00125     {
00126         if(FalagardPropertyBase<T>::d_dataType.compare(Falagard_xmlHandler::GenericDataType) != 0)
00127             xml_stream.attribute(Falagard_xmlHandler::TypeAttribute, FalagardPropertyBase<T>::d_dataType);
00128 
00129         if (!PropertyDefinitionBase::d_helpString.empty() && PropertyDefinitionBase::d_helpString.compare(CEGUI::Falagard_xmlHandler::PropertyDefinitionHelpDefaultValue) != 0)
00130             xml_stream.attribute(Falagard_xmlHandler::HelpStringAttribute, PropertyDefinitionBase::d_helpString);
00131     }
00132 
00133 
00134     //------------------------------------------------------------------------//
00135 
00136     String d_userStringName;
00137 };
00138 
00139 }
00140 
00141 #endif
00142 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends