MultiVectorComposition.hpp
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
00040 #ifndef MULTIVECTOR_COMPOSITION_HPP
00041 #define MULTIVECTOR_COMPOSITION_HPP
00042
00043 #include "MultiVector.hpp"
00044 #include "Property.hpp"
00045 #include "PropertyBag.hpp"
00046 #include "Logger.hpp"
00047
00048 namespace RTT
00049 {
00050 class PropertyIntrospection;
00051 class PropertyBag;
00052 template<class T>
00053 class Property;
00054
00059 template<class T, int S>
00060 void decomposeProperty(PropertyIntrospection *pi, const Property< MultiVector<S, T> >& c)
00061 {
00062 Property<PropertyBag> result(c.getName(),c.getDescription(), PropertyBag("MultiVector") );
00063
00064 MultiVector<S,T> vec = c;
00065 Property<int>* dimension = new Property<int>("Size","Size of the MultiVector", vec.size() );
00066
00067 result.value().add( dimension );
00068
00069 std::stringstream data_name;
00070
00071 for ( int i=0; i < dimension->get() ; i++)
00072 {
00073 data_name << i;
00074 result.value().add( new Property<T>(data_name.str(),"",vec[i]) );
00075 data_name.str("");
00076 }
00077
00078 pi->introspect(result);
00079 deleteProperties( result.value() );
00080
00081 }
00082
00083 template<class T, int S>
00084 void decomposeProperty(PropertyIntrospection *pi, const Property< const MultiVector<S, T>& >& c)
00085 {
00086 Property<PropertyBag> result(c.getName(),c.getDescription(), PropertyBag("MultiVector") );
00087
00088 MultiVector<S,T> vec = c;
00089 Property<int>* dimension = new Property<int>("Size","Size of the MultiVector", vec.size() );
00090
00091 result.value().add( dimension );
00092
00093 std::stringstream data_name;
00094
00095 for ( int i=0; i < dimension->get() ; i++)
00096 {
00097 data_name << i;
00098 result.value().add( new Property<T>(data_name.str(),"",vec[i]) );
00099 data_name.str("");
00100 }
00101
00102 pi->introspect(result);
00103 deleteProperties( result.value() );
00104
00105 }
00106
00110 template<class T, int S>
00111 bool composeProperty(const PropertyBag& bag, Property<MultiVector<S,T> >& result)
00112 {
00113 PropertyBase* v_base = bag.find( result.getName() );
00114 if ( v_base == 0 )
00115 return false;
00116
00117 Property<PropertyBag>* v_bag = dynamic_cast< Property<PropertyBag>* >( v_base );
00118
00119 if (v_bag != 0 && v_bag->get().getType() == "MultiVector")
00120 {
00121 Property<T>* comp;
00122
00123 Property<int>* dim;
00124 v_base = v_bag->get().find("Size");
00125 if ( v_base == 0 ) {
00126 Logger::log() << Logger::Error << "In PropertyBag for Property< MultiVector<S,T> > :"
00127 << result.getName() << " : could not find property \"Size\"."<<Logger::endl;
00128 return false;
00129 }
00130 dim = dynamic_cast< Property<int>* >(v_base);
00131 if ( dim == 0) {
00132 Logger::log() << Logger::Error << "In PropertyBag for Property< MultiVector<S,T> > :"
00133 << result.getName() << " : Expected \"Size\" to be of type short."<<Logger::endl;
00134 return false;
00135 }
00136 int dimension = dim->get();
00137
00138 std::stringstream data_name;
00139
00140
00141 for (int i = 0; i < dimension ; i++)
00142 {
00143 data_name << i;
00144 PropertyBase* element = v_bag->get().find( data_name.str() );
00145 if ( element == 0 ) {
00146 Logger::log() << Logger::Error << "Aborting composition of Property< MultiVector<S,T> > "<<result.getName()
00147 << ": Data element "<< data_name.str() <<" not found !"
00148 <<Logger::endl;
00149 return false;
00150 }
00151 comp = dynamic_cast< Property<T>* >( element );
00152 if ( comp == 0 ) {
00153 DataSourceBase::shared_ptr ds = element->getDataSource();
00154 Logger::log() << Logger::Error << "Aborting composition of Property< MultiVector<S,T> > "<<result.getName()
00155 << ": Exptected data element "<< data_name.str() << " to be of type "<<DataSource<T>::GetType()
00156 <<" got type " << element->getType()
00157 <<Logger::endl;
00158 return false;
00159 }
00160 result.value()[i] = comp->get();
00161
00162 data_name.str("");
00163 }
00164 }
00165 else
00166 {
00167 if ( v_bag != 0 ) {
00168 Logger::log() << Logger::Error << "Composing Property< MultiVector<S,T> > :"
00169 << result.getName() << " : type mismatch, got type '"<< v_bag->get().getType() <<"'"<<Logger::endl;
00170 } else {
00171 Logger::log() << Logger::Error << "Composing Property< MultiVector<S,T> > :"
00172 << result.getName() << " : not a PropertyBag."<<Logger::endl;
00173 }
00174
00175 Logger::log() << Logger::Debug << "Could not update Property< MultiVector<S,T> > : "<<result.getName()<<Logger::endl;
00176 return false;
00177 }
00178 return true;
00179
00180 }
00181
00182 };
00183
00184
00185 #endif