kdlTypekitJntArray.cpp
Go to the documentation of this file.
00001 #include "kdlTypekit.hpp"
00002 
00003 namespace KDL{
00004   using namespace std;
00005   using namespace RTT;
00006 
00007 
00008     double& get_item(JntArray& v, int index)
00009     {
00010         if (index >= (int) (v.rows()) || index < 0)
00011             return RTT::internal::NA<double&>::na();
00012         return v(index);
00013     }
00014 
00015     double get_item_copy(const JntArray& v, int index)
00016     {
00017         if (index >= (int) (v.rows()) || index < 0)
00018             return RTT::internal::NA<double>::na();
00019         return v(index);
00020     }
00021 
00022 
00023     int get_size(const JntArray& v)
00024     {
00025         return v.rows();
00026     }
00027 
00028     struct JntArrayTypeInfo : public types::TemplateTypeInfo<JntArray,true>
00029 #if (RTT_VERSION_MAJOR*100+RTT_VERSION_MINOR) >= 206
00030                             , public MemberFactory
00031 #endif
00032     {
00033         JntArrayTypeInfo():TemplateTypeInfo<JntArray, true >("KDL.JntArray")
00034         {
00035         }
00036         
00037 #if (RTT_VERSION_MAJOR*100+RTT_VERSION_MINOR) >= 206
00038             bool installTypeInfoObject(TypeInfo* ti) {
00039                 // aquire a shared reference to the this object
00040                 boost::shared_ptr< JntArrayTypeInfo > mthis = boost::dynamic_pointer_cast<JntArrayTypeInfo >( this->getSharedPtr() );
00041                 assert(mthis);
00042                 // Allow base to install first
00043                 TemplateTypeInfo<JntArray,true>::installTypeInfoObject(ti);
00044                 // Install the factories for primitive types
00045                 ti->setMemberFactory( mthis );
00046                 // Don't delete us, we're memory-managed.
00047                 return false;
00048             }
00049 #endif
00050 
00054             bool composeType( base::DataSourceBase::shared_ptr dssource, base::DataSourceBase::shared_ptr dsresult) const {
00055                 const internal::DataSource<PropertyBag>* pb = dynamic_cast< const internal::DataSource<PropertyBag>* > (dssource.get() );
00056                 if ( !pb )
00057                     return false;
00058                 internal::AssignableDataSource<JntArray>::shared_ptr ads = boost::dynamic_pointer_cast< internal::AssignableDataSource<JntArray> >( dsresult );
00059                 if ( !ads )
00060                     return false;
00061 
00062                 PropertyBag const& source = pb->rvalue();
00063                 internal::AssignableDataSource<JntArray>::reference_t result = ads->set();
00064 
00065                 // take into account sequences:
00066                 base::PropertyBase* sz = source.find("Size");
00067                 if (!sz)
00068                     sz = source.find("size");
00069                 if (sz)
00070                 {
00071                     internal::DataSource<int>::shared_ptr sz_ds = internal::DataSource<int>::narrow(sz->getDataSource().get());
00072                     if (sz_ds)
00073                         result.resize( sz_ds->get() );
00074                 }
00075                 else
00076                 {
00077                     // no size found, inform parent of number of elements to come:
00078                     result.resize( source.size() );
00079                 }
00080                 // recurse into items of this sequence:
00081                 PropertyBag target( source.getType() );
00082                 PropertyBag decomp;
00083                 internal::ReferenceDataSource<JntArray> rds(result);
00084                 rds.ref(); // prevent dealloc.
00085                 // we compose each item in this sequence and then update result with target's result.
00086                 // 1. each child is composed into target (this is a recursive thing)
00087                 // 2. we decompose result one-level deep and 'refresh' it with the composed children of step 1.
00088                 if ( composePropertyBag(source, target) && typeDecomposition( &rds, decomp, false) && ( decomp.getType() == target.getType() ) && refreshProperties(decomp, target, true) ) {
00089                     assert(result.rows() == source.size());
00090                     assert(source.size() == target.size());
00091                     assert(source.size() == decomp.size());
00092                     ads->updated();
00093                     Logger::log() <<Logger::Debug<<"Successfuly composed type from "<< source.getType() <<Logger::endl;
00094                     return true;
00095                 }
00096                 return false;
00097             }
00098 
00102             base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
00103             {
00104                 return base::DataSourceBase::shared_ptr();
00105             }
00106 
00107 
00108         bool resize(base::DataSourceBase::shared_ptr arg, int size) const
00109         {
00110             if (arg->isAssignable()) {
00111                  RTT::internal::AssignableDataSource<JntArray>::shared_ptr asarg = RTT::internal::AssignableDataSource<JntArray>::narrow( arg.get() );
00112                 asarg->set().resize( size );
00113                 asarg->updated();
00114                 return true;
00115             }
00116             return false;
00117         }
00118 
00119         virtual std::vector<std::string> getMemberNames() const {
00120             // only discover the parts of this struct:
00121             std::vector<std::string> result;
00122             result.push_back("size");
00123             result.push_back("capacity");
00124             return result;
00125         }
00126 
00127         base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string& name) const {
00128             // the only thing we do is to check for an integer in name, otherwise, assume a part (size) is accessed:
00129             try {
00130                 unsigned int indx = boost::lexical_cast<unsigned int>(name);
00131                 // @todo could also return a direct reference to item indx using another DS type that respects updated().
00132                 return getMember( item, new RTT::internal::ConstantDataSource<int>(indx));
00133             } catch(...) {}
00134 
00135             return getMember( item, new RTT::internal::ConstantDataSource<std::string>(name) );
00136         }
00137 
00138         base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item,
00139                                                    base::DataSourceBase::shared_ptr id) const {
00140             // discover if user gave us a part name or index:
00141              RTT::internal::DataSource<int>::shared_ptr id_indx = RTT::internal::DataSource<int>::narrow( RTT::internal::DataSourceTypeInfo<int>::getTypeInfo()->convert(id).get() );
00142              RTT::internal::DataSource<string>::shared_ptr id_name = RTT::internal::DataSource<string>::narrow( id.get() );
00143             if ( id_name ) {
00144                 if ( id_name->get() == "size" || id_name->get() == "capacity") {
00145                     try {
00146                         return RTT::internal::newFunctorDataSource(&get_size, RTT::internal::GenerateDataSource()(item.get()) );
00147                     } catch(...) {}
00148                 }
00149             }
00150 
00151             if ( id_indx ) {
00152                 try {
00153                     if ( item->isAssignable() )
00154                         return RTT::internal::newFunctorDataSource(&get_item, RTT::internal::GenerateDataSource()(item.get(), id_indx.get() ) );
00155                     else
00156                         return RTT::internal::newFunctorDataSource(&get_item_copy, RTT::internal::GenerateDataSource()(item.get(), id_indx.get() ) );
00157                 } catch(...) {}
00158             }
00159             if (id_name) {
00160                 log(Error) << "JntArrayTypeInfo: No such member : " << id_name->get() << endlog();
00161             }
00162             if (id_indx) {
00163                 log(Error) << "JntArrayTypeInfo: Invalid index : " << id_indx->get() <<":"<< id_indx->getTypeName() << endlog();
00164             }
00165             if ( !id_name && ! id_indx)
00166                 log(Error) << "JntArrayTypeInfo: Not a member or index : " << id <<":"<< id->getTypeName() << endlog();
00167             return base::DataSourceBase::shared_ptr();
00168         }
00169 
00170     };
00171     void loadJntArrayTypes(){
00172         RTT::types::Types()->addType( new JntArrayTypeInfo() );
00173         RTT::types::Types()->addType( new SequenceTypeInfo<std::vector< JntArray > >("KDL.JntArray[]") );
00174   };
00175 }  


kdl_typekit
Author(s): Steven Bellens, Ruben Smits
autogenerated on Mon Oct 6 2014 07:21:52