bbtk::any< TypeTraits > Class Template Reference

#include <bbtkAny.h>

Collaboration diagram for bbtk::any< TypeTraits >:

Collaboration graph
[legend]

List of all members.

Public Types

typedef any< TypeTraits > self

Public Member Functions

 any ()
 Default constructor.
template<typename ValueType >
 any (const ValueType &value)
 Constructor with a value of template type.
 any (const any &other)
 Copy constructor.
 ~any ()
 Destructor.
anyswap (any &rhs)
 Swaps the content of this with another any.
template<typename ValueType >
anyoperator= (const ValueType &rhs)
 Affectation of a value of template type.
anyoperator= (const any &rhs)
 Affectation of another any.
bool empty () const
 Is it empty (no value held) ?
const std::type_info & type () const
 Returns the type_info of the held value.
const std::type_info & pointed_type () const
 Returns the type_info of the pointed held value.
template<typename Type >
bool contains ()
 Returns true iff the contained type is Type.
bool contains_pointer ()
 Returns true iff the contained type is a pointer.
bool contains (TypeInfo t)
 Returns true iff the contained type is t.
template<typename Type >
bool accepts ()
 Returns true iff any of type ValueType can be held.
template<typename ValueType >
const ValueType & get () const
template<typename ValueType >
const ValueType * getP () const
template<typename ValueType >
const ValueType & unsafe_get () const
void * get_pointer () const
void * get_pointer_to (const std::type_info &t) const

Private Attributes

anyplaceholdercontent
 content


Detailed Description

template<template< class > class TypeTraits>
class bbtk::any< TypeTraits >

A magic class which can store any type of data which is allowed by the template template parameter TypeTrait.

The only requirement on TypeTrait<T> is to have the member : static const bool value; which is true iff the type T is an allowed type

TypeTraits compliant objects are usually template structs for which the initialisation of value is set to false by default and set to true for the allowed types (template specialisation) Example : template <typename t>=""> struct mytypes { static const bool value; }; template <typename t>=""> const bool mytypes<T>::value = false; template <> const bool mytypes<int>::value = true; template <> const bool mytypes<float>::value = true; etc. You can use any boost type_trait, like is_pointer, is_floating_point, etc.

