Orocos Real-Time Toolkit
2.5.0
|
00001 /*************************************************************************** 00002 tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 CArrayTypeInfo.hpp 00003 00004 CArrayTypeInfo.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_CARRAY_INFO_HPP 00040 #define ORO_TEMPLATE_CARRAY_INFO_HPP 00041 00042 #include "PrimitiveTypeInfo.hpp" 00043 #include "../internal/ArrayPartDataSource.hpp" 00044 #include <boost/lexical_cast.hpp> 00045 #include "carray.hpp" 00046 #include "../internal/carray.hpp" 00047 #include "PropertyComposition.hpp" 00048 #include "PropertyDecomposition.hpp" 00049 00050 namespace RTT 00051 { 00052 namespace types 00053 { 00062 template<typename T, bool has_ostream = false> 00063 class CArrayTypeInfo: public PrimitiveTypeInfo<T, has_ostream> 00064 { 00065 public: 00066 CArrayTypeInfo(std::string name) : 00067 PrimitiveTypeInfo<T, has_ostream> (name) 00068 { 00069 } 00070 00071 virtual base::AttributeBase* buildVariable(std::string name,int sizehint) const 00072 { 00073 // There were two choices: create an empty carray, ie pointer-like behavior; OR create one with storage in the DS. 00074 // We need to redefine assignment in case of the latter, and make the storage dynamic, depending on sizehint. 00075 // pointer-like is dangerous due to non-tracking of reference-counts, so this is left for the default buildVariable 00076 // without a sizehint (using ValueDataSource), while the size hint version has storage. 00077 typename internal::ArrayDataSource<T>::shared_ptr ads = new internal::UnboundDataSource<internal::ArrayDataSource<T> >(); 00078 ads->newArray( sizehint ); 00079 return new Attribute<T>( name, ads.get() ); 00080 } 00081 00082 /* buildConstant() with sizehint is left out since it is identical to buildConstant() without sizehint. 00083 We make a shallow copy, so the size is automatically taken from the original expression the constant 00084 refers to. */ 00085 00086 virtual std::vector<std::string> getMemberNames() const { 00087 // only discover the parts of this struct: 00088 std::vector<std::string> result; 00089 result.push_back("size"); 00090 result.push_back("capacity"); 00091 return result; 00092 } 00093 00094 virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string& name) const { 00095 using namespace internal; 00096 typename DataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< DataSource<T> >( item ); 00097 00098 // size and capacity can not change during program execution: 00099 if (name == "size" || name == "capacity") { 00100 return new ConstantDataSource<int>( data->rvalue().count() ); 00101 } 00102 00103 typename AssignableDataSource<T>::shared_ptr adata = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item ); 00104 if ( !adata ) { 00105 return base::DataSourceBase::shared_ptr(); 00106 } 00107 00108 // contents of indx can change during program execution: 00109 try { 00110 unsigned int indx = boost::lexical_cast<unsigned int>(name); 00111 // @todo could also return a direct reference to item indx using another DS type that respects updated(). 00112 return new ArrayPartDataSource<typename T::value_type>( *adata->set().address(), new ConstantDataSource<unsigned int>(indx), item, data->rvalue().count() ); 00113 } catch(...) {} 00114 log(Error) << "CArrayTypeInfo: No such part (or invalid index): " << name << endlog(); 00115 return base::DataSourceBase::shared_ptr(); 00116 } 00117 00118 virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, 00119 base::DataSourceBase::shared_ptr id) const { 00120 using namespace internal; 00121 typename DataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< DataSource<T> >( item ); 00122 if ( !data ) { 00123 return base::DataSourceBase::shared_ptr(); 00124 } 00125 00126 // discover if user gave us a part name or index: 00127 typename DataSource<std::string>::shared_ptr id_name = DataSource<std::string>::narrow( id.get() ); 00128 if ( id_name ) { 00129 // size and capacity can not change during program execution: 00130 if (id_name->get() == "size" || id_name->get() == "capacity") { 00131 return new ConstantDataSource<int>( data->rvalue().count() ); 00132 } else { 00133 log(Error) << "CArrayTypeInfo: No such part : " << id_name->get() << endlog(); 00134 return base::DataSourceBase::shared_ptr(); 00135 } 00136 } 00137 00138 typename AssignableDataSource<T>::shared_ptr adata = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item ); 00139 if ( !adata ) { 00140 log(Error) << "CArrayTypeInfo: need assignable data type for indexing " << this->getTypeName() << endlog(); 00141 return base::DataSourceBase::shared_ptr(); 00142 } 00143 00144 typename DataSource<unsigned int>::shared_ptr id_indx = DataSource<unsigned int>::narrow( DataSourceTypeInfo<unsigned int>::getTypeInfo()->convert(id).get() ); 00145 if ( id_indx ) { 00146 return new ArrayPartDataSource<typename T::value_type>( *adata->set().address(), id_indx, item, data->rvalue().count() ); 00147 } 00148 log(Error) << "CArrayTypeInfo: Invalid index) for type " << this->getTypeName() << endlog(); 00149 return base::DataSourceBase::shared_ptr(); 00150 } 00151 00155 virtual base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const 00156 { 00157 return base::DataSourceBase::shared_ptr(); 00158 } 00159 00160 virtual bool composeType( base::DataSourceBase::shared_ptr dssource, base::DataSourceBase::shared_ptr dsresult) const { 00161 const internal::DataSource<PropertyBag>* pb = dynamic_cast< const internal::DataSource<PropertyBag>* > (dssource.get() ); 00162 if ( !pb ) 00163 return false; 00164 typename internal::AssignableDataSource<T>::shared_ptr ads = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( dsresult ); 00165 if ( !ads ) 00166 return false; 00167 00168 PropertyBag const& source = pb->rvalue(); 00169 typename internal::AssignableDataSource<T>::reference_t result = ads->set(); 00170 00171 //result.resize( source.size() ); 00172 if(result.count() != source.size()) { 00173 log(Error) << "Refusing to compose C Arrays from a property list of different size. Use the same number of properties as the C array size." << endlog(); 00174 return false; 00175 } 00176 // recurse into items of this sequence: 00177 PropertyBag target( source.getType() ); 00178 PropertyBag decomp; 00179 internal::ReferenceDataSource<T> rds(result); 00180 rds.ref(); // prevent dealloc. 00181 // we compose each item in this sequence and then update result with target's result. 00182 // 1. each child is composed into target (this is a recursive thing) 00183 // 2. we decompose result one-level deep and 'refresh' it with the composed children of step 1. 00184 if ( composePropertyBag(source, target) && typeDecomposition( &rds, decomp, false) && ( decomp.getType() == target.getType() ) && refreshProperties(decomp, target, true) ) { 00185 assert(result.count() == source.size()); 00186 assert(source.size() == target.size()); 00187 assert(source.size() == decomp.size()); 00188 return true; 00189 } 00190 return false; 00191 } 00192 00193 }; 00194 } 00195 } 00196 00197 #endif