TuttleOFX  1
OfxhClipImageSet.cpp
Go to the documentation of this file.
00001 #include "OfxhClipImageSet.hpp"
00002 
00003 #include <tuttle/host/ofx/OfxhImageEffectNodeDescriptor.hpp>
00004 
00005 #include <boost/algorithm/string/join.hpp>
00006 #include <boost/algorithm/string/predicate.hpp>
00007 
00008 namespace tuttle {
00009 namespace host {
00010 namespace ofx {
00011 namespace attribute {
00012 
00013 OfxhClipImageSet::OfxhClipImageSet()
00014         : _clipPrefsDirty( true )
00015 {}
00016 
00017 OfxhClipImageSet::OfxhClipImageSet( const OfxhClipImageSet& other )
00018         : _clipsByOrder( other._clipsByOrder.clone() )
00019 {
00020         initMapFromList();
00021 }
00022 
00023 void OfxhClipImageSet::initMapFromList()
00024 {
00025         for( ClipImageVector::iterator it = _clipsByOrder.begin(), itEnd = _clipsByOrder.end();
00026              it != itEnd;
00027              ++it )
00028         {
00029                 _clipImages[it->getName()] = &( *it );
00030         }
00031 }
00032 
00033 OfxhClipImageSet::~OfxhClipImageSet()
00034 {}
00035 
00036 bool OfxhClipImageSet::operator==( const This& other ) const
00037 {
00038         return _clipsByOrder == other._clipsByOrder;
00039 }
00040 
00041 void OfxhClipImageSet::copyClipsValues( const OfxhClipImageSet& other )
00042 {
00043         if( _clipsByOrder.size() != other._clipsByOrder.size() )
00044         {
00045                 BOOST_THROW_EXCEPTION( exception::Bug()
00046                     << exception::dev( "You try to copy clips values, but the two lists are not identical." ) );
00047         }
00048 
00049         ClipImageVector::const_iterator oit = other._clipsByOrder.begin(), oitEnd = other._clipsByOrder.end();
00050         for( ClipImageVector::iterator it = _clipsByOrder.begin(), itEnd = _clipsByOrder.end();
00051              it != itEnd && oit != oitEnd;
00052              ++it, ++oit )
00053         {
00054                 OfxhClipImage& c        = *it;
00055                 const OfxhClipImage& oc = *oit;
00056                 if( c.getName() != oc.getName() )
00057                 {
00058                         BOOST_THROW_EXCEPTION( exception::Bug()
00059                             << exception::dev( "You try to copy clips values, but it is not the same clips in the two lists." ) );
00060                 }
00061                 c.copyValues( oc );
00062         }
00063 }
00064 
00065 void OfxhClipImageSet::populateClips( const imageEffect::OfxhImageEffectNodeDescriptor& descriptor ) OFX_EXCEPTION_SPEC
00066 {
00067         const imageEffect::OfxhImageEffectNodeDescriptor::ClipImageDescriptorVector& clips = descriptor.getClipsByOrder();
00068 
00069         _clipsByOrder.reserve( clips.size() );
00070         /// @todo tuttle don't manipulate clip here, delegate to ClipInstanceSet
00071         for( imageEffect::OfxhImageEffectNodeDescriptor::ClipImageDescriptorVector::const_iterator it = clips.begin(), itEnd = clips.end();
00072              it != itEnd;
00073              ++it )
00074         {
00075                 const std::string& name = it->getName();
00076                 // foreach clip descriptor make a ClipImageInstance
00077                 OfxhClipImage* instance = newClipImage( *it ); //( this, *it, counter );
00078                 if( !instance )
00079                         BOOST_THROW_EXCEPTION( exception::Bug()
00080                             << exception::dev( "Error on ClipImage creation." ) );
00081 
00082                 _clipsByOrder.push_back( instance );
00083                 _clipImages[name] = instance;
00084         }
00085 }
00086 
00087 OfxhClipImage& OfxhClipImageSet::getClip( const std::string& name, const bool acceptPartialName )
00088 {
00089         ClipImageMap::const_iterator it = _clipImages.find( name );
00090 
00091         if( it != _clipImages.end() )
00092                 return *it->second;
00093 
00094         std::vector<std::string> matches;
00095         OfxhClipImage* res = NULL;
00096         if( acceptPartialName )
00097         {
00098                 BOOST_FOREACH( ClipImageMap::value_type& p, _clipImages )
00099                 {
00100                         if( boost::algorithm::starts_with( p.first, name ) )
00101                         {
00102                                 matches.push_back( p.first );
00103                                 res = p.second;
00104                         }
00105                 }
00106                 if( matches.size() > 1 )
00107                 {
00108                         BOOST_THROW_EXCEPTION( exception::Value()
00109                                         << exception::user() + "Ambiguous partial clip name \"" + name + "\". Possible values are: " + boost::algorithm::join( matches, ", " ) + "."
00110                                 );
00111                 }
00112         }
00113 
00114         if( matches.size() == 0 )
00115         {
00116                 std::ostringstream ss;
00117                 ss << "Clip not found (" << name << ").\n";
00118                 ss << "List of existing clips [";
00119                 BOOST_FOREACH( const ClipImageMap::value_type& c, _clipImages )
00120                 {
00121                         ss << "(\"" << c.first << "\":\"" << c.second->getClipIdentifier() << "\"), ";
00122                 }
00123                 ss << "].\n";
00124                 BOOST_THROW_EXCEPTION( OfxhException( ss.str() ) );
00125         }
00126         return *res;
00127 }
00128 
00129 OfxhClipImage* OfxhClipImageSet::getClipPtr( const std::string& name, const bool acceptPartialName )
00130 {
00131         try
00132         {
00133                 return &this->getClip( name, acceptPartialName );
00134         }
00135         catch(...)
00136         {
00137                 return NULL;
00138         }
00139 }
00140 
00141 const OfxhClipImage* OfxhClipImageSet::getClipPtr( const std::string& name, const bool acceptPartialName ) const
00142 {
00143         try
00144         {
00145                 return &this->getClip( name, acceptPartialName );
00146         }
00147         catch(...)
00148         {
00149                 return NULL;
00150         }
00151 }
00152 
00153 
00154 }
00155 }
00156 }
00157 }
00158