TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/host/ofx/property/OfxhProperty.hpp
Go to the documentation of this file.
00001 /*
00002  * Software License :
00003  *
00004  * Copyright (c) 2007-2009, The Open Effects Association Ltd.  All Rights Reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * Redistributions of source code must retain the above copyright notice,
00010  *    this list of conditions and the following disclaimer.
00011  * Redistributions in binary form must reproduce the above copyright notice,
00012  *    this list of conditions and the following disclaimer in the documentation
00013  *    and/or other materials provided with the distribution.
00014  * Neither the name The Open Effects Association Ltd, nor the names of its
00015  *    contributors may be used to endorse or promote products derived from this
00016  *    software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
00022  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00025  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00027  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 #ifndef _TUTTLE_HOST_OFX_PROPERTY_HPP_
00030 #define _TUTTLE_HOST_OFX_PROPERTY_HPP_
00031 
00032 #include <tuttle/host/ofx/OfxhCore.hpp>
00033 #include <tuttle/host/ofx/OfxhUtilities.hpp>
00034 #include <tuttle/host/ofx/OfxhException.hpp>
00035 #include <tuttle/host/exceptions.hpp>
00036 
00037 #include <boost/type_traits/is_virtual_base_of.hpp>
00038 #include <boost/serialization/extended_type_info.hpp>
00039 #include <boost/serialization/serialization.hpp>
00040 #include <boost/serialization/nvp.hpp>
00041 #include <boost/serialization/export.hpp>
00042 #include <boost/serialization/string.hpp>
00043 #include <boost/lexical_cast.hpp>
00044 
00045 #include <string>
00046 #include <vector>
00047 #include <map>
00048 #include <algorithm>
00049 #include <sstream>
00050 #include <stdexcept>
00051 
00052 namespace tuttle {
00053 namespace host {
00054 namespace ofx {
00055 namespace property {
00056 
00057 // forward declarations
00058 class OfxhSet;
00059 class OfxhGetHook;
00060 class OfxhNotifyHook;
00061 
00062 /// type of a property
00063 enum EPropType
00064 {
00065         ePropTypeNone    = -1,
00066         ePropTypeInt     = 0,
00067         ePropTypeDouble  = 1,
00068         ePropTypeString  = 2,
00069         ePropTypePointer = 3
00070 };
00071 
00072 inline std::string mapTypeEnumToString( const EPropType& e )
00073 {
00074         switch( e )
00075         {
00076                 case ePropTypeNone:
00077                         return "none";
00078                 case ePropTypeInt:
00079                         return "int";
00080                 case ePropTypeDouble:
00081                         return "double";
00082                 case ePropTypeString:
00083                         return "string";
00084                 case ePropTypePointer:
00085                         return "pointer";
00086         }
00087         BOOST_THROW_EXCEPTION( OfxhException( kOfxStatErrValue ) );
00088         return "";
00089 }
00090 
00091 enum EModifiedBy
00092 {
00093         eModifiedByHost = 0,
00094         eModifiedByPlugin
00095 };
00096 
00097 /// base class for all properties
00098 class OfxhProperty : private boost::noncopyable
00099 {
00100 public:
00101         typedef OfxhProperty This;
00102 
00103 protected:
00104         std::string _name;                         ///< name of this property
00105         EPropType _type;                           ///< type of this property
00106         std::size_t _dimension;                    ///< the fixed dimension of this property
00107         bool _pluginReadOnly;                      ///< set is forbidden through suite: value may still change between getValue() calls
00108         EModifiedBy _modifiedBy;                   ///< who set this property most recently
00109         std::vector<OfxhNotifyHook*> _notifyHooks; ///< hooks to call whenever the property is set
00110         OfxhGetHook* _getHook;                     ///< if we are not storing props locally, they are stored via fetching from here
00111 
00112         friend class OfxhSet;
00113 
00114 public:
00115         OfxhProperty( const std::string& name,
00116                       EPropType          type,
00117                       std::size_t        dimension = 1,
00118                       bool               pluginReadOnly = false );
00119 
00120         OfxhProperty( const This& other );
00121 
00122         virtual ~OfxhProperty() = 0;
00123 
00124         virtual bool operator==( const This& other ) const
00125         {
00126                 if( _name != other._name )
00127                 {
00128                         //TUTTLE_TLOG( TUTTLE_INFO, "OfxhProperty::operator== not same name : " << _name << " != " << other._name );
00129                         return false;
00130                 }
00131                 if( _type != other._type )
00132                 {
00133                         //TUTTLE_TLOG( TUTTLE_INFO, "OfxhProperty::operator== not same type : " << _type << " != " << other._type );
00134                         return false;
00135                 }
00136                 if( _dimension != other._dimension )
00137                 {
00138                         //TUTTLE_TLOG( TUTTLE_INFO, "OfxhProperty::operator== not same size : " << _dimension << " != " << other._dimension );
00139                         return false;
00140                 }
00141                 if( _pluginReadOnly != other._pluginReadOnly )
00142                 {
00143                         //TUTTLE_TLOG( TUTTLE_INFO, "OfxhProperty::operator== not sale read only : " << _pluginReadOnly << " != " << other._pluginReadOnly );
00144                         return false;
00145                 }
00146                 return true;
00147         }
00148 
00149         bool operator!=( const This& other ) const { return !This::operator==( other ); }
00150 
00151         virtual void copyValues( const This& other ) = 0;
00152 
00153         /// is it read only?
00154         bool getPluginReadOnly() const { return _pluginReadOnly; }
00155 
00156         /// change the state of readonlyness
00157         void setPluginReadOnly( bool v ) { _pluginReadOnly = v; }
00158 
00159         void        setModifiedBy( const EModifiedBy who ) { _modifiedBy = who; }
00160         EModifiedBy getModifiedBy() const                  { return _modifiedBy; }
00161 
00162         /// override this to return a clone of the property
00163         virtual OfxhProperty* clone() const = 0;
00164 
00165         /// get the name of this property
00166         const std::string& getName() const
00167         {
00168                 return _name;
00169         }
00170 
00171         /// get the type of this property
00172         EPropType getType() const
00173         {
00174                 return _type;
00175         }
00176 #ifndef SWIG
00177         /// add a notify hook
00178         void addNotifyHook( OfxhNotifyHook* hook )
00179         {
00180                 _notifyHooks.push_back( hook );
00181         }
00182 
00183         /// set the get hook
00184         void setGetHook( OfxhGetHook* hook )
00185         {
00186                 _getHook = hook;
00187         }
00188         /// call notify on the contained notify hooks
00189         void notify( bool single, int indexOrN );
00190 #endif
00191 
00192         // get the current dimension of this property
00193         virtual std::size_t getDimension() const = 0;
00194 
00195         /// get the fixed dimension of this property
00196         std::size_t getFixedDimension() const
00197         {
00198                 return _dimension;
00199         }
00200 
00201         /// are we a fixed dim property
00202         bool isFixedSize() const
00203         {
00204                 return _dimension != 0;
00205         }
00206 
00207         /// reset this property to the default
00208         virtual void reset() = 0;
00209 
00210         /// get a string representing the value of this property at element nth
00211         virtual std::string getStringValueAt( int index = 0 ) const = 0;
00212 
00213         std::vector<std::string> getStringValues() const;
00214 
00215         /// get a string representing all the values of this property
00216         std::string getStringValue() const;
00217 
00218 private:
00219         friend class boost::serialization::access;
00220         template<class Archive>
00221         void serialize( Archive& ar, const unsigned int version )
00222         {
00223                 ar& BOOST_SERIALIZATION_NVP( _name );
00224                 ar& BOOST_SERIALIZATION_NVP( _type );
00225                 ar& BOOST_SERIALIZATION_NVP( _dimension );
00226                 ar& BOOST_SERIALIZATION_NVP( _pluginReadOnly );
00227         }
00228 
00229 };
00230 
00231 #ifndef SWIG
00232 inline OfxhProperty* new_clone( const OfxhProperty& p )
00233 {
00234         return p.clone();
00235 }
00236 
00237 #endif
00238 
00239 }
00240 }
00241 }
00242 }
00243 
00244 #endif