TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/host/ofx/attribute/OfxhParamSet.cpp
Go to the documentation of this file.
00001 #include "OfxhParamSet.hpp"
00002 
00003 #include <boost/functional/hash.hpp>
00004 
00005 #include <boost/algorithm/string/join.hpp>
00006 #include <boost/algorithm/string/predicate.hpp>
00007 #include <boost/foreach.hpp>
00008 
00009 
00010 namespace tuttle {
00011 namespace host {
00012 namespace ofx {
00013 namespace attribute {
00014 
00015 OfxhParamSet::OfxhParamSet()
00016 {}
00017 
00018 OfxhParamSet::OfxhParamSet( const OfxhParamSet& other )
00019 {
00020         operator=( other );
00021 }
00022 
00023 void OfxhParamSet::initMapFromList()
00024 {
00025         BOOST_FOREACH( OfxhParam& p, _paramVector )
00026         {
00027                 _params[p.getName()] = &p;
00028                 _paramsByScriptName[p.getScriptName()] = &p;
00029         }
00030 }
00031 
00032 OfxhParamSet::~OfxhParamSet()
00033 {}
00034 
00035 OfxhParamSet& OfxhParamSet::operator=( const OfxhParamSet& other )
00036 {
00037         _paramVector = other._paramVector.clone();
00038         initMapFromList();
00039         return *this;
00040 }
00041 
00042 void OfxhParamSet::copyParamsValues( const OfxhParamSet& other )
00043 {
00044         if( _paramVector.size() != other._paramVector.size() )
00045         {
00046                 BOOST_THROW_EXCEPTION( exception::Bug()
00047                     << exception::dev( "You try to copy parameters values, but the two lists are not identical." ) );
00048         }
00049 
00050         ParamVector::const_iterator oit = other._paramVector.begin(), oitEnd = other._paramVector.end();
00051         for( ParamVector::iterator it = _paramVector.begin(), itEnd = _paramVector.end();
00052              it != itEnd && oit != oitEnd;
00053              ++it, ++oit )
00054         {
00055                 OfxhParam& p        = *it;
00056                 const OfxhParam& op = *oit;
00057                 if( p.getName() != op.getName() )
00058                 {
00059                         BOOST_THROW_EXCEPTION( exception::Bug()
00060                             << exception::dev( "You try to copy parameters values, but it is not the same parameters in the two lists." ) );
00061                 }
00062                 p.copy( op );
00063         }
00064         initMapFromList();
00065 }
00066 
00067 std::size_t OfxhParamSet::getHashAtTime( const OfxTime time ) const
00068 {
00069         std::size_t seed = 0;
00070         BOOST_FOREACH( const OfxhParam& param, getParamVector() )
00071         {
00072                 //TUTTLE_TLOG_VAR( TUTTLE_INFO, param.getName() );
00073                 if( param.paramTypeHasData() && param.getEvaluateOnChange() )
00074                 {
00075                         boost::hash_combine( seed, param.getHashAtTime( time ) );
00076                 }
00077         }
00078         return seed;
00079 }
00080 
00081 //void OfxhParamSet::referenceParam( const std::string& name, OfxhParam* instance ) OFX_EXCEPTION_SPEC
00082 //{
00083         //      if( _allParams.find( name ) != _allParams.end() )
00084         //      {
00085         //              BOOST_THROW_EXCEPTION( OfxhException( kOfxStatErrExists, "Trying to reference a new parameter which already exists." ) );
00086         //      }
00087         //      _allParams[name] = instance;
00088 //}
00089 
00090 OfxhParam& OfxhParamSet::getParam( const std::string& name )
00091 {
00092         ParamMap::iterator it = _params.find( name );
00093         if( it == _params.end() )
00094         {
00095                 BOOST_THROW_EXCEPTION( exception::BadIndex()
00096                                 << exception::user() + "Param name \"" + name + "\" not found."
00097                         );
00098         }
00099         return *it->second;
00100 }
00101 
00102 OfxhParam& OfxhParamSet::getParamByScriptName( const std::string& scriptName, const bool acceptPartialName )
00103 {
00104         ParamMap::iterator it = _paramsByScriptName.find( scriptName );
00105         if( it != _paramsByScriptName.end() )
00106                 return *it->second;
00107 
00108         std::vector<std::string> matches;
00109         OfxhParam* res = NULL;
00110         if( acceptPartialName )
00111         {
00112                 BOOST_FOREACH( ParamMap::value_type& p, _paramsByScriptName )
00113                 {
00114                         if( boost::algorithm::starts_with( p.first, scriptName ) )
00115                         {
00116                                 matches.push_back( p.first );
00117                                 res = p.second;
00118                         }
00119                 }
00120                 if( matches.size() > 1 )
00121                 {
00122                         BOOST_THROW_EXCEPTION( exception::Value()
00123                                         << exception::user() + "Ambiguous partial param script name \"" + scriptName + "\". Possible values are: " + boost::algorithm::join( matches, ", " ) + "."
00124                                 );
00125                 }
00126         }
00127 
00128         if( matches.size() == 0 )
00129         {
00130                 BOOST_THROW_EXCEPTION( exception::Value()
00131                                 << exception::user() + "Param script name \"" + scriptName + "\" not found."
00132                         );
00133         }
00134         return *res;
00135 }
00136 OfxhParam* OfxhParamSet::getParamPtrByScriptName( const std::string& name, const bool acceptPartialName )
00137 {
00138         try
00139         {
00140                 return &this->getParamByScriptName( name, acceptPartialName );
00141         }
00142         catch(...)
00143         {
00144                 return NULL;
00145         }
00146 }
00147 const OfxhParam* OfxhParamSet::getParamPtrByScriptName( const std::string& name, const bool acceptPartialName ) const
00148 {
00149         try
00150         {
00151                 return &this->getParamByScriptName( name, acceptPartialName );
00152         }
00153         catch(...)
00154         {
00155                 return NULL;
00156         }
00157 }
00158 
00159 // get the param
00160 OfxhParam& OfxhParamSet::getParam( const std::size_t index )
00161 {
00162         if( index > _paramVector.size() )
00163                 BOOST_THROW_EXCEPTION( exception::BadIndex()
00164                                 << exception::user() + "Param not found, index out of range. (index=" + index + ", nb params=" + _paramVector.size() + ")"
00165                         );
00166 
00167         return _paramVector[index];
00168 }
00169 
00170 // get the param
00171 OfxhParam& OfxhParamSet::getChildParam( const std::size_t index )
00172 {
00173         if( index > _childParamVector.size() )
00174                 BOOST_THROW_EXCEPTION( exception::BadIndex()
00175                                 << exception::user() + "Param not found, index out of range. (index=" + index + ", nb params=" + _paramVector.size() + ")"
00176                         );
00177 
00178         return *_childParamVector[index];
00179 }
00180 
00181 
00182 void OfxhParamSet::addParam( OfxhParam* instance ) OFX_EXCEPTION_SPEC
00183 {
00184         if( _params.find( instance->getName() ) != _params.end() )
00185         {
00186                 BOOST_THROW_EXCEPTION( OfxhException( kOfxStatErrExists, "Trying to add a new parameter which already exists." ) );
00187         }
00188         _paramVector.push_back( instance );
00189         _params[instance->getName()] = instance;
00190         _paramsByScriptName[instance->getScriptName()] = instance;
00191         //      referenceParam( name, instance );
00192 }
00193 
00194 void OfxhParamSet::declareChildParam( OfxhParam& childParam )
00195 {
00196         childParam.setParamSetInstance( this );
00197         _childParamVector.push_back( &childParam );
00198         _childParams[childParam.getName()] = &childParam;
00199 }
00200 
00201 }
00202 }
00203 }
00204 }
00205