/*========================================================================= Program: bbtk Module: $RCSfile: bbtkAny.h,v $ Language: C++ Date: $Date: 2008/01/22 15:02:00 $ Version: $Revision: 1.1 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See doc/license.txt or http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ /** *\file *\brief Defines the class any which can store any type of data (adapted from boost::any). */ #ifndef __BBTKANY_H_INCLUDED__ #define __BBTKANY_H_INCLUDED__ /* #include #include #include #include #include #include #include #include */ #include #include "bbtkRTTI.h" #include "bbtkMessageManager.h" #include "bbtkException.h" #ifdef _USE_BOOST_ #include #endif namespace bbtk { //========================================================= /// Abstract class used by the any class to store values class anyplaceholder { public: // structors virtual ~anyplaceholder() {} public: // queries /// returns the type of the held value virtual const std::type_info & type() const = 0; /// returns the type of the pointed held value virtual const std::type_info & pointed_type() const = 0; /// returns true iff the stored value is a pointer virtual bool is_pointer() const = 0; /// If the held value is a pointer then /// returns its value virtual void* get_pointer() const = 0; /// virtual void* get_pointer_to( const std::type_info& ) const = 0; virtual anyplaceholder * clone() const = 0; }; //========================================================= //========================================================= /// Concrete template class used by the any class to store values /// which stores a value of type ValueType template class anyholder : public anyplaceholder { public: // structors anyholder(const ValueType & value) : held(value) {} public: // queries virtual const std::type_info & type() const { return typeid(ValueType);} virtual bool is_pointer() const { return false; } virtual const std::type_info & pointed_type() const { return typeid(void); } virtual void* get_pointer() const { return 0; } virtual void* get_pointer_to( const std::type_info& ) const { return 0; } virtual anyplaceholder * clone() const { return new anyholder(held); } public: // representation ValueType held; }; //========================================================= //========================================================= /// specialization of anyholder for pointer types template class anyholder : public anyplaceholder { public: // structors anyholder(ValueType* const & value) : held(value) { } public: // queries virtual const std::type_info & type() const { return typeid(ValueType*); } virtual bool is_pointer() const { return true; } virtual const std::type_info & pointed_type() const { return typeid(ValueType); } virtual void* get_pointer() const { return (void*)held; } virtual void* get_pointer_to( const std::type_info& t) const { return run_time_up_or_down_cast(t,typeid(ValueType),held); } virtual anyplaceholder * clone() const { return new anyholder(held); } public: // representation ValueType* held; }; //========================================================= /** A magic class which can store any type of data which * is allowed by the template template parameter TypeTrait. * * The only requirement on TypeTrait 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 struct mytypes { static const bool value; }; * template const bool mytypes::value = false; * template <> const bool mytypes::value = true; * template <> const bool mytypes::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, * where thing is a TypeTrait whose value is true for all types. **/ template