2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FUi_PropertyBase.h
19 * @brief This is the header file for the _PropertyBase class.
21 * This header file contains declarations of the _PropertyBase class. @n
23 #ifndef _FUI_INTERNAL_PROPERTYBASE_H_
24 #define _FUI_INTERNAL_PROPERTYBASE_H_
26 #include <FOspConfig.h>
27 #include <FBaseTypes.h>
28 #include <FBaseObject.h>
29 #include <FBaseColHashMap.h>
30 #include <FUiVariant.h>
31 #include <FBaseSysLog.h>
32 #include "FUi_IPropertyChangeEventListener.h"
33 #include "FUi_PropertyUtils.h"
42 namespace Tizen { namespace Ui
45 #define DECLARE_CLASS_BEGIN(classType, superClassType) \
47 typedef classType _THISCLASS; \
48 typedef superClassType _SUPERCLASS; \
49 class __register_property_class__ \
50 : public Tizen::Ui::_RegisterPropertyBase \
53 __register_property_class__() \
56 #define DECLARE_PROPERTY(Name, GetName, SetName) \
58 _RegisterPropertyBase::RegisterProperty<_THISCLASS>(Name, &_THISCLASS::GetName, &_THISCLASS::SetName); \
61 #define DECLARE_READONLY_PROPERTY(Name, GetName) \
63 _RegisterPropertyBase::RegisterProperty<_THISCLASS>(Name, &_THISCLASS::GetName, null); \
66 #define DECLARE_WRITEONLY_PROPERTY(Name, SetName) \
68 _RegisterPropertyBase::RegisterProperty<_THISCLASS>(Name, null, &_THISCLASS::SetName); \
71 #define DECLARE_CLASS_END() \
73 virtual ~__register_property_class__(void) { } \
76 static __register_property_class__ _registerPropertyClass; \
77 virtual result SetProperty(const Tizen::Base::String & name, const Tizen::Ui::Variant &value) \
80 result r = E_SUCCESS; \
81 if (IsPropertyChangeEventListenerAdded()) \
83 bool returnValue = _registerPropertyClass.IsPropertyAvailable<_THISCLASS>(this, name); \
86 Tizen::Ui::Variant oldValue(GetProperty(name)); \
87 _PropertyBase::FirePropertyEvent(*this, name, oldValue, value, false); \
88 r = _registerPropertyClass.SetProperty<_THISCLASS>(this, name, value); \
89 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r)); \
90 _PropertyBase::FirePropertyEvent(*this, name, oldValue, value, true); \
94 r = _SUPERCLASS::SetProperty(name, value); \
100 r = _registerPropertyClass.SetProperty<_THISCLASS>(this, name, value); \
101 if (r == E_KEY_NOT_FOUND) \
103 r = _SUPERCLASS::SetProperty(name, value); \
107 if (r != E_KEY_NOT_FOUND) \
109 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r)); \
113 virtual Tizen::Ui::Variant GetProperty(const Tizen::Base::String & name) const { \
115 Tizen::Ui::Variant returnValue = _registerPropertyClass.GetProperty<_THISCLASS>(this, name); \
116 if (GetLastResult() == E_KEY_NOT_FOUND) \
118 returnValue = _SUPERCLASS::GetProperty(name); \
119 return returnValue; \
123 result r = GetLastResult();\
124 SysTryReturn(NID_UI, r == E_SUCCESS, returnValue, r, "[%s] Propagating.", GetErrorMessage(r)); \
126 return returnValue; \
129 #define IMPLEMENT_PROPERTY(classType) \
130 classType::__register_property_class__ classType::_registerPropertyClass;
132 template<typename OwnType>
134 : public Tizen::Base::Object
137 typedef result (OwnType::*SetterType)(const Variant& value);
138 typedef Variant (OwnType::*GetterType)(void) const;
145 virtual ~_Property(void)
149 void Register(GetterType getter, SetterType setter) { __setter = setter; __getter = getter; }
150 Variant GetProperty(const Object* pObject) const
152 return Variant((((OwnType*) pObject)->*__getter)());
154 result SetProperty(const Object* pObject, const Variant& value)
156 OwnType* pOwnType = (OwnType*) pObject;
157 return (pOwnType->*__setter)(value);
159 bool IsReadOnly(void) const { return __setter == null; }
160 bool IsWriteOnly(void) const { return __getter == null; }
167 class _RegisterPropertyBase
168 : public Tizen::Base::Object
171 _RegisterPropertyBase()
175 virtual ~_RegisterPropertyBase(void)
179 __pHashMap->RemoveAll(true);
184 template<class OwnType>
185 result SetProperty(const Object* pObject, const Tizen::Base::String& name, const Variant& value)
189 Tizen::Base::Collection::HashMap* pMap = GetHashMap();
190 _Property<OwnType>* pProperty = null;
193 pProperty = static_cast<_Property<OwnType>*>(pMap->GetValue(name));
194 if (pProperty != null)
196 SysTryReturn(NID_UI, !pProperty->IsReadOnly(), E_INVALID_OPERATION, E_INVALID_OPERATION, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
197 return pProperty->SetProperty(pObject, value);
200 return E_KEY_NOT_FOUND;
203 template<class OwnType>
204 Variant GetProperty(const Object* pObject, const Tizen::Base::String& name) const
208 Tizen::Base::Collection::HashMap* pMap = GetHashMap();
209 _Property<OwnType>* pProperty = null;
212 pProperty = static_cast<_Property<OwnType>*>(pMap->GetValue(name));
213 if (pProperty != null)
215 SysTryReturn(NID_UI, !pProperty->IsWriteOnly(), Variant(), E_INVALID_OPERATION, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
216 return pProperty->GetProperty(pObject);
220 SetLastResult(E_KEY_NOT_FOUND);
224 return Variant::NULL_VARIANT;
227 template<class OwnType>
228 bool IsPropertyAvailable(const Object* pObject, const Tizen::Base::String& name) const
230 Tizen::Base::Collection::HashMap* pMap = GetHashMap();
231 _Property<OwnType>* pProperty = null;
234 pProperty = static_cast<_Property<OwnType>*>(pMap->GetValue(name));
235 if (pProperty != null)
244 template<class OwnType>
245 void RegisterProperty(const Tizen::Base::String& propertyName, typename _Property<OwnType>::GetterType getter, typename _Property<OwnType>::SetterType setter)
248 result r = E_SUCCESS;
250 Tizen::Base::Collection::HashMap* pMap = GetHashMap();
253 // create the property name key in the HashMap
255 r = pMap->ContainsKey(propertyName, hasKey);
256 if (!IsFailed(r) && hasKey == false)
258 _Property<OwnType>* pProperty = new (std::nothrow) _Property<OwnType>;
259 if (pProperty == null)
261 SysLog(NID_UI, "[E_OUT_OF_MEMORY] Unable to create _Property.");
262 SetLastResult(E_OUT_OF_MEMORY);
265 pProperty->Register(getter, setter);
266 pMap->Add(*(new (std::nothrow) Tizen::Base::String(propertyName)), *pProperty);
271 result InitHashMap(void)
273 result r = E_SUCCESS;
275 if (__pHashMap == null)
277 __pHashMap = new (std::nothrow) Tizen::Base::Collection::HashMap;
278 if (__pHashMap == null)
280 return E_OUT_OF_MEMORY;
283 r = __pHashMap->Construct(16, 0.75f, *_PropertyUtils::GetInstance()->GetStringHashProvider(), *_PropertyUtils::GetInstance()->GetStringComparer());
289 Tizen::Base::Collection::HashMap* GetHashMap(void) const
291 result r = E_SUCCESS;
293 _RegisterPropertyBase& thisObject = const_cast<_RegisterPropertyBase&>(*this);
294 if (__pHashMap == null)
296 r = thisObject.InitHashMap();
299 SysLog(NID_UI, "[E_OUT_OF_MEMORY] Unable to create HashMap.");
300 SetLastResult(E_OUT_OF_MEMORY);
308 _RegisterPropertyBase(const _RegisterPropertyBase&);
309 _RegisterPropertyBase& operator =(const _RegisterPropertyBase&);
310 Tizen::Base::Collection::HashMap* __pHashMap;
311 }; // _RegisterPropertyBase
313 class _OSP_EXPORT_ _PropertyBase
314 : public Tizen::Base::Object
318 virtual ~_PropertyBase(void);
320 virtual result SetProperty(const Tizen::Base::String& name, const Variant& value);
321 virtual Variant GetProperty(const Tizen::Base::String& name) const;
323 result AddPropertyChangeEventListener(const Tizen::Ui::_IPropertyChangeEventListener& listener);
324 result RemovePropertyChangeEventListener(const Tizen::Ui::_IPropertyChangeEventListener& listener);
325 result SetPropertyChangeEventListener(const Tizen::Ui::_IPropertyChangeEventListener* pListener);
326 bool FirePropertyEvent(const Tizen::Ui::_PropertyBase& source, const Tizen::Base::String& name, const Variant& oldValue, const Variant& newValue, bool isChangedEvent = false);
327 bool IsPropertyChangeEventListenerAdded(void) const;
330 _PropertyBase(const _PropertyBase&);
331 _PropertyBase& operator =(const _PropertyBase&);
332 Tizen::Ui::_IPropertyChangeEventListener* __pPropertyChangeEventListener;
337 #endif //_FUI_INTERNAL_PROPERTYBASE_H_