BoostArrayTypeInfo.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: The SourceWorks  Tue Sep 7 00:55:18 CEST 2010  BoostArrayTypeInfo.hpp
00003 
00004                         BoostArrayTypeInfo.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_TEMPLATE_BOOSTARRAY_INFO_HPP
00040 #define ORO_TEMPLATE_BOOSTARRAY_INFO_HPP
00041 
00042 #include "PrimitiveTypeInfo.hpp"
00043 #include "../internal/ArrayPartDataSource.hpp"
00044 #include "type_discovery.hpp"
00045 #include <boost/lexical_cast.hpp>
00046 #include <boost/array.hpp>
00047 #include "PropertyComposition.hpp"
00048 #include "PropertyDecomposition.hpp"
00049 #include "CompositionFactory.hpp"
00050 #include "MemberFactory.hpp"
00051 
00052 namespace RTT
00053 {
00054     namespace types
00055     {
00067         template<typename T, bool has_ostream = false>
00068         class BoostArrayTypeInfo :
00069             public PrimitiveTypeInfo<T, has_ostream>,
00070             public MemberFactory, public CompositionFactory
00071         {
00072         public:
00073             BoostArrayTypeInfo(std::string name) :
00074                 PrimitiveTypeInfo<T, has_ostream> (name)
00075             {
00076             }
00077 
00078         bool installTypeInfoObject(TypeInfo* ti) {
00079             // aquire a shared reference to the this object
00080             boost::shared_ptr< BoostArrayTypeInfo<T> > mthis = boost::dynamic_pointer_cast<BoostArrayTypeInfo<T> >( this->getSharedPtr() );
00081             // Allow base to install first
00082             PrimitiveTypeInfo<T,has_ostream>::installTypeInfoObject(ti);
00083             // Install the factories for primitive types
00084             ti->setMemberFactory( mthis );
00085             ti->setCompositionFactory( mthis );
00086 
00087             // Don't delete us, we're memory-managed.
00088             return false;
00089         }
00090 
00091             virtual std::vector<std::string> getMemberNames() const {
00092                 // only discover the parts of this struct:
00093                 std::vector<std::string> result;
00094                 result.push_back("size");
00095                 result.push_back("capacity");
00096                 return result;
00097             }
00098 
00099             virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string& name) const {
00100                 using namespace internal;
00101                 typename AssignableDataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item );
00102                 if ( !data ) {
00103                     return base::DataSourceBase::shared_ptr();
00104                 }
00105 
00106                 // size and capacity can not change during program execution:
00107                 if (name == "size" || name == "capacity") {
00108                     return new ConstantDataSource<int>( T::static_size );
00109                 }
00110 
00111                 // contents of indx can change during program execution:
00112                 try {
00113                     unsigned int indx = boost::lexical_cast<unsigned int>(name);
00114                     // @todo could also return a direct reference to item indx using another DS type that respects updated().
00115                     return new ArrayPartDataSource<typename T::value_type>( *data->set().c_array(), new ConstantDataSource<unsigned int>(indx), item, T::static_size);
00116                 } catch(...) {}
00117                 log(Error) << "BoostArrayTypeInfo: No such part (or invalid index): " << name << endlog();
00118                 return base::DataSourceBase::shared_ptr();
00119             }
00120 
00121             virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item,
00122                                                              base::DataSourceBase::shared_ptr id) const {
00123                 using namespace internal;
00124                 typename AssignableDataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item );
00125                 if ( !data ) {
00126                     return base::DataSourceBase::shared_ptr();
00127                 }
00128 
00129                 // discover if user gave us a part name or index:
00130                 typename DataSource<unsigned int>::shared_ptr id_indx = DataSource<unsigned int>::narrow( id.get() );
00131                 typename DataSource<std::string>::shared_ptr id_name = DataSource<std::string>::narrow( id.get() );
00132                 if ( id_name ) {
00133                     // size and capacity can not change during program execution:
00134                     if (id_name->get() == "size" || id_name->get() == "capacity") {
00135                         return new ConstantDataSource<int>( T::static_size );
00136                     }
00137                 }
00138 
00139                 if ( id_indx ) {
00140                     return new ArrayPartDataSource<typename T::value_type>( *data->set().c_array(), id_indx, item, T::static_size );
00141                 }
00142                 log(Error) << "BoostArrayTypeInfo: No such part (or invalid index): " << id_name->get() << id_indx->get() << endlog();
00143                 return base::DataSourceBase::shared_ptr();
00144             }
00145 
00149             virtual base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
00150             {
00151                 return base::DataSourceBase::shared_ptr();
00152             }
00153 
00154             virtual bool composeType( base::DataSourceBase::shared_ptr dssource, base::DataSourceBase::shared_ptr dsresult) const {
00155                 const internal::DataSource<PropertyBag>* pb = dynamic_cast< const internal::DataSource<PropertyBag>* > (dssource.get() );
00156                 if ( !pb )
00157                     return false;
00158                 typename internal::AssignableDataSource<T>::shared_ptr ads = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( dsresult );
00159                 if ( !ads )
00160                     return false;
00161 
00162                 PropertyBag const& source = pb->rvalue();
00163                 typename internal::AssignableDataSource<T>::reference_t result = ads->set();
00164 
00165                 //result.resize( source.size() );
00166                 if(result.size() != source.size()) {
00167                     log(Error) << "Refusing to compose Boost Arrays from a property list of different size. Use the same number of properties as the C++ boost array size." << endlog();
00168                     return false;
00169                 }
00170                 // recurse into items of this sequence:
00171                 TypeInfoRepository::shared_ptr tir = Types();
00172                 PropertyBag target( source.getType() );
00173                 PropertyBag decomp;
00174                 internal::ReferenceDataSource<T> rds(result);
00175                 rds.ref(); // prevent dealloc.
00176                 // we compose each item in this sequence and then update result with target's result.
00177                 // 1. each child is composed into target (this is a recursive thing)
00178                 // 2. we decompose result one-level deep and 'refresh' it with the composed children of step 1.
00179                 if ( composePropertyBag(source, target) && typeDecomposition( &rds, decomp, false) && ( tir->type(decomp.getType()) == tir->type(target.getType()) ) && refreshProperties(decomp, target, true) ) {
00180                     assert(result.size() == source.size());
00181                     assert(source.size() == target.size());
00182                     assert(source.size() == decomp.size());
00183                     return true;
00184                 }
00185                 return false;
00186             }
00187 
00188         };
00189     }
00190 }
00191 
00192 #endif


rtt
Author(s): RTT Developers
autogenerated on Sat Jun 8 2019 18:46:06