ISIS Core Library 0.7.2 (api 3.0.0)

/scr/tee1/isis/lib/Core/CoreUtils/value_base.cpp

Go to the documentation of this file.
00001 //
00002 // C++ Implementation: type_base
00003 //
00004 // Description:
00005 //
00006 //
00007 // Author: Enrico Reimer<reimer@cbs.mpg.de>, (C) 2009
00008 //
00009 // Copyright: See COPYING file that comes with this distribution
00010 //
00011 //
00012 
00013 #include "value_base.hpp"
00014 #include "singletons.hpp"
00015 
00016 namespace isis
00017 {
00018 namespace util
00019 {
00021 namespace _internal
00022 {
00023 bool GenericValue::isSameType ( const GenericValue &second ) const
00024 {
00025     return getTypeID() == second.getTypeID();
00026 }
00027 }
00029 
00030 ValueBase::~ValueBase() {}
00031 
00032 
00033 const _internal::ValueConverterMap &ValueBase::converters()
00034 {
00035     static _internal::ValueConverterMap ret; //@todo not using class Singleton because ValueArrayConverterMap is hidden
00036     return ret;
00037 }
00038 
00039 const ValueBase::Converter &ValueBase::getConverterTo( unsigned short ID )const
00040 {
00041     const _internal::ValueConverterMap::const_iterator f1 = converters().find( getTypeID() );
00042     assert( f1 != converters().end() );
00043     const _internal::ValueConverterMap::mapped_type::const_iterator f2 = f1->second.find( ID );
00044     assert( f2 != f1->second.end() );
00045     return f2->second;
00046 }
00047 bool ValueBase::convert( const ValueBase &from, ValueBase &to )
00048 {
00049     const Converter &conv = from.getConverterTo( to.getTypeID() );
00050 
00051     if ( conv ) {
00052         switch ( conv->convert( from, to ) ) {
00053         case boost::numeric::cPosOverflow:
00054             LOG( Runtime, error ) << "Positive overflow when converting " << from.toString( true ) << " to " << to.getTypeName() << ".";
00055             break;
00056         case boost::numeric::cNegOverflow:
00057             LOG( Runtime, error ) << "Negative overflow when converting " << from.toString( true ) << " to " << to.getTypeName() << ".";
00058             break;
00059         case boost::numeric::cInRange:
00060             return true;
00061             break;
00062         }
00063     } else {
00064         LOG( Runtime, error )
00065                 << "I dont know any conversion from "
00066                 << MSubject( from.toString( true ) ) << " to " << MSubject( to.getTypeName() );
00067     }
00068 
00069     return false;
00070 }
00071 
00072 bool ValueBase::fitsInto( short unsigned int ID ) const
00073 {
00074     boost::scoped_ptr<ValueBase> to;
00075     const Converter &conv = getConverterTo( ID );
00076 
00077     if ( conv ) {
00078         return ( conv->generate( *this, to ) ==  boost::numeric::cInRange );
00079     } else {
00080         LOG( Runtime, warning )
00081                 << "I dont know any conversion from "
00082                 << MSubject( toString( true ) ) << " to " << MSubject( getTypeMap( true, false )[ID] );
00083         return false; // return an empty Reference
00084     }
00085 }
00086 
00087 ValueBase::Reference ValueBase::copyByID( short unsigned int ID ) const
00088 {
00089     boost::scoped_ptr<ValueBase> to;
00090     const Converter &conv = getConverterTo( ID );
00091 
00092     if ( conv ) {
00093         switch ( conv->generate( *this, to ) ) {
00094         case boost::numeric::cPosOverflow:
00095             LOG( Runtime, error ) << "Positive overflow when converting " << MSubject( toString( true ) ) << " to " << MSubject( getTypeMap( true, false )[ID] ) << ".";
00096             break;
00097         case boost::numeric::cNegOverflow:
00098             LOG( Runtime, error ) << "Negative overflow when converting " << MSubject( toString( true ) ) << " to " << MSubject( getTypeMap( true, false )[ID] ) << ".";
00099             break;
00100         case boost::numeric::cInRange:
00101             break;
00102         }
00103 
00104         return *to; // return the generated Value-Object - wrapping it into Reference
00105     } else {
00106         LOG( Runtime, error )
00107                 << "I dont know any conversion from "
00108                 << MSubject( toString( true ) ) << " to " << MSubject( getTypeMap( true, false )[ID] );
00109         return Reference(); // return an empty Reference
00110     }
00111 }
00112 
00113 }
00114 }