ISIS Core Library 0.7.2 (api 3.0.0)

/scr/tee1/isis/lib/Core/CoreUtils/generic_value.hpp

Go to the documentation of this file.
00001 /*
00002     <one line to give the program's name and a brief idea of what it does.>
00003     Copyright (C) <year>  <name of author>
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 */
00019 
00020 #ifndef GENERIC_TYPE_HPP
00021 #define GENERIC_TYPE_HPP
00022 
00023 #include <stdexcept>
00024 #include <cstdlib>
00025 #include "log.hpp"
00026 
00027 
00028 namespace isis
00029 {
00030 namespace data {template<typename TYPE> class ValueArray;}
00031 namespace util
00032 {
00033 template<typename TYPE> class Value;
00034 
00035 namespace _internal
00036 {
00037 
00038 class GenericValue
00039 {
00040 protected:
00041     template<typename T> T &m_cast_to() {
00042         LOG_IF( getTypeID() != T::staticID, Debug, error ) << "using " << getTypeName() << " at " << this << " as " << T::staticName() << " aborting ...";
00043         assert( getTypeID() == T::staticID );
00044         //      return *dynamic_cast<T *>( this );// @todo Mac doesn't like that (http://www.cocoabuilder.com/archive/xcode/247376-rtti-dynamic-cast-across-shared-module-boundaries.html)
00045         return *( reinterpret_cast<T *>( this ) );
00046     }
00047     template<typename T> const T &m_cast_to()const {
00048         LOG_IF( getTypeID() != T::staticID, Debug, error ) << "using " << getTypeName() << " at " << this << " as " << T::staticName() << " aborting ...";
00049         assert( getTypeID() == T::staticID );
00050         //      return *dynamic_cast<const T *>( this );// @todo Mac doesn't like that (http://www.cocoabuilder.com/archive/xcode/247376-rtti-dynamic-cast-across-shared-module-boundaries.html)
00051         return *( reinterpret_cast<const T *>( this ) );
00052     }
00053 
00054 public:
00056     virtual std::string toString( bool labeled = false )const = 0;
00057 
00059     virtual std::string getTypeName()const = 0;
00060 
00062     virtual unsigned short getTypeID()const = 0;
00063 
00065     virtual bool isFloat()const = 0;
00066 
00068     virtual bool isInteger()const = 0;
00069 
00071     bool isSameType( const GenericValue &second )const;
00072     virtual ~GenericValue() {}
00073 };
00074 
00083 template<typename TYPE_TYPE> class GenericReference: protected boost::scoped_ptr<TYPE_TYPE>
00084 {
00085     template<typename TT> friend class data::ValueArray; //allow Value and ValueArray to use the protected contructor below
00086     template<typename TT> friend class Value;
00087 protected:
00088     //dont use this directly
00089     GenericReference( TYPE_TYPE *t ): boost::scoped_ptr<TYPE_TYPE>( t ) {}
00090 public:
00092     TYPE_TYPE *operator->() const {return boost::scoped_ptr<TYPE_TYPE>::operator->();}
00093     TYPE_TYPE &operator*() const {return boost::scoped_ptr<TYPE_TYPE>::operator*();}
00095     GenericReference() {}
00101     GenericReference( const GenericReference &src ): boost::scoped_ptr<TYPE_TYPE>( NULL ) {
00102         operator=( src );
00103     }
00104     GenericReference( const TYPE_TYPE &src ): boost::scoped_ptr<TYPE_TYPE>( NULL ) {
00105         operator=( src );
00106     }
00114     GenericReference<TYPE_TYPE>& operator=( const GenericReference<TYPE_TYPE> &src ) {
00115         boost::scoped_ptr<TYPE_TYPE>::reset( src.isEmpty() ? 0 : src->clone() );
00116         return *this;
00117     }
00123     GenericReference<TYPE_TYPE>& operator=( const TYPE_TYPE &src ) {
00124         boost::scoped_ptr<TYPE_TYPE>::reset( src.clone() );
00125         return *this;
00126     }
00128     bool isEmpty()const {
00129         return boost::scoped_ptr<TYPE_TYPE>::get() == NULL;
00130     }
00131     const std::string toString( bool label = false )const {
00132         if ( isEmpty() )
00133             return std::string( "\xd8" ); //ASCII code empty set
00134         else
00135             return this->get()->toString( label );
00136     }
00137 };
00138 
00139 }
00140 }
00141 }
00142 #endif // GENERIC_TYPE_HPP