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_CORELIB_DATASOURCES_HPP
00040 #define ORO_CORELIB_DATASOURCES_HPP
00041
00042 #include "mystd.hpp"
00043 #include "DataSource.hpp"
00044 #include "DataSourceTypeInfo.hpp"
00045 #include <vector>
00046
00047 namespace RTT
00048 {
00049 namespace internal {
00050
00058 template<typename T>
00059 class ValueDataSource
00060 : public AssignableDataSource<T>
00061 {
00062 protected:
00063 mutable typename DataSource<T>::value_t mdata;
00064
00065 public:
00069 ~ValueDataSource();
00070
00071 typedef boost::intrusive_ptr<ValueDataSource<T> > shared_ptr;
00072
00073 ValueDataSource( T data );
00074
00075 ValueDataSource( );
00076
00077 typename DataSource<T>::result_t get() const
00078 {
00079 return mdata;
00080 }
00081
00082 typename DataSource<T>::result_t value() const
00083 {
00084 return mdata;
00085 }
00086
00087 void set( typename AssignableDataSource<T>::param_t t );
00088
00089 typename AssignableDataSource<T>::reference_t set()
00090 {
00091 return mdata;
00092 }
00093
00094 typename AssignableDataSource<T>::const_reference_t rvalue() const
00095 {
00096 return mdata;
00097 }
00098
00099 virtual ValueDataSource<T>* clone() const;
00100
00101 virtual ValueDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const;
00102 };
00103
00107 template<>
00108 RTT_API void ValueDataSource<std::string>::set( AssignableDataSource<std::string>::param_t t );
00109
00113 template<>
00114 RTT_API ValueDataSource<std::string>::ValueDataSource(std::string t );
00115
00121 template<typename T>
00122 class ConstantDataSource
00123 : public DataSource<T>
00124 {
00129 typename boost::add_const<typename DataSource<T>::value_t>::type mdata;
00130
00131 public:
00135 ~ConstantDataSource();
00136
00137 typedef boost::intrusive_ptr< ConstantDataSource<T> > shared_ptr;
00138
00139 ConstantDataSource( T value );
00140
00141 typename DataSource<T>::result_t get() const
00142 {
00143 return mdata;
00144 }
00145
00146 typename DataSource<T>::result_t value() const
00147 {
00148 return mdata;
00149 }
00150
00151 typename DataSource<T>::const_reference_t rvalue() const
00152 {
00153 return mdata;
00154 }
00155
00156 virtual ConstantDataSource<T>* clone() const;
00157
00158 virtual ConstantDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const;
00159 };
00160
00166 template<typename T>
00167 class ConstReferenceDataSource
00168 : public DataSource<T>
00169 {
00170
00171 typename DataSource<T>::const_reference_t mref;
00172 public:
00176 ~ConstReferenceDataSource();
00177
00178 typedef boost::intrusive_ptr<ConstReferenceDataSource<T> > shared_ptr;
00179
00180 ConstReferenceDataSource( typename DataSource<T>::const_reference_t ref );
00181
00182 typename DataSource<T>::result_t get() const
00183 {
00184 return mref;
00185 }
00186
00187 typename DataSource<T>::result_t value() const
00188 {
00189 return mref;
00190 }
00191
00192 typename DataSource<T>::const_reference_t rvalue() const
00193 {
00194 return mref;
00195 }
00196
00197 virtual ConstReferenceDataSource<T>* clone() const;
00198
00199 virtual ConstReferenceDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const;
00200 };
00201
00202
00208 template<typename T>
00209 class ReferenceDataSource
00210 : public AssignableDataSource<T>
00211 {
00212
00213 typename AssignableDataSource<T>::reference_t mref;
00214 public:
00218 ~ReferenceDataSource();
00219
00220 typedef boost::intrusive_ptr<ReferenceDataSource<T> > shared_ptr;
00221
00222 ReferenceDataSource( typename AssignableDataSource<T>::reference_t ref );
00223
00224 typename DataSource<T>::result_t get() const
00225 {
00226 return mref;
00227 }
00228
00229 typename DataSource<T>::result_t value() const
00230 {
00231 return mref;
00232 }
00233
00234 void set( typename AssignableDataSource<T>::param_t t );
00235
00236 typename AssignableDataSource<T>::reference_t set()
00237 {
00238 return mref;
00239 }
00240
00241 typename AssignableDataSource<T>::const_reference_t rvalue() const
00242 {
00243 return mref;
00244 }
00245
00246 virtual ReferenceDataSource<T>* clone() const;
00247
00248 virtual ReferenceDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const;
00249 };
00250
00257 template<typename T>
00258 class AliasDataSource
00259 : public DataSource<T>
00260 {
00261 typename DataSource<T>::shared_ptr alias;
00262 public:
00263 typedef boost::intrusive_ptr<AliasDataSource<T> > shared_ptr;
00264
00265 AliasDataSource(DataSource<T>* ds)
00266 : alias(ds)
00267 {}
00268
00269 ~AliasDataSource() { }
00270
00271 bool evaluate() const {
00272 return alias->evaluate();
00273 }
00274
00275 typename DataSource<T>::result_t get() const
00276 {
00277 return alias->get();
00278 }
00279
00280 typename DataSource<T>::result_t value() const
00281 {
00282 return alias->value();
00283 }
00284
00285 typename DataSource<T>::const_reference_t rvalue() const
00286 {
00287 return alias->rvalue();
00288 }
00289
00290 virtual void reset() { alias->reset(); }
00291
00292 virtual AliasDataSource<T>* clone() const {
00293 return new AliasDataSource(alias.get());
00294 }
00295 virtual AliasDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
00296 return new AliasDataSource(alias->copy(alreadyCloned) );
00297 }
00298 };
00299
00300
00309 template<typename T>
00310 class LateReferenceDataSource
00311 : public AssignableDataSource<T>
00312 {
00313
00314 typename AssignableDataSource<T>::value_t* mptr;
00315 public:
00316
00317 typedef boost::intrusive_ptr<LateReferenceDataSource<T> > shared_ptr;
00318
00319 LateReferenceDataSource( typename AssignableDataSource<T>::value_t* ptr = 0)
00320 :mptr(ptr) {}
00321
00322 void setPointer( typename AssignableDataSource<T>::value_t* ptr ) {
00323 mptr = ptr;
00324 }
00325
00326 typename DataSource<T>::result_t get() const
00327 {
00328 return *mptr;
00329 }
00330
00331 typename DataSource<T>::result_t value() const
00332 {
00333 return *mptr;
00334 }
00335
00336 void const* getRawDataConst()
00337 {
00338 return mptr;
00339 }
00340 void* getRawData()
00341 {
00342 return mptr;
00343 }
00344
00345 void set( typename AssignableDataSource<T>::param_t t ) {
00346 *mptr = t;
00347 }
00348
00349 typename AssignableDataSource<T>::reference_t set()
00350 {
00351 return *mptr;
00352 }
00353
00354 typename AssignableDataSource<T>::const_reference_t rvalue() const
00355 {
00356 return *mptr;
00357 }
00358
00359 virtual LateReferenceDataSource<T>* clone() const {
00360 return new LateReferenceDataSource<T>( mptr );
00361 }
00362
00363 virtual LateReferenceDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& ) const {
00364 return const_cast<LateReferenceDataSource<T>* >(this);
00365 }
00366 };
00367
00376 template<typename T>
00377 class LateConstReferenceDataSource
00378 : public DataSource<T>
00379 {
00380
00381 const typename DataSource<T>::value_t* mptr;
00382 public:
00383
00384 typedef boost::intrusive_ptr<LateConstReferenceDataSource<T> > shared_ptr;
00385
00386 LateConstReferenceDataSource(const typename DataSource<T>::value_t* ptr = 0)
00387 :mptr(ptr) {}
00388
00389 void setPointer(const typename AssignableDataSource<T>::value_t* ptr ) {
00390 mptr = ptr;
00391 }
00392
00393 void const* getRawDataConst()
00394 {
00395 return mptr;
00396 }
00397
00398 typename DataSource<T>::result_t get() const
00399 {
00400 return *mptr;
00401 }
00402
00403 typename DataSource<T>::result_t value() const
00404 {
00405 return *mptr;
00406 }
00407
00408 typename DataSource<T>::const_reference_t rvalue() const
00409 {
00410 return *mptr;
00411 }
00412
00413 virtual LateConstReferenceDataSource<T>* clone() const {
00414 return new LateConstReferenceDataSource<T>( mptr );
00415 }
00416
00417 virtual LateConstReferenceDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& ) const {
00418 return const_cast<LateConstReferenceDataSource<T>* >(this);
00419 }
00420 };
00421
00422
00428 template<typename T>
00429 class ActionAliasDataSource
00430 : public DataSource<T>
00431 {
00432 base::ActionInterface* action;
00433 typename DataSource<T>::shared_ptr alias;
00434 public:
00435 typedef boost::intrusive_ptr<ActionAliasDataSource<T> > shared_ptr;
00436
00437 ActionAliasDataSource(base::ActionInterface* act, DataSource<T>* ds)
00438 : action(act), alias(ds)
00439 {}
00440
00441 ~ActionAliasDataSource() { delete action; }
00442
00443 bool evaluate() const {
00444
00445
00446 action->readArguments();
00447 bool r = action->execute();
00448 action->reset();
00449
00450 alias->evaluate();
00451 return r;
00452 }
00453
00454 typename DataSource<T>::result_t get() const
00455 {
00456 action->readArguments();
00457 action->execute();
00458 action->reset();
00459 return alias->get();
00460 }
00461
00462 typename DataSource<T>::result_t value() const
00463 {
00464 return alias->value();
00465 }
00466
00467 typename DataSource<T>::const_reference_t rvalue() const
00468 {
00469 return alias->rvalue();
00470 }
00471
00472 virtual void reset() { alias->reset(); }
00473
00474 virtual ActionAliasDataSource<T>* clone() const {
00475 return new ActionAliasDataSource(action, alias.get());
00476 }
00477 virtual ActionAliasDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
00478 return new ActionAliasDataSource( action->copy(alreadyCloned), alias->copy(alreadyCloned) );
00479 }
00480 };
00481
00482
00488 template<typename T>
00489 class ActionAliasAssignableDataSource
00490 : public AssignableDataSource<T>
00491 {
00492 base::ActionInterface* action;
00493 typename AssignableDataSource<T>::shared_ptr alias;
00494 public:
00495 typedef boost::intrusive_ptr<ActionAliasDataSource<T> > shared_ptr;
00496
00497 ActionAliasAssignableDataSource(base::ActionInterface* act, AssignableDataSource<T>* ds)
00498 : action(act), alias(ds)
00499 {}
00500
00501 ~ActionAliasAssignableDataSource() { delete action;}
00502
00503 bool evaluate() const {
00504
00505
00506 action->readArguments();
00507 bool r = action->execute();
00508 action->reset();
00509
00510 alias->evaluate();
00511 return r;
00512 }
00513
00514 typename DataSource<T>::result_t get() const
00515 {
00516 action->readArguments();
00517 action->execute();
00518 action->reset();
00519 return alias->get();
00520 }
00521
00522 typename DataSource<T>::result_t value() const
00523 {
00524 return alias->value();
00525 }
00526
00527 void set( typename AssignableDataSource<T>::param_t t ) {
00528 alias->set( t );
00529 }
00530
00531 typename AssignableDataSource<T>::reference_t set()
00532 {
00533 return alias->set();
00534 }
00535
00536 typename AssignableDataSource<T>::const_reference_t rvalue() const
00537 {
00538 return alias->rvalue();
00539 }
00540
00541 virtual void reset() { alias->reset(); }
00542
00543 virtual ActionAliasAssignableDataSource<T>* clone() const {
00544 return new ActionAliasAssignableDataSource(action, alias.get());
00545 }
00546 virtual ActionAliasAssignableDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
00547 return new ActionAliasAssignableDataSource( action->copy(alreadyCloned), alias->copy(alreadyCloned) );
00548 }
00549 };
00550
00565 template<typename BoundType>
00566 class UnboundDataSource
00567 : public BoundType
00568 {
00569 public:
00570 typedef typename BoundType::result_t T;
00571 typedef boost::intrusive_ptr< UnboundDataSource<BoundType> > shared_ptr;
00572
00573 UnboundDataSource( T data );
00574
00575 UnboundDataSource( );
00576
00577 ~UnboundDataSource() {
00578 }
00579
00580 virtual BoundType* clone() const {
00581 return BoundType::clone();
00582 }
00583
00584 virtual UnboundDataSource<BoundType>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace) const;
00585 };
00586
00599 template<typename function>
00600 class BinaryDataSource
00601 : public DataSource< typename remove_cr<typename function::result_type>::type >
00602 {
00603 typedef typename remove_cr<typename function::result_type>::type value_t;
00604 typedef typename remove_cr<typename function::first_argument_type>::type first_arg_t;
00605 typedef typename remove_cr<typename function::second_argument_type>::type second_arg_t;
00606 typename DataSource<first_arg_t>::shared_ptr mdsa;
00607 typename DataSource<second_arg_t>::shared_ptr mdsb;
00608 function fun;
00609 mutable value_t mdata;
00610 public:
00611 typedef boost::intrusive_ptr<BinaryDataSource<function> > shared_ptr;
00612
00617 BinaryDataSource( typename DataSource<first_arg_t>::shared_ptr a,
00618 typename DataSource<second_arg_t>::shared_ptr b,
00619 function f )
00620 : mdsa( a ), mdsb( b ), fun( f )
00621 {
00622 }
00623
00624 virtual value_t get() const
00625 {
00626 first_arg_t a = mdsa->get();
00627 second_arg_t b = mdsb->get();
00628 return mdata = fun( a, b );
00629 }
00630
00631 virtual value_t value() const
00632 {
00633 return mdata;
00634 }
00635
00636 typename DataSource<value_t>::const_reference_t rvalue() const
00637 {
00638 return mdata;
00639 }
00640
00641 virtual void reset()
00642 {
00643 mdsa->reset();
00644 mdsb->reset();
00645 }
00646
00647 virtual BinaryDataSource<function>* clone() const
00648 {
00649 return new BinaryDataSource<function>(mdsa.get(), mdsb.get(), fun);
00650 }
00651
00652 virtual BinaryDataSource<function>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
00653 return new BinaryDataSource<function>( mdsa->copy( alreadyCloned ), mdsb->copy( alreadyCloned ), fun );
00654 }
00655 };
00656
00662 template <typename function>
00663 class UnaryDataSource
00664 : public DataSource<typename remove_cr<typename function::result_type>::type>
00665 {
00666 typedef typename remove_cr<typename function::result_type>::type value_t;
00667 typedef typename remove_cr<typename function::argument_type>::type arg_t;
00668 typename DataSource<arg_t>::shared_ptr mdsa;
00669 function fun;
00670 mutable value_t mdata;
00671 public:
00672 typedef boost::intrusive_ptr<UnaryDataSource<function> > shared_ptr;
00673
00678 UnaryDataSource( typename DataSource<arg_t>::shared_ptr a, function f )
00679 : mdsa( a ), fun( f )
00680 {
00681 }
00682
00683 virtual value_t get() const
00684 {
00685 return mdata = fun( mdsa->get() );
00686 }
00687
00688 virtual value_t value() const
00689 {
00690 return mdata;
00691 }
00692
00693 typename DataSource<value_t>::const_reference_t rvalue() const
00694 {
00695 return mdata;
00696 }
00697
00698
00699 void reset()
00700 {
00701 mdsa->reset();
00702 }
00703
00704 virtual UnaryDataSource<function>* clone() const
00705 {
00706 return new UnaryDataSource<function>(mdsa.get(), fun);
00707 }
00708
00709 virtual UnaryDataSource<function>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
00710 return new UnaryDataSource<function>( mdsa->copy( alreadyCloned ), fun );
00711 }
00712 };
00713
00721 template<typename function>
00722 class NArityDataSource
00723 : public DataSource<typename remove_cr<typename function::result_type>::type>
00724 {
00725 typedef typename remove_cr<typename function::result_type>::type value_t;
00726 typedef typename remove_cr<typename function::argument_type>::type arg_t;
00727 mutable std::vector<arg_t> margs;
00728 std::vector<typename DataSource<arg_t>::shared_ptr > mdsargs;
00729 function fun;
00730 mutable value_t mdata;
00731 public:
00732 typedef boost::intrusive_ptr<NArityDataSource<function> > shared_ptr;
00733
00738 NArityDataSource( function f = function() )
00739 : fun( f )
00740 {
00741 }
00742
00747 NArityDataSource( function f, const std::vector<typename DataSource<arg_t>::shared_ptr >& dsargs )
00748 : margs( dsargs.size() ), mdsargs(dsargs), fun( f )
00749 {
00750 }
00751
00752 void add( typename DataSource<arg_t>::shared_ptr ds ) {
00753 mdsargs.push_back(ds);
00754 margs.push_back( ds->value() );
00755 }
00756
00757 virtual value_t get() const
00758 {
00759 assert( mdsargs.size() == margs.size() );
00760 for( unsigned int i=0; i !=mdsargs.size(); ++i)
00761 margs[i] = mdsargs[i]->get();
00762 return mdata = fun( margs );
00763 }
00764
00765 virtual value_t value() const
00766 {
00767 return mdata;
00768 }
00769
00770 typename DataSource<value_t>::const_reference_t rvalue() const
00771 {
00772 return mdata;
00773 }
00774
00775 virtual void reset()
00776 {
00777 for( unsigned int i=0; i !=mdsargs.size(); ++i)
00778 mdsargs[i]->reset();
00779 }
00780
00781 virtual NArityDataSource<function>* clone() const
00782 {
00783 return new NArityDataSource<function>(fun, mdsargs);
00784 }
00785
00786 virtual NArityDataSource<function>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
00787 std::vector<typename DataSource<arg_t>::shared_ptr > newargs( mdsargs.size() );
00788 for( unsigned int i=0; i !=mdsargs.size(); ++i)
00789 newargs[i] = mdsargs[i]->copy(alreadyCloned);
00790 return new NArityDataSource<function>( fun, newargs );
00791 }
00792 };
00793 }
00794 }
00795
00796 #include "DataSources.inl"
00797
00798 #endif
00799