#include <bbtkAny.h>
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. | |
any & | swap (any &rhs) |
Swaps the content of this with another any. | |
template<typename ValueType> | |
any & | operator= (const ValueType &rhs) |
Affectation of a value of template type. | |
any & | operator= (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 | |
anyplaceholder * | content |
content |
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 206 of file bbtkAny.h.
bbtk::any< TypeTraits >::any | ( | ) | [inline] |
Default constructor.
Definition at line 213 of file bbtkAny.h.
References bbtk::any< TypeTraits >::content.
00214 : content(0) 00215 { 00216 }
bbtk::any< TypeTraits >::any | ( | const ValueType & | value | ) | [inline] |
Constructor with a value of template type.
Definition at line 220 of file bbtkAny.h.
References bbtkDebugMessage, bbtkError, bbtk::any< TypeTraits >::content, and bbtk::HumanTypeName().
00221 : content(0) 00222 { 00223 bbtkDebugMessage("Data",1, 00224 bbtk::HumanTypeName<self>()<<" construction with <" 00225 <<bbtk::HumanTypeName<ValueType>()<<">"<<std::endl); 00226 // ValueType v = value; 00227 // int** i = (int**)(&v); 00228 // std::cout << "v="<<*i<<std::endl; 00229 00230 if (accepts<ValueType>()) 00231 { 00232 content = new anyholder<ValueType>(value); 00233 } 00234 else 00235 { 00236 bbtkError(bbtk::HumanTypeName<self>() 00237 <<" constructor : data of type <" 00238 <<bbtk::HumanTypeName<ValueType>() 00239 <<"> are not accepted by traits <" 00240 <<bbtk::HumanTypeName<TypeTraits<void> >()<<"> "); 00241 } 00242 }
bbtk::any< TypeTraits >::any | ( | const any< TypeTraits > & | other | ) | [inline] |
Copy constructor.
Definition at line 245 of file bbtkAny.h.
References bbtkDebugMessage, bbtk::any< TypeTraits >::content, bbtk::HumanTypeName(), and bbtk::any< TypeTraits >::type().
00246 : content(other.content ? other.content->clone() : 0) 00247 { 00248 bbtkDebugMessage("Data",1, 00249 HumanTypeName<self>() 00250 <<" copy construction with new content : " 00251 <<HumanTypeName(type()) 00252 <<std::endl); 00253 }
bbtk::any< TypeTraits >::~any | ( | ) | [inline] |
Destructor.
Definition at line 256 of file bbtkAny.h.
References bbtk::any< TypeTraits >::content.
00257 { 00258 delete content; 00259 }
any& bbtk::any< TypeTraits >::swap | ( | any< TypeTraits > & | rhs | ) | [inline] |
Swaps the content of this with another any.
Definition at line 263 of file bbtkAny.h.
References bbtk::any< TypeTraits >::content, and bbtk::any< TypeTraits >::swap().
Referenced by bbtk::any< TypeTraits >::swap().
any& bbtk::any< TypeTraits >::operator= | ( | const ValueType & | rhs | ) | [inline] |
Affectation of a value of template type.
Definition at line 271 of file bbtkAny.h.
References bbtkDebugMessage, bbtkError, bbtk::HumanTypeName(), and bbtk::any< TypeTraits >::operator=().
Referenced by bbtk::any< TypeTraits >::operator=().
00272 { 00273 bbtkDebugMessage("Data",1, 00274 HumanTypeName<self>() 00275 <<" operator= with data of type <" 00276 <<HumanTypeName<ValueType>() 00277 <<">"<<std::endl); 00278 if (accepts<ValueType>()) 00279 { 00280 any(rhs).swap(*this); 00281 return *this; 00282 } 00283 else 00284 { 00285 bbtkError(HumanTypeName<self>() 00286 <<" operator= : data of type <" 00287 <<HumanTypeName<ValueType>() 00288 <<"> are not accepted by traits <" 00289 <<HumanTypeName<TypeTraits<void> >()<<"> "); 00290 } 00291 00292 }
any& bbtk::any< TypeTraits >::operator= | ( | const any< TypeTraits > & | rhs | ) | [inline] |
Affectation of another any.
Definition at line 295 of file bbtkAny.h.
References bbtkDebugMessage, bbtk::HumanTypeName(), bbtk::any< TypeTraits >::operator=(), and bbtk::any< TypeTraits >::type().
00296 { 00297 bbtkDebugMessage("Data",1, 00298 HumanTypeName<self >() 00299 <<" operator=(const any&) with content : " 00300 <<HumanTypeName(rhs.type())<<std::endl); 00301 00302 any(rhs).swap(*this); 00303 return *this; 00304 }
bool bbtk::any< TypeTraits >::empty | ( | ) | const [inline] |
Is it empty (no value held) ?
Definition at line 308 of file bbtkAny.h.
References bbtk::any< TypeTraits >::content, and bbtk::any< TypeTraits >::empty().
Referenced by bbtk::any< TypeTraits >::empty().
00309 { 00310 return !content; 00311 }
const std::type_info& bbtk::any< TypeTraits >::type | ( | ) | const [inline] |
Returns the type_info of the held value.
Definition at line 314 of file bbtkAny.h.
References bbtk::any< TypeTraits >::content, bbtk::anyplaceholder::type(), and bbtk::any< TypeTraits >::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=(), bbtk::any< TypeTraits >::type(), and bbtk::any< TypeTraits >::unsafe_get().
const std::type_info& bbtk::any< TypeTraits >::pointed_type | ( | ) | const [inline] |
Returns the type_info of the pointed held value.
Definition at line 320 of file bbtkAny.h.
References bbtk::any< TypeTraits >::content, bbtk::anyplaceholder::pointed_type(), and bbtk::any< TypeTraits >::pointed_type().
Referenced by bbtk::any< TypeTraits >::pointed_type().
bool bbtk::any< TypeTraits >::contains | ( | ) | [inline] |
Returns true iff the contained type is Type.
Definition at line 328 of file bbtkAny.h.
References bbtk::any< TypeTraits >::contains(), and bbtk::any< TypeTraits >::type().
Referenced by bbtk::any< TypeTraits >::contains().
00329 { 00330 return ( type() == typeid(Type) ); 00331 }
bool bbtk::any< TypeTraits >::contains_pointer | ( | ) | [inline] |
Returns true iff the contained type is a pointer.
Definition at line 334 of file bbtkAny.h.
References bbtk::any< TypeTraits >::contains_pointer(), bbtk::any< TypeTraits >::content, and bbtk::anyplaceholder::is_pointer().
Referenced by bbtk::any< TypeTraits >::contains_pointer().
00335 { 00336 return content->is_pointer() ; 00337 }
bool bbtk::any< TypeTraits >::contains | ( | TypeInfo | t | ) | [inline] |
Returns true iff the contained type is t.
Definition at line 340 of file bbtkAny.h.
References bbtk::any< TypeTraits >::contains(), and bbtk::any< TypeTraits >::type().
00341 { 00342 return ( (bool)(type() == t) ); 00343 }
bool bbtk::any< TypeTraits >::accepts | ( | ) | [inline] |
Returns true iff any of type ValueType can be held.
Definition at line 347 of file bbtkAny.h.
References bbtk::any< TypeTraits >::accepts().
Referenced by bbtk::any< TypeTraits >::accepts().
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 355 of file bbtkAny.h.
References bbtkDebugMessage, bbtkError, bbtk::any< TypeTraits >::content, bbtk::HumanTypeName(), and bbtk::any< TypeTraits >::type().
00356 { 00357 bbtkDebugMessage("Data",1, 00358 HumanTypeName<self >() 00359 <<" get<"<<HumanTypeName<ValueType>() 00360 <<"> with content : " 00361 <<HumanTypeName(type())<<std::endl); 00362 00363 if ( type() == typeid(ValueType) ) 00364 return static_cast< anyholder<ValueType> *>(content)->held; 00365 00366 bbtkError(HumanTypeName<self >() 00367 <<" get with type <" 00368 <<bbtk::HumanTypeName<ValueType>() 00369 <<"> does not match content type <" 00370 <<bbtk::HumanTypeName<>(type())<<">"); 00371 }
const ValueType* bbtk::any< TypeTraits >::getP | ( | ) | const [inline] |
Definition at line 374 of file bbtkAny.h.
References bbtkError, bbtk::any< TypeTraits >::content, bbtk::any< TypeTraits >::getP(), and bbtk::any< TypeTraits >::type().
Referenced by bbtk::any< TypeTraits >::getP().
00375 { 00376 if ( type() == typeid(ValueType) ) 00377 return &static_cast< anyholder<ValueType> *>(content)->held; 00378 00379 bbtkError(HumanTypeName<self >() 00380 <<" getP with type <" 00381 <<bbtk::HumanTypeName<ValueType>() 00382 <<"> does not match content type <" 00383 <<bbtk::HumanTypeName<>(type())<<">"); 00384 }
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 391 of file bbtkAny.h.
References bbtkDebugMessage, bbtkError, bbtk::any< TypeTraits >::content, bbtk::HumanTypeName(), bbtk::any< TypeTraits >::type(), and bbtk::any< TypeTraits >::unsafe_get().
Referenced by bbtk::any< TypeTraits >::unsafe_get().
00392 { 00393 bbtkDebugMessage("Data",1, 00394 HumanTypeName<self>() 00395 <<"::unsafe_get<" 00396 <<HumanTypeName<ValueType>()<<"> with content : " 00397 <<HumanTypeName(this->type()) 00398 <<std::endl); 00399 00400 // PrintValueIfIsPointer<ValueType>(static_cast< anyholder<ValueType> * >(content)->held); 00401 // int** i = (int**)(&static_cast< anyholder<ValueType> * >(content)->held); 00402 // std::cout << "v="<<*i<<std::endl; 00403 00404 if (content) 00405 return static_cast< anyholder<ValueType> * >(content)->held; 00406 00407 bbtkError(HumanTypeName<self >() 00408 <<"::usafe_get<" 00409 <<bbtk::HumanTypeName<ValueType>() 00410 <<"> : void content"); 00411 }
void* bbtk::any< TypeTraits >::get_pointer | ( | ) | const [inline] |
Definition at line 414 of file bbtkAny.h.
References bbtkDebugMessage, bbtk::any< TypeTraits >::content, bbtk::anyplaceholder::get_pointer(), bbtk::any< TypeTraits >::get_pointer(), bbtk::HumanTypeName(), and bbtk::any< TypeTraits >::type().
Referenced by bbtk::any< TypeTraits >::get_pointer().
00415 { 00416 void* p = content->get_pointer(); 00417 bbtkDebugMessage("Data",1, 00418 HumanTypeName<self>() 00419 <<"::get_pointer() with content <" 00420 <<HumanTypeName(this->type()) 00421 <<"> : result = " 00422 << p 00423 <<std::endl); 00424 return p; 00425 }
void* bbtk::any< TypeTraits >::get_pointer_to | ( | const std::type_info & | t | ) | const [inline] |
Definition at line 427 of file bbtkAny.h.
References bbtkDebugMessage, bbtk::any< TypeTraits >::content, bbtk::anyplaceholder::get_pointer_to(), bbtk::any< TypeTraits >::get_pointer_to(), bbtk::HumanTypeName(), and bbtk::any< TypeTraits >::type().
Referenced by bbtk::any< TypeTraits >::get_pointer_to().
00428 { 00429 void* p = content->get_pointer_to(t); 00430 bbtkDebugMessage("Data",1, 00431 HumanTypeName<self>() 00432 <<"::get_pointer_to("<<HumanTypeName(t) 00433 <<") with content <" 00434 <<HumanTypeName(this->type()) 00435 <<"> : result = " 00436 << p 00437 <<std::endl); 00438 return p; 00439 }
anyplaceholder* bbtk::any< TypeTraits >::content [private] |
content
Definition at line 443 of file bbtkAny.h.
Referenced by bbtk::any< TypeTraits >::any(), bbtk::any< TypeTraits >::contains_pointer(), bbtk::any< TypeTraits >::empty(), bbtk::any< TypeTraits >::get(), bbtk::any< TypeTraits >::get_pointer(), bbtk::any< TypeTraits >::get_pointer_to(), bbtk::any< TypeTraits >::getP(), bbtk::any< TypeTraits >::pointed_type(), bbtk::any< TypeTraits >::swap(), bbtk::any< TypeTraits >::type(), bbtk::any< TypeTraits >::unsafe_get(), and bbtk::any< TypeTraits >::~any().