1 #ifndef __DALI_ANY_TYPE_H__
2 #define __DALI_ANY_TYPE_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 // Licensed under the Flora License, Version 1.0 (the License);
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://floralicense.org/license/
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an AS IS BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
21 * @addtogroup CAPI_DALI_OBJECT_MODULE
26 #include <typeinfo> // operator typeid
30 #include <dali/public-api/common/dali-common.h>
32 namespace Dali DALI_IMPORT_API
36 * @brief Stores a value of any type.
40 * Any uintVariable = 5u;
41 * Any floatVariable( 4.5f );
42 * Any strVariable( std::string( "Hello world" ) );
44 * unsigned int variable = AnyCast< unsigned int >( uintVariable );
45 * if ( typeid( int ) == uintVariable.GetType() )
52 * Default constructor.
57 * Destructor. Free resources.
62 * @brief Pass Assert message
64 * @param assertMessage Assert message to report
66 static void AssertAlways( const char* assertMessage );
69 * @brief Constructs a Any type with the given value.
71 * @param[in] value The given value.
73 template<typename Type>
74 Any( const Type& value )
75 : mContainer( new AnyContainerImpl<Type>( value ) )
81 * @param [in] any Any to be copied.
85 // If container isn't empty then copy the container?
86 if ( NULL != any.mContainer )
88 mContainer = any.mContainer->mCloneFunc( *any.mContainer );
92 // Otherwise mark new container as empty
98 * @brief Assigns a given value to the Any type.
100 * @note If the types are different, then the current container will be re-created.
102 * @param[in] value The given value.
104 template<typename Type>
105 Any& operator=( const Type& value )
107 // If the container is empty then assign the new value
108 if ( NULL == mContainer )
110 mContainer = new AnyContainerImpl< Type >( value );
114 // Check to see if this type is compatible with current container?
115 if ( mContainer->GetType() == typeid( Type ) )
117 // Same type so just set the new value
118 static_cast< AnyContainerImpl< Type >* >( mContainer )->SetValue( value );
122 // Incompatible types, so delete old container and assign a new one with this type and value
123 mContainer->mDeleteFunc( mContainer );
124 mContainer = new AnyContainerImpl< Type >( value );
131 * @brief Assignment operator.
133 * @exception DaliException If parameter any is of a different type.
135 * @param [in] any Any to be assigned which contains a value of identical type to current contents.
137 Any& operator=( const Any& any );
140 * @brief Get a value of type Type from container
142 * @param type destination of type Type to write to
144 template<typename Type>
145 void Get( Type& type )
151 * @brief Returns the type info of the stored value.
153 * @return The std::type_info of the stored value or the type info of the void
154 * type if there is no value stored.
156 const std::type_info& GetType() const;
159 * @brief Retrieves the stored value in the Any type.
161 * @return The stored value.
163 template<typename Type>
164 const Type& Get() const
167 if ( NULL == mContainer )
169 AssertAlways( "Any::Get(). mContainer is NULL" );
172 // Check if the value has the same value than the Any type.
173 if( mContainer->GetType() != typeid( Type ) )
175 AssertAlways( "Any::Get(). Trying to retrieve a value of a different type than the template one." );
177 return static_cast< AnyContainerImpl< Type >* >( mContainer )->GetValue();
181 * @brief Return pointer of Type to the value stored
183 * @return pointer to the value or NULL if no value is contained
185 template<typename Type>
188 if( NULL == mContainer )
192 // Check if the value has the same value than the Any type.
193 if( mContainer->GetType() != typeid( Type ) )
195 AssertAlways( "Any::GetPointer(). Trying to retrieve a pointer to a value of a different type than the template one." );
197 return static_cast< AnyContainerImpl< Type >* >( mContainer )->GetPointerToValue();
201 * @brief Returns whether container holds a value
203 * @return true if the container is empty, else false.
207 return ( NULL == mContainer ) ? true : false;
210 struct AnyContainerBase; // Forward declaration for typedef
211 typedef AnyContainerBase* (*CloneFunc)( const AnyContainerBase& base );
212 typedef void (*DeleteFunc)( const AnyContainerBase* base );
215 * Base container to hold type for match verification and instance cloning function
218 struct AnyContainerBase
221 * @brief Constructor of base container
223 * @param type typeid of container
224 * @param cloneFunc Cloning function to replicate this container type
225 * @param deleteFunc Deleting function to destroy this container type
227 AnyContainerBase( const std::type_info& type, CloneFunc cloneFunc, DeleteFunc deleteFunc )
229 mCloneFunc( cloneFunc ),
230 mDeleteFunc( deleteFunc )
234 * @brief Get the typeid of this container
238 const std::type_info& GetType() const
243 const::std::type_info& mType; // typeID
244 CloneFunc mCloneFunc; // cloning function for this container
245 DeleteFunc mDeleteFunc; // deleting function for this container
250 * @brief Templated Clone function from container base
252 * @param base reference to container
254 template<typename Type>
255 struct AnyContainerImplCloner
257 static AnyContainerBase* Clone( const AnyContainerBase& base )
259 return new AnyContainerImpl< Type >( static_cast< AnyContainerImpl< Type > >( base ) );
264 * @brief Templated Delete function from container base
266 * @param base pointer to container
268 template<typename Type>
269 struct AnyContainerImplDelete
271 static void Delete( const AnyContainerBase* base )
273 delete ( static_cast< const AnyContainerImpl< Type >* > ( base ) );
278 * @brief Templated class to hold value for type
281 template<typename Type>
282 class AnyContainerImpl : public AnyContainerBase
287 * @brief Constructor to create container holding value of type Type
289 * @param value Value of Type
291 AnyContainerImpl( const Type& value )
292 : AnyContainerBase( typeid( Type ),
293 static_cast< CloneFunc >( &AnyContainerImplCloner< Type >::Clone ),
294 static_cast< DeleteFunc >( &AnyContainerImplDelete< Type >::Delete ) ),
299 * @brief Constructor to create new container of type from and existing container (cloning)
301 * @param base reference to base container to copy from
303 AnyContainerImpl( const AnyContainerBase& base )
304 : AnyContainerBase( typeid( Type ),
305 static_cast< CloneFunc >( &AnyContainerImplCloner< Type >::Clone ),
306 static_cast< DeleteFunc >( &AnyContainerImplDelete< Type >::Delete ) )
308 mValue = static_cast< const AnyContainerImpl& >( base ).GetValue();
312 * @brief Get the container's stored value
314 * @return value of type Type
316 const Type& GetValue() const
322 * @brief Set the container's stored value
324 * @param value of type Type
326 void SetValue( const Type& value )
332 * @brief Get a pointer to the value held
334 * @return pointer to the value of type Type
336 Type* GetPointerToValue()
338 return static_cast< Type* >( &mValue );
345 AnyContainerBase* mContainer;
350 * AnyCast helper functions ( replicates boost functionality, but without exception generation )
354 * @brief Extract a pointer to the held type of an Any object from a pointer to that Any object (NULL if empty )
356 * @param any Pointer to an Any object
358 * @return Pointer to the Type held
360 template<typename Type>inline Type* AnyCast( Any* any )
362 return any->GetPointer<Type>();
366 * @brief Extract a const pointer to the held type of an Any object from a pointer to that Any object (NULL if empty )
368 * @param any const Pointer to an Any object
370 * @return const Pointer to the Type held
372 template<typename Type>inline const Type* AnyCast( const Any* any )
374 return any->GetPointer<Type>();
378 * @brief Extract a held value of type Type from an Any object from a reference to that Any object
380 * @param any reference to an Any object
382 * @return Type value of type Type
384 template<typename Type>inline Type AnyCast( Any& any )
386 return any.Get<Type>();
390 * @brief Extract a held value of type Type from an Any object from a const reference to that Any object
392 * @param any reference to an Any object
394 * @return Type value of type Type
396 template<typename Type>inline Type AnyCast( const Any& any )
398 return any.Get<Type>();
406 #endif // __DALI_ANY_TYPE_H__