Orocos Real-Time Toolkit
2.6.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: 00064 public PrimitiveTypeInfo<T, has_ostream>, 00065 public MemberFactory, public CompositionFactory 00066 { 00067 public: 00068 CArrayTypeInfo(std::string name) : 00069 PrimitiveTypeInfo<T, has_ostream> (name) 00070 { 00071 } 00072 00073 bool installTypeInfoObject(TypeInfo* ti) { 00074 // aquire a shared reference to the this object 00075 boost::shared_ptr< CArrayTypeInfo<T> > mthis = boost::dynamic_pointer_cast<CArrayTypeInfo<T> >( this->getSharedPtr() ); 00076 // Allow base to install first 00077 PrimitiveTypeInfo<T,has_ostream>::installTypeInfoObject(ti); 00078 // Install the factories for primitive types 00079 ti->setMemberFactory( mthis ); 00080 ti->setCompositionFactory( mthis ); 00081 00082 // Don't delete us, we're memory-managed. 00083 return false; 00084 } 00085 00086 using TemplateValueFactory<T>::buildVariable; 00087 virtual base::AttributeBase* buildVariable(std::string name,int sizehint) const 00088 { 00089 // There were two choices: create an empty carray, ie pointer-like behavior; OR create one with storage in the DS. 00090 // We need to redefine assignment in case of the latter, and make the storage dynamic, depending on sizehint. 00091 // pointer-like is dangerous due to non-tracking of reference-counts, so this is left for the default buildVariable 00092 // without a sizehint (using ValueDataSource), while the size hint version has storage. 00093 typename internal::ArrayDataSource<T>::shared_ptr ads = new internal::UnboundDataSource<internal::ArrayDataSource<T> >(); 00094 ads->newArray( sizehint ); 00095 return new Attribute<T>( name, ads.get() ); 00096 } 00097 00098 /* buildConstant() with sizehint is left out since it is identical to buildConstant() without sizehint. 00099 We make a shallow copy, so the size is automatically taken from the original expression the constant 00100 refers to. */ 00101 00102 virtual std::vector<std::string> getMemberNames() const { 00103 // only discover the parts of this struct: 00104 std::vector<std::string> result; 00105 result.push_back("size"); 00106 result.push_back("capacity"); 00107 return result; 00108 } 00109 00110 virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string& name) const { 00111 using namespace internal; 00112 typename DataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< DataSource<T> >( item ); 00113 00114 // size and capacity can not change during program execution: 00115 if (name == "size" || name == "capacity") { 00116 return new ConstantDataSource<int>( data->rvalue().count() ); 00117 } 00118 00119 typename AssignableDataSource<T>::shared_ptr adata = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item ); 00120 if ( !adata ) { 00121 return base::DataSourceBase::shared_ptr(); 00122 } 00123 00124 // contents of indx can change during program execution: 00125 try { 00126 unsigned int indx = boost::lexical_cast<unsigned int>(name); 00127 // @todo could also return a direct reference to item indx using another DS type that respects updated(). 00128 return new ArrayPartDataSource<typename T::value_type>( *adata->set().address(), new ConstantDataSource<unsigned int>(indx), item, data->rvalue().count() ); 00129 } catch(...) {} 00130 log(Error) << "CArrayTypeInfo: No such part (or invalid index): " << name << endlog(); 00131 return base::DataSourceBase::shared_ptr(); 00132 } 00133 00134 virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, 00135 base::DataSourceBase::shared_ptr id) const { 00136 using namespace internal; 00137 typename DataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< DataSource<T> >( item ); 00138 if ( !data ) { 00139 return base::DataSourceBase::shared_ptr(); 00140 } 00141 00142 // discover if user gave us a part name or index: 00143 typename DataSource<std::string>::shared_ptr id_name = DataSource<std::string>::narrow( id.get() ); 00144 if ( id_name ) { 00145 // size and capacity can not change during program execution: 00146 if (id_name->get() == "size" || id_name->get() == "capacity") { 00147 return new ConstantDataSource<int>( data->rvalue().count() ); 00148 } else { 00149 log(Error) << "CArrayTypeInfo: No such part : " << id_name->get() << endlog(); 00150 return base::DataSourceBase::shared_ptr(); 00151 } 00152 } 00153 00154 typename AssignableDataSource<T>::shared_ptr adata = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item ); 00155 if ( !adata ) { 00156 log(Error) << "CArrayTypeInfo: need assignable data type for indexing " << this->getTypeName() << endlog(); 00157 return base::DataSourceBase::shared_ptr(); 00158 } 00159 00160 typename DataSource<unsigned int>::shared_ptr id_indx = DataSource<unsigned int>::narrow( DataSourceTypeInfo<unsigned int>::getTypeInfo()->convert(id).get() ); 00161 if ( id_indx ) { 00162 return new ArrayPartDataSource<typename T::value_type>( *adata->set().address(), id_indx, item, data->rvalue().count() ); 00163 } 00164 log(Error) << "CArrayTypeInfo: Invalid index) for type " << this->getTypeName() << endlog(); 00165 return base::DataSourceBase::shared_ptr(); 00166 } 00167 00171 virtual base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const 00172 { 00173 return base::DataSourceBase::shared_ptr(); 00174 } 00175 00176 virtual bool composeType( base::DataSourceBase::shared_ptr dssource, base::DataSourceBase::shared_ptr dsresult) const { 00177 const internal::DataSource<PropertyBag>* pb = dynamic_cast< const internal::DataSource<PropertyBag>* > (dssource.get() ); 00178 if ( !pb ) 00179 return false; 00180 typename internal::AssignableDataSource<T>::shared_ptr ads = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( dsresult ); 00181 if ( !ads ) 00182 return false; 00183 00184 PropertyBag const& source = pb->rvalue(); 00185 typename internal::AssignableDataSource<T>::reference_t result = ads->set(); 00186 00187 //result.resize( source.size() ); 00188 if(result.count() != source.size()) { 00189 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(); 00190 return false; 00191 } 00192 // recurse into items of this sequence: 00193 TypeInfoRepository::shared_ptr tir = Types(); 00194 PropertyBag target( source.getType() ); 00195 PropertyBag decomp; 00196 internal::ReferenceDataSource<T> rds(result); 00197 rds.ref(); // prevent dealloc. 00198 // we compose each item in this sequence and then update result with target's result. 00199 // 1. each child is composed into target (this is a recursive thing) 00200 // 2. we decompose result one-level deep and 'refresh' it with the composed children of step 1. 00201 if ( composePropertyBag(source, target) && typeDecomposition( &rds, decomp, false) && ( tir->type( decomp.getType() ) == tir->type( target.getType() ) ) && refreshProperties(decomp, target, true) ) { 00202 assert(result.count() == source.size()); 00203 assert(source.size() == target.size()); 00204 assert(source.size() == decomp.size()); 00205 return true; 00206 } 00207 return false; 00208 } 00209 00210 }; 00211 } 00212 } 00213 00214 #endif