SequenceTypeInfoBase.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: The SourceWorks  Tue Sep 7 00:55:18 CEST 2010  SequenceTypeInfo.hpp
00003 
00004                         SequenceTypeInfo.hpp -  description
00005                            -------------------
00006     begin                : Tue September 07 2010
00007     copyright            : (C) 2010 The SourceWorks
00008     email                : peter@thesourceworks.com
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 #ifndef ORO_SEQUENCE_TYPE_INFO_BASE_HPP
00040 #define ORO_SEQUENCE_TYPE_INFO_BASE_HPP
00041 
00042 #include "SequenceConstructor.hpp"
00043 #include "TemplateConstructor.hpp"
00044 #include "PropertyComposition.hpp"
00045 #include "VectorTemplateComposition.hpp"
00046 #include "PropertyDecomposition.hpp"
00047 #include "../internal/FusedFunctorDataSource.hpp"
00048 #include "../internal/DataSourceGenerator.hpp"
00049 #include <boost/lexical_cast.hpp>
00050 
00051 namespace RTT
00052 {
00053     namespace types
00054     {
00060         template<class T>
00061         int get_capacity(T const& cont)
00062         {
00063             return cont.capacity();
00064         }
00065 
00071         template<class T>
00072         int get_size(T const& cont)
00073         {
00074             return cont.size();
00075         }
00076 
00084         template<class T>
00085         typename T::reference get_container_item(T & cont, int index)
00086         {
00087             if (index >= (int) (cont.size()) || index < 0)
00088                 return internal::NA<typename T::reference>::na();
00089             return cont[index];
00090         }
00091 
00099         template<class T>
00100         typename T::value_type get_container_item_copy(const T & cont, int index)
00101         {
00102             if (index >= (int) (cont.size()) || index < 0)
00103                 return internal::NA<typename T::value_type>::na();
00104             return cont[index];
00105         }
00106 
00115         bool get_container_item(std::vector<bool> & cont, int index);
00116         bool get_container_item_copy(const std::vector<bool> & cont, int index);
00117 
00125         template<typename T>
00126         class SequenceTypeInfoBase
00127         {
00128         public:
00129             SequenceTypeInfoBase()
00130             {
00131             }
00132 
00133             bool installTypeInfoObject(TypeInfo* ti) {
00134                 ti->addConstructor( new SequenceBuilder<T>() );
00135                 ti->addConstructor( newConstructor( sequence_ctor<T>() ) );
00136                 ti->addConstructor( newConstructor( sequence_ctor2<T>() ) );
00137                 // Don't delete us, we're memory-managed.
00138                 return false;
00139             }
00140 
00141             ~SequenceTypeInfoBase() {}
00142 
00143             base::AttributeBase* buildVariable(std::string name,int size) const
00144             {
00145                 // if a sizehint is given
00146                 T t_init(size, typename T::value_type() );
00147 
00148                 return new Attribute<T>( name, new internal::UnboundDataSource<internal::ValueDataSource<T> >( t_init ) );
00149             }
00150 
00151             bool resize(base::DataSourceBase::shared_ptr arg, int size) const
00152             {
00153                 if (arg->isAssignable()) {
00154                     typename internal::AssignableDataSource<T>::shared_ptr asarg = internal::AssignableDataSource<T>::narrow( arg.get() );
00155                     asarg->set().resize( size );
00156                     asarg->updated();
00157                     return true;
00158                 }
00159                 return false;
00160             }
00161 
00165             bool composeType( base::DataSourceBase::shared_ptr dssource, base::DataSourceBase::shared_ptr dsresult) const {
00166                 const internal::DataSource<PropertyBag>* pb = dynamic_cast< const internal::DataSource<PropertyBag>* > (dssource.get() );
00167                 if ( !pb )
00168                     return false;
00169                 typename internal::AssignableDataSource<T>::shared_ptr ads = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( dsresult );
00170                 if ( !ads )
00171                     return false;
00172 
00173                 PropertyBag const& source = pb->rvalue();
00174                 typename internal::AssignableDataSource<T>::reference_t result = ads->set();
00175 
00176                 // recurse into items of this sequence:
00177                 PropertyBag target( source.getType() );
00178                 // we compose each item in this sequence and then update result with target's result.
00179                 // 1. each child is composed into target (this is a recursive thing using composeType() on each child)
00180                 // 2. we decompose result one-level deep and 
00181                 // 3. 'refresh' it with the composed children of step 1.
00182                 if ( composePropertyBag(source, target) && composeTemplateProperty(target, result ) ){
00183                     ads->updated();
00184                     Logger::log() <<Logger::Debug<<"Successfuly composed Sequence from "<< source.getType() <<Logger::endl;
00185                     return true;
00186                 } else 
00187                     Logger::log() <<Logger::Debug<<"Failed to composed Sequence from "<< source.getType() <<Logger::endl;
00188 
00189                 return false;
00190             }
00191 
00195             base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
00196             {
00197                 return base::DataSourceBase::shared_ptr();
00198             }
00199 
00200 
00201             std::vector<std::string> getMemberNames() const {
00202                 // only discover the parts of this struct:
00203                 std::vector<std::string> result;
00204                 result.push_back("size");
00205                 result.push_back("capacity");
00206                 return result;
00207             }
00208 
00209             base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string& name) const {
00210                 // the only thing we do is to check for an integer in name, otherwise, assume a part (size/capacity) is accessed:
00211                 try {
00212                     unsigned int indx = boost::lexical_cast<unsigned int>(name);
00213                     // @todo could also return a direct reference to item indx using another DS type that respects updated().
00214                     return getMember( item, new internal::ConstantDataSource<int>(indx));
00215                 } catch(...) {}
00216 
00217                 return getMember( item, new internal::ConstantDataSource<std::string>(name) );
00218             }
00219 
00220             base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item,
00221                                                              base::DataSourceBase::shared_ptr id) const {
00222                 // discover if user gave us a part name or index:
00223                 typename internal::DataSource<int>::shared_ptr id_indx = internal::DataSource<int>::narrow( internal::DataSourceTypeInfo<int>::getTypeInfo()->convert(id).get() );
00224                 typename internal::DataSource<std::string>::shared_ptr id_name = internal::DataSource<std::string>::narrow( id.get() );
00225                 if ( id_name ) {
00226                     if ( id_name->get() == "size" ) {
00227                         try {
00228                             return internal::newFunctorDataSource(&get_size<T>, internal::GenerateDataSource()(item.get()) );
00229                         } catch(...) {}
00230                     }
00231                     if ( id_name->get() == "capacity" ) {
00232                         try {
00233                             return internal::newFunctorDataSource(&get_capacity<T>, internal::GenerateDataSource()(item.get()) );
00234                         } catch(...) {}
00235                     }
00236                 }
00237 
00238                 if ( id_indx ) {
00239                     try {
00240                         if ( item->isAssignable() )
00241                                 return internal::newFunctorDataSource(&get_container_item<T>, internal::GenerateDataSource()(item.get(), id_indx.get() ) );
00242                             else
00243                                 return internal::newFunctorDataSource(&get_container_item_copy<T>, internal::GenerateDataSource()(item.get(), id_indx.get() ) );
00244                     } catch(...) {}
00245                 }
00246                 if (id_name) {
00247                     log(Error) << "SequenceTypeInfo: No such member : " << id_name->get() << endlog();
00248                 }
00249                 if (id_indx) {
00250                     log(Error) << "SequenceTypeInfo: Invalid index : " << id_indx->get() <<":"<< id_indx->getTypeName() << endlog();
00251                 }
00252                 if ( !id_name && ! id_indx)
00253                     log(Error) << "SequenceTypeInfo: Not a member or index : " << id <<":"<< id->getTypeName() << endlog();
00254                 return base::DataSourceBase::shared_ptr();
00255             }
00256         };
00257     }
00258 }
00259 
00260 #endif


rtt
Author(s): RTT Developers
autogenerated on Wed Aug 26 2015 16:15:53