1 #ifndef __DALI_ANY_TYPE_H__
2 #define __DALI_ANY_TYPE_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <typeinfo> // operator typeid
23 #include <cstddef> // NULL
26 #include <dali/public-api/common/dali-common.h>
31 * @addtogroup dali_core_object
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() )
53 * @brief Default constructor.
56 DALI_IMPORT_API Any();
59 * @brief Destructor. Free resources.
62 DALI_IMPORT_API ~Any();
65 * @brief Pass Assert message
68 * @param[in] assertMessage Assert message to report
70 DALI_IMPORT_API static void AssertAlways( const char* assertMessage );
73 * @brief Constructs a Any type with the given value.
76 * @param[in] value The given value.
78 template<typename Type>
79 Any( const Type& value )
80 : mContainer( new AnyContainerImpl<Type>( value ) )
85 * @brief Copy Constructor.
87 * @param[in] any Any to be copied.
91 // If container isn't empty then copy the container?
92 if ( NULL != any.mContainer )
94 mContainer = any.mContainer->mCloneFunc( *any.mContainer );
98 // Otherwise mark new container as empty
104 * @brief Assigns a given value to the Any type.
107 * @param[in] value The given value
108 * @return A reference to this
109 * @note If the types are different, then the current container will be re-created.
112 template<typename Type>
113 Any& operator=( const Type& value )
115 // If the container is empty then assign the new value
116 if ( NULL == mContainer )
118 mContainer = new AnyContainerImpl< Type >( value );
122 // Check to see if this type is compatible with current container?
123 if ( mContainer->GetType() == typeid( Type ) )
125 // Same type so just set the new value
126 static_cast< AnyContainerImpl< Type >* >( mContainer )->SetValue( value );
130 // Incompatible types, so delete old container and assign a new one with this type and value
131 mContainer->mDeleteFunc( mContainer );
132 mContainer = new AnyContainerImpl< Type >( value );
139 * @brief Assignment operator.
142 * @param [in] any Any to be assigned which contains a value of identical type to current contents.
143 * @return A reference to this
144 * @exception DaliException If parameter any is of a different type.
147 DALI_IMPORT_API Any& operator=( const Any& any );
150 * @brief Get a value of type Type from container
153 * @param[in] type destination of type Type to write to
155 template<typename Type>
156 void Get( Type& type ) const
162 * @brief Returns the type info of the stored value.
165 * @return The std::type_info of the stored value or the type info of the void
166 * type if there is no value stored.
168 DALI_IMPORT_API const std::type_info& GetType() const;
171 * @brief Retrieves the stored value in the Any type.
174 * @return The stored value.
176 template<typename Type>
177 const Type& Get() const
180 if ( NULL == mContainer )
182 AssertAlways( "Any::Get(). mContainer is NULL" );
185 // Check if the value has the same value than the Any type.
186 if( mContainer->GetType() != typeid( Type ) )
188 AssertAlways( "Any::Get(). Trying to retrieve a value of a different type than the template one." );
190 return static_cast< AnyContainerImpl< Type >* >( mContainer )->GetValue();
194 * @brief Return pointer of Type to the value stored
197 * @return pointer to the value or NULL if no value is contained
199 template<typename Type>
202 if( NULL == mContainer )
206 // Check if the value has the same value than the Any type.
207 if( mContainer->GetType() != typeid( Type ) )
209 AssertAlways( "Any::GetPointer(). Trying to retrieve a pointer to a value of a different type than the template one." );
211 return static_cast< AnyContainerImpl< Type >* >( mContainer )->GetPointerToValue();
215 * @brief Return pointer of Type to the value stored
218 * @return pointer to the value or NULL if no value is contained
220 template<typename Type>
221 const Type* GetPointer() const
223 if( NULL == mContainer )
227 // Check if the value has the same value than the Any type.
228 if( mContainer->GetType() != typeid( Type ) )
230 AssertAlways( "Any::GetPointer(). Trying to retrieve a pointer to a value of a different type than the template one." );
232 return static_cast< AnyContainerImpl< Type >* >( mContainer )->GetPointerToValue();
236 * @brief Returns whether container holds a value
239 * @return true if the container is empty, else false.
243 return ( NULL == mContainer ) ? true : false;
246 struct AnyContainerBase; // Forward declaration for typedef
247 typedef AnyContainerBase* (*CloneFunc)( const AnyContainerBase& base );
248 typedef void (*DeleteFunc)( const AnyContainerBase* base );
251 * @brief Base container to hold type for match verification and instance cloning function
255 struct AnyContainerBase
258 * @brief Constructor of base container
261 * @param[in] type typeid of container
262 * @param[in] cloneFunc Cloning function to replicate this container type
263 * @param[in] deleteFunc Deleting function to destroy this container type
265 AnyContainerBase( const std::type_info& type, CloneFunc cloneFunc, DeleteFunc deleteFunc )
267 mCloneFunc( cloneFunc ),
268 mDeleteFunc( deleteFunc )
272 * @brief Get the typeid of this container
277 const std::type_info& GetType() const
282 const::std::type_info& mType; // typeID
283 CloneFunc mCloneFunc; // cloning function for this container
284 DeleteFunc mDeleteFunc; // deleting function for this container
289 * @brief Templated Clone function from container base
292 * @param[in] base reference to container
294 template<typename Type>
295 struct AnyContainerImplCloner
297 static AnyContainerBase* Clone( const AnyContainerBase& base )
299 return new AnyContainerImpl< Type >( static_cast< AnyContainerImpl< Type > >( base ) );
304 * @brief Templated Delete function from container base
307 * @param[in] base pointer to container
309 template<typename Type>
310 struct AnyContainerImplDelete
312 static void Delete( const AnyContainerBase* base )
314 delete ( static_cast< const AnyContainerImpl< Type >* > ( base ) );
319 * @brief Templated class to hold value for type
323 template<typename Type>
324 class AnyContainerImpl : public AnyContainerBase
329 * @brief Constructor to create container holding value of type Type
332 * @param[in] value Value of Type
334 AnyContainerImpl( const Type& value )
335 : AnyContainerBase( typeid( Type ),
336 static_cast< CloneFunc >( &AnyContainerImplCloner< Type >::Clone ),
337 static_cast< DeleteFunc >( &AnyContainerImplDelete< Type >::Delete ) ),
342 * @brief Constructor to create new container of type from and existing container (cloning)
345 * @param[in] base reference to base container to copy from
347 AnyContainerImpl( const AnyContainerBase& base )
348 : AnyContainerBase( typeid( Type ),
349 static_cast< CloneFunc >( &AnyContainerImplCloner< Type >::Clone ),
350 static_cast< DeleteFunc >( &AnyContainerImplDelete< Type >::Delete ) )
352 mValue = static_cast< const AnyContainerImpl& >( base ).GetValue();
356 * @brief Get the container's stored value
359 * @return value of type Type
361 const Type& GetValue() const
367 * @brief Set the container's stored value
370 * @param[in] value of type Type
372 void SetValue( const Type& value )
378 * @brief Get a pointer to the value held
381 * @return pointer to the value of type Type
383 Type* GetPointerToValue()
385 return static_cast< Type* >( &mValue );
389 * @brief Get a pointer to the value held
392 * @return pointer to the value of type Type
394 const Type* GetPointerToValue() const
396 return static_cast< const Type* >( &mValue );
403 AnyContainerBase* mContainer;
408 * AnyCast helper functions
412 * @brief Extract a pointer to the held type of an Any object from a pointer to that Any object (NULL if empty )
415 * @param[in] any Pointer to an Any object
417 * @return Pointer to the Type held
419 template<typename Type>
420 inline Type* AnyCast( Any* any )
422 return any->GetPointer<Type>();
426 * @brief Extract a const pointer to the held type of an Any object from a pointer to that Any object (NULL if empty )
429 * @param[in] any const Pointer to an Any object
431 * @return const Pointer to the Type held
433 template<typename Type>
434 inline const Type* AnyCast( const Any* any )
436 return any->GetPointer<Type>();
440 * @brief Extract a held value of type Type from an Any object from a reference to that Any object
443 * @param[in] any reference to an Any object
445 * @return Type value of type Type
447 template<typename Type>
448 inline Type AnyCast( Any& any )
450 return any.Get<Type>();
454 * @brief Extract a held value of type Type from an Any object from a const reference to that Any object
457 * @param[in] any reference to an Any object
459 * @return Type value of type Type
461 template<typename Type>
462 inline Type AnyCast( const Any& any )
464 return any.Get<Type>();
468 * @brief Extract a reference to the held value of type Type from an Any object from a reference to that Any object
471 * @param[in] any reference to an Any object
473 * @return A reference to the Type value of type Type
475 template<typename Type>
476 inline Type& AnyCastReference( Any& any )
478 return any.Get<Type>();
482 * @brief Extract a const reference to the held value of type Type from an Any object from a const reference to that Any object
485 * @param[in] any reference to an Any object
487 * @return A const reference to the Type value of type Type
489 template<typename Type>
490 inline const Type& AnyCastReference( const Any& any )
492 return any.Get<Type>();
500 #endif // __DALI_ANY_TYPE_H__