/*========================================================================= Program: bbtk Module: $RCSfile: bbtkAny.h,v $ Language: C++ Date: $Date: 2010/01/14 13:17:27 $ Version: $Revision: 1.8 $ =========================================================================*/ /* --------------------------------------------------------------------- * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale) * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux * * This software is governed by the CeCILL-B license under French law and * abiding by the rules of distribution of free software. You can use, * modify and/ or redistribute the software under the terms of the CeCILL-B * license as circulated by CEA, CNRS and INRIA at the following URL * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html * or in the file LICENSE.txt. * * As a counterpart to the access to the source code and rights to copy, * modify and redistribute granted by the license, users are provided only * with a limited warranty and the software's author, the holder of the * economic rights, and the successive licensors have only limited * liability. * * The fact that you are presently reading this means that you have had * knowledge of the CeCILL-B license and that you accept its terms. * ------------------------------------------------------------------------ */ /** *\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" #include 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