The class any is a generalisation of the boost::any class (see http://www.boost.org/doc/html/any.html). The boost::any class itself is reproduced by any<thing>, where thing is a TypeTrait whose value is true for all types.

Definition at line 184 of file bbtkAny.h.


Member Typedef Documentation

template<template< class > class TypeTraits>
typedef any< TypeTraits > bbtk::any< TypeTraits >::self

Definition at line 187 of file bbtkAny.h.


Constructor & Destructor Documentation

template<template< class > class TypeTraits>
bbtk::any< TypeTraits >::any (  )  [inline]

Default constructor.

Definition at line 191 of file bbtkAny.h.

Referenced by bbtk::any< TypeTraits >::operator=().

00192       : content(0)
00193     {
00194     }

Here is the caller graph for this function:

template<template< class > class TypeTraits>
template<typename ValueType >
bbtk::any< TypeTraits >::any ( const ValueType &  value  )  [inline]

Constructor with a value of template type.

Definition at line 198 of file bbtkAny.h.

References bbtkDebugMessage, bbtkError, bbtk::any< TypeTraits >::content, and bbtk::HumanTypeName().

00199       : content(0)
00200     {
00201       bbtkDebugMessage("Data",1,
00202                        bbtk::HumanTypeName<self>()<<" construction with <"
00203                        <<bbtk::HumanTypeName<ValueType>()<<">"<<std::endl);
00204       //      ValueType v = value;
00205       //      int** i = (int**)(&v);
00206       //      std::cout << "v="<<*i<<std::endl;
00207       
00208       if (accepts<ValueType>()) 
00209         { 
00210           content = new anyholder<ValueType>(value);
00211         }
00212       else 
00213         {
00214           bbtkError(bbtk::HumanTypeName<self>()
00215                     <<" constructor : data of type <"
00216                     <<bbtk::HumanTypeName<ValueType>()
00217                     <<"> are not accepted by traits <"
00218                     <<bbtk::HumanTypeName<TypeTraits<void> >()<<"> ");
00219         }
00220     }

Here is the call graph for this function:

template<template< class > class TypeTraits>
bbtk::any< TypeTraits >::any ( const any< TypeTraits > &  other  )  [inline]

Copy constructor.

Definition at line 223 of file bbtkAny.h.

References bbtkDebugMessage, bbtk::HumanTypeName(), and bbtk::any< TypeTraits >::type().

00224       : content(other.content ? other.content->clone() : 0)
00225     {
00226       bbtkDebugMessage("Data",1,
00227                        HumanTypeName<self>()
00228                        <<" copy construction with new content : "
00229                        <<HumanTypeName(type())
00230                        <<std::endl);
00231     }

Here is the call graph for this function:

template<template< class > class TypeTraits>
bbtk::any< TypeTraits >::~any (  )  [inline]

Destructor.

Definition at line 234 of file bbtkAny.h.

References bbtk::any< TypeTraits >::content.

00235     {
00236       delete content;
00237     }


Member Function Documentation

template<template< class > class TypeTraits>
template<typename Type >
bool bbtk::any< TypeTraits >::accepts (  )  [inline]

Returns true iff any of type ValueType can be held.

Definition at line 325 of file bbtkAny.h.

00326     {
00327       return TypeTraits<Type>::value; //is_valid();
00328     }

template<template< class > class TypeTraits>
bool bbtk::any< TypeTraits >::contains ( TypeInfo  t  )  [inline]

Returns true iff the contained type is t.

Definition at line 318 of file bbtkAny.h.

References bbtk::any< TypeTraits >::type().

00319     {
00320       return ( (bool)((type() == t)!=0) );
00321      }

Here is the call graph for this function:

template<template< class > class TypeTraits>
template<typename Type >
bool bbtk::any< TypeTraits >::contains (  )  [inline]

Returns true iff the contained type is Type.

Definition at line 306 of file bbtkAny.h.

References bbtk::any< TypeTraits >::type().

00307     {
00308       return ( type() == typeid(Type) );
00309     }

Here is the call graph for this function:

template<template< class > class TypeTraits>
bool bbtk::any< TypeTraits >::contains_pointer (  )  [inline]

Returns true iff the contained type is a pointer.

Definition at line 312 of file bbtkAny.h.

References bbtk::any< TypeTraits >::content, and bbtk::anyplaceholder::is_pointer().

00313     {
00314       return content ? content->is_pointer() : false;
00315     }

Here is the call graph for this function:

template<template< class > class TypeTraits>
bool bbtk::any< TypeTraits >::empty (  )  const [inline]

Is it empty (no value held) ?

Definition at line 286 of file bbtkAny.h.

References bbtk::any< TypeTraits >::content.

00287     {
00288       return !content;
00289     }

template<template< class > class TypeTraits>
template<typename ValueType >
const ValueType& bbtk::any< TypeTraits >::get (  )  const [inline]

Returns a const reference on the held value iff its type matches the template parameter

Definition at line 333 of file bbtkAny.h.

References bbtkDebugMessage, bbtkError, bbtk::any< TypeTraits >::content, bbtk::HumanTypeName(), and bbtk::any< TypeTraits >::type().

00334     {
00335         bbtkDebugMessage("Data",1,
00336                          HumanTypeName<self >()
00337                          <<" get<"<<HumanTypeName<ValueType>()
00338                          <<"> with content : "
00339                          <<HumanTypeName(type())<<std::endl);
00340 
00341       if ( type() == typeid(ValueType) )
00342         return static_cast< anyholder<ValueType> *>(content)->held;
00343 
00344       bbtkError(HumanTypeName<self >()
00345                 <<" get with type <"
00346                 <<bbtk::HumanTypeName<ValueType>()
00347                 <<"> does not match content type <"
00348                 <<bbtk::HumanTypeName<>(type())<<">");
00349     }

Here is the call graph for this function:

template<template< class > class TypeTraits>
void* bbtk::any< TypeTraits >::get_pointer (  )  const [inline]

Definition at line 392 of file bbtkAny.h.

References bbtkDebugMessage, bbtk::any< TypeTraits >::content, bbtk::anyplaceholder::get_pointer(), bbtk::HumanTypeName(), and bbtk::any< TypeTraits >::type().

00393     {
00394       void* p = content->get_pointer();
00395       bbtkDebugMessage("Data",1,
00396                        HumanTypeName<self>()
00397                        <<"::get_pointer() with content <"
00398                        <<HumanTypeName(this->type())
00399                        <<"> : result = "
00400                        << p
00401                        <<std::endl);
00402       return p;
00403     }

Here is the call graph for this function:

template<template< class > class TypeTraits>
void* bbtk::any< TypeTraits >::get_pointer_to ( const std::type_info &  t  )  const [inline]

Definition at line 405 of file bbtkAny.h.

References bbtkDebugMessage, bbtk::any< TypeTraits >::content, bbtk::anyplaceholder::get_pointer_to(), bbtk::HumanTypeName(), and bbtk::any< TypeTraits >::type().

00406     {
00407       void* p = content->get_pointer_to(t);
00408       bbtkDebugMessage("Data",1,
00409                        HumanTypeName<self>()
00410                        <<"::get_pointer_to("<<HumanTypeName(t)
00411                        <<") with content <"
00412                        <<HumanTypeName(this->type())
00413                        <<"> : result = "
00414                        << p
00415                        <<std::endl);
00416       return p;
00417     }

Here is the call graph for this function:

template<template< class > class TypeTraits>
template<typename ValueType >
const ValueType* bbtk::any< TypeTraits >::getP (  )  const [inline]

Definition at line 352 of file bbtkAny.h.

References bbtkError, bbtk::any< TypeTraits >::content, and bbtk::any< TypeTraits >::type().

00353     {
00354       if ( type() == typeid(ValueType) )
00355         return &static_cast< anyholder<ValueType> *>(content)->held;
00356  
00357       bbtkError(HumanTypeName<self >()
00358                 <<" getP with type <"
00359                 <<bbtk::HumanTypeName<ValueType>()
00360                 <<"> does not match content type <"
00361                 <<bbtk::HumanTypeName<>(type())<<">");
00362     }

Here is the call graph for this function:

template<template< class > class TypeTraits>
any& bbtk::any< TypeTraits >::operator= ( const any< TypeTraits > &  rhs  )  [inline]

Affectation of another any.

Definition at line 273 of file bbtkAny.h.

References bbtk::any< TypeTraits >::any(), bbtkDebugMessage, bbtk::HumanTypeName(), and bbtk::any< TypeTraits >::type().

00274     {
00275         bbtkDebugMessage("Data",1,
00276                          HumanTypeName<self >()
00277                          <<" operator=(const any&) with content : "
00278                          <<HumanTypeName(rhs.type())<<std::endl);
00279 
00280       any(rhs).swap(*this);
00281       return *this;
00282     }

Here is the call graph for this function:

template<template< class > class TypeTraits>
template<typename ValueType >
any& bbtk::any< TypeTraits >::operator= ( const ValueType &  rhs  )  [inline]

Affectation of a value of template type.

Definition at line 249 of file bbtkAny.h.

References bbtk::any< TypeTraits >::any(), bbtkDebugMessage, bbtkError, and bbtk::HumanTypeName().

00250     {
00251       bbtkDebugMessage("Data",1,
00252                        HumanTypeName<self>()
00253                        <<" operator= with data of type <"
00254                        <<HumanTypeName<ValueType>()
00255                        <<">"<<std::endl);
00256       if (accepts<ValueType>()) 
00257         {
00258           any(rhs).swap(*this);
00259           return *this;
00260         }
00261       else 
00262         {
00263           bbtkError(HumanTypeName<self>()
00264                     <<" operator= : data of type <"
00265                     <<HumanTypeName<ValueType>()
00266                     <<"> are not accepted by traits <"
00267                     <<HumanTypeName<TypeTraits<void> >()<<"> ");
00268         }
00269     
00270     }

Here is the call graph for this function:

template<template< class > class TypeTraits>
const std::type_info& bbtk::any< TypeTraits >::pointed_type (  )  const [inline]

Returns the type_info of the pointed held value.

Definition at line 298 of file bbtkAny.h.

References bbtk::any< TypeTraits >::content, and bbtk::anyplaceholder::pointed_type().

00299     {
00300       return content ? content->pointed_type() : typeid(void);
00301     }

Here is the call graph for this function:

template<template< class > class TypeTraits>
any& bbtk::any< TypeTraits >::swap ( any< TypeTraits > &  rhs  )  [inline]

Swaps the content of this with another any.

Definition at line 241 of file bbtkAny.h.

References bbtk::any< TypeTraits >::content.

00242     {
00243       std::swap(content, rhs.content);
00244       return *this;
00245     }

template<template< class > class TypeTraits>
const std::type_info& bbtk::any< TypeTraits >::type (  )  const [inline]

Returns the type_info of the held value.

Definition at line 292 of file bbtkAny.h.

References bbtk::any< TypeTraits >::content, and bbtk::anyplaceholder::type().

Referenced by bbtk::any< TypeTraits >::any(), bbtk::any< TypeTraits >::contains(), bbtk::any< TypeTraits >::get(), bbtk::any< TypeTraits >::get_pointer(), bbtk::any< TypeTraits >::get_pointer_to(), bbtk::any< TypeTraits >::getP(), bbtk::any< TypeTraits >::operator=(), and bbtk::any< TypeTraits >::unsafe_get().

00293     {
00294       return content ? content->type() : typeid(void);
00295     }

Here is the call graph for this function:

Here is the caller graph for this function:

template<template< class > class TypeTraits>
template<typename ValueType >
const ValueType& bbtk::any< TypeTraits >::unsafe_get (  )  const [inline]

Returns a const reference on the held value EVEN IF ITS TYPE DOES NOT MATCH THE TEMPLATE PARAMETER ** Hence must be used when one knows that the type is good Otherwise can lead to unpredictible results

Definition at line 369 of file bbtkAny.h.

References bbtkDebugMessage, bbtkError, bbtk::any< TypeTraits >::content, bbtk::HumanTypeName(), and bbtk::any< TypeTraits >::type().

00370     {
00371         bbtkDebugMessage("Data",1,
00372                          HumanTypeName<self>()
00373                          <<"::unsafe_get<"
00374                          <<HumanTypeName<ValueType>()<<"> with content : "
00375                          <<HumanTypeName(this->type())
00376                          <<std::endl);
00377 
00378         //      PrintValueIfIsPointer<ValueType>(static_cast< anyholder<ValueType> * >(content)->held);
00379         //      int** i = (int**)(&static_cast< anyholder<ValueType> * >(content)->held);
00380         //      std::cout << "v="<<*i<<std::endl;
00381 
00382         if (content) 
00383           return static_cast< anyholder<ValueType> * >(content)->held;
00384         
00385         bbtkError(HumanTypeName<self >()
00386                   <<"::usafe_get<"
00387                   <<bbtk::HumanTypeName<ValueType>()
00388                   <<"> : void content");
00389     }

Here is the call graph for this function:


Member Data Documentation

template<template< class > class TypeTraits>
anyplaceholder* bbtk::any< TypeTraits >::content [private]


The documentation for this class was generated from the following file:

Generated on Thu May 31 14:13:42 2012 for BBTK by  doxygen 1.5.7.1