ISIS Core Library 0.7.2 (api 3.0.0)

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

Go to the documentation of this file.
00001 //
00002 // C++ Interface: property
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 #ifndef ISISPROPERTY_HPP
00014 #define ISISPROPERTY_HPP
00015 
00016 #include <boost/shared_ptr.hpp>
00017 #include <map>
00018 #include "value.hpp"
00019 #include "log.hpp"
00020 
00021 namespace isis
00022 {
00023 namespace util
00024 {
00025 
00033 class PropertyValue: public ValueReference
00034 {
00035     bool m_needed;
00036     //prevent usage of ValueReference's dereferencing (the hooks below should be used) (use "*" if you really have to)
00037     ValueBase *operator->();
00038     const ValueBase *operator->()const;
00039 public:
00047     template<typename T> PropertyValue( const T &ref, bool _needed = false ):
00048         ValueReference( new Value<T>( ref ) ), m_needed( _needed ) {
00049         checkType<T>();
00050     }
00051     template<typename T> PropertyValue( const Value<T>& ref, bool _needed = false ):
00052         ValueReference( new Value<T>( ref ) ), m_needed( _needed ) {
00053         checkType<T>();
00054     }
00059     PropertyValue();
00061     bool &needed();
00063     bool isNeeded ()const;
00064 
00073     bool operator ==( const PropertyValue &second )const;
00083     bool operator !=( const PropertyValue &second )const;
00091     bool operator ==( const ValueBase &second )const;
00103     template<typename T> bool operator ==( const T &second )const {
00104         checkType<T>();
00105 
00106         if( isEmpty() ) {
00107             return false;
00108         }
00109 
00110         if ( get()->is<T>() ) { // If I'm of the same type as the comparator
00111             const T &cmp = get()->castTo<T>();
00112             return second == cmp; //compare our values
00113         } else if ( ! isEmpty() ) { // otherwise try to make me T and compare that
00114             LOG( Debug, info )
00115                     << *this << " is not " << Value<T>::staticName() << ", trying to convert.";
00116             ValueReference dst = ( **this ).copyByID( Value<T>::staticID );
00117 
00118             if ( !dst.isEmpty() )
00119                 return dst->castTo<T>() == second;
00120         }
00121 
00122         return false;
00123     }
00124 
00129     template<class T> T as()const {
00130         LOG_IF( !*this, Debug, error ) << "Doing as<" << util::Value<T>::staticName() <<  ">() on empty property, this will crash";
00131         return ( **this ).as<T>();
00132     }
00133 
00138     template<class T> bool is()const {
00139         LOG_IF( !*this, Debug, error ) << "Doing is<" << util::Value<T>::staticName() <<  ">() on empty property, this will crash";
00140         return ( **this ).is<T>();
00141     }
00142 
00147     std::string getTypeName()const;
00148 
00153     unsigned short getTypeID()const;
00154 
00159     template<class T> T &castTo()const {
00160         LOG_IF( !*this, Debug, error ) << "Casting empty property to " << util::MSubject( util::Value<T>::staticName() ) << ", this will crash";
00161         return ( **this ).castTo<T>();
00162     }
00163 };
00164 
00165 }
00166 }
00167 #endif
00168 
00169