Orocos Real-Time Toolkit
2.5.0
|
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 00050 namespace RTT 00051 { 00052 namespace types 00053 { 00065 template<typename T, bool has_ostream = false> 00066 class BoostArrayTypeInfo: public PrimitiveTypeInfo<T, has_ostream> 00067 { 00068 public: 00069 BoostArrayTypeInfo(std::string name) : 00070 PrimitiveTypeInfo<T, has_ostream> (name) 00071 { 00072 } 00073 00074 virtual std::vector<std::string> getMemberNames() const { 00075 // only discover the parts of this struct: 00076 std::vector<std::string> result; 00077 result.push_back("size"); 00078 result.push_back("capacity"); 00079 return result; 00080 } 00081 00082 virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string& name) const { 00083 using namespace internal; 00084 typename AssignableDataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item ); 00085 if ( !data ) { 00086 return base::DataSourceBase::shared_ptr(); 00087 } 00088 00089 // size and capacity can not change during program execution: 00090 if (name == "size" || name == "capacity") { 00091 return new ConstantDataSource<int>( T::static_size ); 00092 } 00093 00094 // contents of indx can change during program execution: 00095 try { 00096 unsigned int indx = boost::lexical_cast<unsigned int>(name); 00097 // @todo could also return a direct reference to item indx using another DS type that respects updated(). 00098 return new ArrayPartDataSource<typename T::value_type>( *data->set().c_array(), new ConstantDataSource<unsigned int>(indx), item, T::static_size); 00099 } catch(...) {} 00100 log(Error) << "BoostArrayTypeInfo: No such part (or invalid index): " << name << endlog(); 00101 return base::DataSourceBase::shared_ptr(); 00102 } 00103 00104 virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, 00105 base::DataSourceBase::shared_ptr id) const { 00106 using namespace internal; 00107 typename AssignableDataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item ); 00108 if ( !data ) { 00109 return base::DataSourceBase::shared_ptr(); 00110 } 00111 00112 // discover if user gave us a part name or index: 00113 typename DataSource<unsigned int>::shared_ptr id_indx = DataSource<unsigned int>::narrow( id.get() ); 00114 typename DataSource<std::string>::shared_ptr id_name = DataSource<std::string>::narrow( id.get() ); 00115 if ( id_name ) { 00116 // size and capacity can not change during program execution: 00117 if (id_name->get() == "size" || id_name->get() == "capacity") { 00118 return new ConstantDataSource<int>( T::static_size ); 00119 } 00120 } 00121 00122 if ( id_indx ) { 00123 return new ArrayPartDataSource<typename T::value_type>( *data->set().c_array(), id_indx, item, T::static_size ); 00124 } 00125 log(Error) << "BoostArrayTypeInfo: No such part (or invalid index): " << id_name->get() << id_indx->get() << endlog(); 00126 return base::DataSourceBase::shared_ptr(); 00127 } 00128 00132 virtual base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const 00133 { 00134 return base::DataSourceBase::shared_ptr(); 00135 } 00136 00137 virtual bool composeType( base::DataSourceBase::shared_ptr dssource, base::DataSourceBase::shared_ptr dsresult) const { 00138 const internal::DataSource<PropertyBag>* pb = dynamic_cast< const internal::DataSource<PropertyBag>* > (dssource.get() ); 00139 if ( !pb ) 00140 return false; 00141 typename internal::AssignableDataSource<T>::shared_ptr ads = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( dsresult ); 00142 if ( !ads ) 00143 return false; 00144 00145 PropertyBag const& source = pb->rvalue(); 00146 typename internal::AssignableDataSource<T>::reference_t result = ads->set(); 00147 00148 //result.resize( source.size() ); 00149 if(result.size() != source.size()) { 00150 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(); 00151 return false; 00152 } 00153 // recurse into items of this sequence: 00154 PropertyBag target( source.getType() ); 00155 PropertyBag decomp; 00156 internal::ReferenceDataSource<T> rds(result); 00157 rds.ref(); // prevent dealloc. 00158 // we compose each item in this sequence and then update result with target's result. 00159 // 1. each child is composed into target (this is a recursive thing) 00160 // 2. we decompose result one-level deep and 'refresh' it with the composed children of step 1. 00161 if ( composePropertyBag(source, target) && typeDecomposition( &rds, decomp, false) && ( decomp.getType() == target.getType() ) && refreshProperties(decomp, target, true) ) { 00162 assert(result.size() == source.size()); 00163 assert(source.size() == target.size()); 00164 assert(source.size() == decomp.size()); 00165 return true; 00166 } 00167 return false; 00168 } 00169 00170 }; 00171 } 00172 } 00173 00174 #endif