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 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 * @note If the types are different, then the current container will be re-created.
111 template<typename Type>
112 Any& operator=( const Type& value )
114 // If the container is empty then assign the new value
115 if ( NULL == mContainer )
117 mContainer = new AnyContainerImpl< Type >( value );
121 // Check to see if this type is compatible with current container?
122 if ( mContainer->GetType() == typeid( Type ) )
124 // Same type so just set the new value
125 static_cast< AnyContainerImpl< Type >* >( mContainer )->SetValue( value );
129 // Incompatible types, so delete old container and assign a new one with this type and value
130 mContainer->mDeleteFunc( mContainer );
131 mContainer = new AnyContainerImpl< Type >( value );
138 * @brief Assignment operator.
141 * @param [in] any Any to be assigned which contains a value of identical type to current contents.
142 * @exception DaliException If parameter any is of a different type.
145 DALI_IMPORT_API Any& operator=( const Any& any );
148 * @brief Get a value of type Type from container
151 * @param type destination of type Type to write to
153 template<typename Type>
154 void Get( Type& type ) const
160 * @brief Returns the type info of the stored value.
163 * @return The std::type_info of the stored value or the type info of the void
164 * type if there is no value stored.
166 DALI_IMPORT_API const std::type_info& GetType() const;
169 * @brief Retrieves the stored value in the Any type.
172 * @return The stored value.
174 template<typename Type>
175 const Type& Get() const
178 if ( NULL == mContainer )
180 AssertAlways( "Any::Get(). mContainer is NULL" );
183 // Check if the value has the same value than the Any type.
184 if( mContainer->GetType() != typeid( Type ) )
186 AssertAlways( "Any::Get(). Trying to retrieve a value of a different type than the template one." );
188 return static_cast< AnyContainerImpl< Type >* >( mContainer )->GetValue();
192 * @brief Return pointer of Type to the value stored
195 * @return pointer to the value or NULL if no value is contained
197 template<typename Type>
200 if( NULL == mContainer )
204 // Check if the value has the same value than the Any type.
205 if( mContainer->GetType() != typeid( Type ) )
207 AssertAlways( "Any::GetPointer(). Trying to retrieve a pointer to a value of a different type than the template one." );
209 return static_cast< AnyContainerImpl< Type >* >( mContainer )->GetPointerToValue();
213 * @brief Return pointer of Type to the value stored
216 * @return pointer to the value or NULL if no value is contained
218 template<typename Type>
219 const Type* GetPointer() const
221 if( NULL == mContainer )
225 // Check if the value has the same value than the Any type.
226 if( mContainer->GetType() != typeid( Type ) )
228 AssertAlways( "Any::GetPointer(). Trying to retrieve a pointer to a value of a different type than the template one." );
230 return static_cast< AnyContainerImpl< Type >* >( mContainer )->GetPointerToValue();
234 * @brief Returns whether container holds a value
237 * @return true if the container is empty, else false.
241 return ( NULL == mContainer ) ? true : false;
244 struct AnyContainerBase; // Forward declaration for typedef
245 typedef AnyContainerBase* (*CloneFunc)( const AnyContainerBase& base );
246 typedef void (*DeleteFunc)( const AnyContainerBase* base );
249 * @brief Base container to hold type for match verification and instance cloning function
253 struct AnyContainerBase
256 * @brief Constructor of base container
259 * @param type typeid of container
260 * @param cloneFunc Cloning function to replicate this container type
261 * @param deleteFunc Deleting function to destroy this container type
263 AnyContainerBase( const std::type_info& type, CloneFunc cloneFunc, DeleteFunc deleteFunc )
265 mCloneFunc( cloneFunc ),
266 mDeleteFunc( deleteFunc )
270 * @brief Get the typeid of this container
275 const std::type_info& GetType() const
280 const::std::type_info& mType; // typeID
281 CloneFunc mCloneFunc; // cloning function for this container
282 DeleteFunc mDeleteFunc; // deleting function for this container
287 * @brief Templated Clone function from container base
290 * @param base reference to container
292 template<typename Type>
293 struct AnyContainerImplCloner
295 static AnyContainerBase* Clone( const AnyContainerBase& base )
297 return new AnyContainerImpl< Type >( static_cast< AnyContainerImpl< Type > >( base ) );
302 * @brief Templated Delete function from container base
305 * @param base pointer to container
307 template<typename Type>
308 struct AnyContainerImplDelete
310 static void Delete( const AnyContainerBase* base )
312 delete ( static_cast< const AnyContainerImpl< Type >* > ( base ) );
317 * @brief Templated class to hold value for type
321 template<typename Type>
322 class AnyContainerImpl : public AnyContainerBase
327 * @brief Constructor to create container holding value of type Type
330 * @param value Value of Type
332 AnyContainerImpl( const Type& value )
333 : AnyContainerBase( typeid( Type ),
334 static_cast< CloneFunc >( &AnyContainerImplCloner< Type >::Clone ),
335 static_cast< DeleteFunc >( &AnyContainerImplDelete< Type >::Delete ) ),
340 * @brief Constructor to create new container of type from and existing container (cloning)
343 * @param base reference to base container to copy from
345 AnyContainerImpl( const AnyContainerBase& base )
346 : AnyContainerBase( typeid( Type ),
347 static_cast< CloneFunc >( &AnyContainerImplCloner< Type >::Clone ),
348 static_cast< DeleteFunc >( &AnyContainerImplDelete< Type >::Delete ) )
350 mValue = static_cast< const AnyContainerImpl& >( base ).GetValue();
354 * @brief Get the container's stored value
357 * @return value of type Type
359 const Type& GetValue() const
365 * @brief Set the container's stored value
368 * @param value of type Type
370 void SetValue( const Type& value )
376 * @brief Get a pointer to the value held
379 * @return pointer to the value of type Type
381 Type* GetPointerToValue()
383 return static_cast< Type* >( &mValue );
387 * @brief Get a pointer to the value held
390 * @return pointer to the value of type Type
392 const Type* GetPointerToValue() const
394 return static_cast< const Type* >( &mValue );
401 AnyContainerBase* mContainer;
406 * AnyCast helper functions
410 * @brief Extract a pointer to the held type of an Any object from a pointer to that Any object (NULL if empty )
413 * @param any Pointer to an Any object
415 * @return Pointer to the Type held
417 template<typename Type>
418 inline Type* AnyCast( Any* any )
420 return any->GetPointer<Type>();
424 * @brief Extract a const pointer to the held type of an Any object from a pointer to that Any object (NULL if empty )
427 * @param any const Pointer to an Any object
429 * @return const Pointer to the Type held
431 template<typename Type>
432 inline const Type* AnyCast( const Any* any )
434 return any->GetPointer<Type>();
438 * @brief Extract a held value of type Type from an Any object from a reference to that Any object
441 * @param any reference to an Any object
443 * @return Type value of type Type
445 template<typename Type>
446 inline Type AnyCast( Any& any )
448 return any.Get<Type>();
452 * @brief Extract a held value of type Type from an Any object from a const reference to that Any object
455 * @param any reference to an Any object
457 * @return Type value of type Type
459 template<typename Type>
460 inline Type AnyCast( const Any& any )
462 return any.Get<Type>();
466 * @brief Extract a reference to the held value of type Type from an Any object from a reference to that Any object
469 * @param any reference to an Any object
471 * @return A reference to the Type value of type Type
473 template<typename Type>
474 inline Type& AnyCastReference( Any& any )
476 return any.Get<Type>();
480 * @brief Extract a const reference to the held value of type Type from an Any object from a const reference to that Any object
483 * @param any reference to an Any object
485 * @return A const reference to the Type value of type Type
487 template<typename Type>
488 inline const Type& AnyCastReference( const Any& any )
490 return any.Get<Type>();
498 #endif // __DALI_ANY_TYPE_H__