TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/host/ofx/property/OfxhPropertyTemplate.cpp
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 
00030 #include "OfxhPropertyTemplate.hpp"
00031 #include "OfxhGetHook.hpp"
00032 #include "OfxhNotifyHook.hpp"
00033 
00034 #include <tuttle/host/ofx/OfxhCore.hpp>
00035 #include <tuttle/host/serialization.hpp>
00036 
00037 #include <ofxCore.h>
00038 #include <ofxImageEffect.h>
00039 
00040 #include <boost/serialization/string.hpp>
00041 #include <boost/serialization/vector.hpp>
00042 
00043 #include <iostream>
00044 #include <cstring>
00045 
00046 //#define DEBUG_PROPERTIES true
00047 
00048 namespace tuttle {
00049 namespace host {
00050 namespace ofx {
00051 namespace property {
00052 
00053 /// type holder, for integers
00054 int OfxhIntValue::kEmpty       = 0;
00055 double OfxhDoubleValue::kEmpty = 0;
00056 void* OfxhPointerValue::kEmpty = 0;
00057 std::string OfxhStringValue::kEmpty;
00058 
00059 inline int castToAPIType( int i )
00060 {
00061         return i;
00062 }
00063 
00064 inline void* castToAPIType( void* v )
00065 {
00066         return v;
00067 }
00068 
00069 inline double castToAPIType( double d )
00070 {
00071         return d;
00072 }
00073 
00074 inline const char* castToAPIType( const std::string& s )
00075 {
00076         return s.c_str();
00077 }
00078 
00079 template<class T>
00080 OfxhPropertyTemplate<T>::OfxhPropertyTemplate()
00081         : OfxhProperty( "", T::typeCode, 0, false )
00082 {}
00083 
00084 template<class T>
00085 OfxhPropertyTemplate<T>::OfxhPropertyTemplate( const std::string& name,
00086                                                size_t             dimension,
00087                                                bool               pluginReadOnly,
00088                                                APIType            defaultValue )
00089         : OfxhProperty( name, T::typeCode, dimension, pluginReadOnly )
00090 {
00091         if( dimension != _value.size() )
00092         {
00093                 _value.resize( dimension );
00094                 _defaultValue.resize( dimension );
00095         }
00096 
00097         if( dimension )
00098         {
00099                 for( size_t i = 0; i < dimension; ++i )
00100                 {
00101                         _defaultValue[i] = defaultValue;
00102                         _value[i]        = defaultValue;
00103                 }
00104         }
00105 }
00106 
00107 template<class T>
00108 OfxhPropertyTemplate<T>::OfxhPropertyTemplate( const OfxhPropertyTemplate<T>& pt )
00109         : OfxhProperty( pt )
00110         , _value( pt._value )
00111         , _defaultValue( pt._defaultValue )
00112 {}
00113 
00114 #if defined( WINDOWS ) && !defined( __GNUC__ )
00115 #pragma warning( disable : 4181 )
00116 #endif
00117 /// get one value
00118 
00119 template<class T>
00120 typename T::ReturnType OfxhPropertyTemplate<T>::getValue( int index ) const OFX_EXCEPTION_SPEC
00121 {
00122         if( _getHook )
00123         {
00124                 return _getHook->getProperty<T >( _name, index );
00125         }
00126         else
00127         {
00128                 return getValueRaw( index );
00129         }
00130 }
00131 
00132 template<class T>
00133 typename T::ReturnType OfxhPropertyTemplate<T>::getConstlessValue( int index ) const OFX_EXCEPTION_SPEC
00134 {
00135         if( _getHook )
00136         {
00137                 return _getHook->getProperty<T >( _name, index );
00138         }
00139         else
00140         {
00141                 return getConstlessValueRaw( index );
00142         }
00143 }
00144 
00145 #if defined( WINDOWS ) && !defined( __GNUC__ )
00146 #pragma warning( default : 4181 )
00147 #endif
00148 // get multiple values
00149 
00150 template<class T>
00151 void OfxhPropertyTemplate<T>::getValueN( typename T::APIType* values, int count ) const OFX_EXCEPTION_SPEC
00152 {
00153         if( _getHook )
00154         {
00155                 _getHook->getPropertyN<T >( _name, values, count );
00156         }
00157         else
00158         {
00159                 getValueNRaw( values, count );
00160         }
00161 }
00162 
00163 // get multiple values
00164 
00165 template<>
00166 void OfxhPropertyTemplate<OfxhStringValue>::getValueN( OfxhStringValue::APIType* values, int count ) const OFX_EXCEPTION_SPEC
00167 {
00168         if( _getHook )
00169         {
00170                 for( int i = 0; i < count; ++i )
00171                 {
00172                         values[i] = castToAPIType( _getHook->getStringProperty( _name, i ) );
00173                 }
00174         }
00175         else
00176         {
00177                 getValueNRaw( values, count );
00178         }
00179 }
00180 
00181 #if defined( WINDOWS ) && !defined( __GNUC__ )
00182 #pragma warning( disable : 4181 )
00183 #endif
00184 
00185 /**
00186  * get one value, without going through the getHook
00187  */
00188 template<class T>
00189 typename T::ReturnType OfxhPropertyTemplate<T>::getValueRaw( int index ) const OFX_EXCEPTION_SPEC
00190 {
00191         if( index < 0 || ( (size_t) index >= _value.size() ) )
00192         {
00193                 BOOST_THROW_EXCEPTION( OfxhException( kOfxStatErrBadIndex ) );
00194         }
00195         return _value[index];
00196 }
00197 
00198 template<class T>
00199 typename T::ReturnType OfxhPropertyTemplate<T>::getConstlessValueRaw( int index ) const OFX_EXCEPTION_SPEC
00200 {
00201         if( index < 0 || ( (size_t) index >= _value.size() ) )
00202         {
00203                 BOOST_THROW_EXCEPTION( OfxhException( kOfxStatErrBadIndex ) );
00204         }
00205         return _value[index];
00206 }
00207 
00208 #if defined( WINDOWS ) && !defined( __GNUC__ )
00209 #pragma warning( default : 4181 )
00210 #endif
00211 
00212 /**
00213  * get multiple values, without going through the getHook
00214  */
00215 template<class T>
00216 void OfxhPropertyTemplate<T>::getValueNRaw( APIType* value, const int count ) const OFX_EXCEPTION_SPEC
00217 {
00218         std::size_t size = count;
00219 
00220         if( size > _value.size() )
00221         {
00222                 size = _value.size();
00223         }
00224 
00225         for( std::size_t i = 0; i < size; ++i )
00226         {
00227                 value[i] = castToAPIType( _value[i] );
00228         }
00229 }
00230 
00231 /**
00232  * set one value
00233  */
00234 template<class T>
00235 void OfxhPropertyTemplate<T>::setValue( const typename T::Type& value, const int index, const EModifiedBy who ) OFX_EXCEPTION_SPEC
00236 {
00237         if( index < 0 || ( (size_t) index > _value.size() && _dimension ) )
00238         {
00239                 BOOST_THROW_EXCEPTION( OfxhException( kOfxStatErrBadIndex ) );
00240         }
00241 
00242         if( _value.size() <= static_cast<std::size_t>(index) )
00243         {
00244                 _value.resize( index + 1 );
00245         }
00246         _value[index] = value;
00247 
00248         //TUTTLE_TLOG( TUTTLE_INFO, "setValue: " << value << " at index: " << index );
00249 
00250         _modifiedBy = who;
00251 
00252         notify( true, index );
00253 }
00254 
00255 /**
00256  * set multiple values
00257  */
00258 template<class T>
00259 void OfxhPropertyTemplate<T>::setValueN( const typename T::APIType* value, const int count, const EModifiedBy who ) OFX_EXCEPTION_SPEC
00260 {
00261         if( _dimension && ( static_cast<std::size_t>(count) > _value.size() ) )
00262         {
00263                 BOOST_THROW_EXCEPTION( OfxhException( kOfxStatErrBadIndex ) );
00264         }
00265         if( _value.size() != static_cast<std::size_t>(count) )
00266         {
00267                 _value.resize( count );
00268         }
00269         for( int i = 0; i < count; ++i )
00270         {
00271                 _value[i] = value[i];
00272         }
00273 
00274         _modifiedBy = who;
00275 
00276         notify( false, count );
00277 }
00278 
00279 /**
00280  * get the dimension of the property
00281  */
00282 template <class T>
00283 std::size_t OfxhPropertyTemplate<T>::getDimension() const OFX_EXCEPTION_SPEC
00284 {
00285         if( _dimension != 0 )
00286         {
00287                 return _dimension;
00288         }
00289         else
00290         {
00291                 // code to get it from the hook
00292                 if( _getHook )
00293                 {
00294                         return _getHook->getDimension( _name );
00295                 }
00296                 else
00297                 {
00298                         return _value.size();
00299                 }
00300         }
00301 }
00302 
00303 template <class T>
00304 void OfxhPropertyTemplate<T>::reset() OFX_EXCEPTION_SPEC
00305 {
00306         if( _getHook )
00307         {
00308                 _getHook->reset( _name );
00309                 std::size_t dim = getDimension();
00310 
00311                 if( !isFixedSize() )
00312                 {
00313                         _value.resize( dim );
00314                 }
00315                 for( std::size_t i = 0; i < dim; ++i )
00316                 {
00317                         _value[i] = _getHook->getProperty<T >( _name, i );
00318                 }
00319         }
00320         else
00321         {
00322                 if( isFixedSize() )
00323                 {
00324                         _value = _defaultValue;
00325                 }
00326                 else
00327                 {
00328                         _value.resize( 0 );
00329                 }
00330 
00331                 // now notify on a reset
00332                 notify( false, _dimension );
00333         }
00334         _modifiedBy = eModifiedByHost;
00335 }
00336 
00337 // only compile this templates list
00338 template class OfxhPropertyTemplate<OfxhIntValue>;
00339 template class OfxhPropertyTemplate<OfxhDoubleValue>;
00340 template class OfxhPropertyTemplate<OfxhStringValue>;
00341 template class OfxhPropertyTemplate<OfxhPointerValue>;
00342 
00343 }
00344 }
00345 }
00346 }
00347 
00348 BOOST_CLASS_EXPORT_IMPLEMENT( tuttle::host::ofx::property::Int )
00349 BOOST_CLASS_EXPORT_IMPLEMENT( tuttle::host::ofx::property::Double )
00350 BOOST_CLASS_EXPORT_IMPLEMENT( tuttle::host::ofx::property::Pointer )
00351 BOOST_CLASS_EXPORT_IMPLEMENT( tuttle::host::ofx::property::String )
00352 //BOOST_CLASS_EXPORT( tuttle::host::ofx::property::Int )
00353 //BOOST_CLASS_EXPORT( tuttle::host::ofx::property::Double )
00354 //BOOST_CLASS_EXPORT( tuttle::host::ofx::property::Pointer )
00355 //BOOST_CLASS_EXPORT( tuttle::host::ofx::property::String )
00356