Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
00138 return false;
00139 }
00140
00141 ~SequenceTypeInfoBase() {}
00142
00143 base::AttributeBase* buildVariable(std::string name,int size) const
00144 {
00145
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
00177 PropertyBag target( source.getType() );
00178
00179
00180
00181
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
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
00211 try {
00212 unsigned int indx = boost::lexical_cast<unsigned int>(name);
00213
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
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