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_CORBA_DATASOURCE_PROXY_HPP
00040 #define ORO_CORBA_DATASOURCE_PROXY_HPP
00041
00042 #include "../../internal/DataSource.hpp"
00043 #include "../../Logger.hpp"
00044 #include "../../base/ActionInterface.hpp"
00045 #include "CorbaConversion.hpp"
00046 #include "CorbaTypeTransporter.hpp"
00047 #include "CorbaLib.hpp"
00048 #include <cassert>
00049
00050 namespace RTT
00051 {
00052 namespace corba
00053 {
00059 struct NonExistingDataSource
00060 {
00061 };
00065 template<class T>
00066 class DataSourceProxy
00067 : public internal::DataSource<T>
00068 {
00069 corba::CService_var mserv;
00070 const std::string mname;
00071 bool misproperty;
00072 mutable typename internal::DataSource<T>::value_t last_value;
00073 CorbaTypeTransporter* ctp;
00074
00075 public:
00085 DataSourceProxy( corba::CService_ptr s, const std::string& name, bool isproperty )
00086 : mserv( corba::CService::_duplicate( s ) ), mname(name), misproperty(isproperty)
00087 {
00088 assert( !CORBA::is_nil(s) );
00089 types::TypeTransporter* tp = this->getTypeInfo()->getProtocol(ORO_CORBA_PROTOCOL_ID);
00090 ctp = dynamic_cast<corba::CorbaTypeTransporter*>(tp);
00091 assert( ctp );
00092 if ( misproperty && ! mserv->hasProperty( name.c_str()))
00093 throw NonExistingDataSource();
00094 if ( !misproperty && ! mserv->hasAttribute( name.c_str()))
00095 throw NonExistingDataSource();
00096 }
00097
00098 typename internal::DataSource<T>::result_t value() const {
00099 return last_value;
00100 }
00101
00102 typename internal::AssignableDataSource<T>::const_reference_t rvalue() const {
00103 return last_value;
00104 }
00105
00106 virtual typename internal::DataSource<T>::result_t get() const {
00107 CORBA::Any_var res;
00108 if ( misproperty ) {
00109 res = mserv->getProperty( mname.c_str() );
00110 } else {
00111 res = mserv->getAttribute( mname.c_str() );
00112 }
00113 internal::ReferenceDataSource<T> rds(last_value);
00114 rds.ref();
00115 if ( ctp->updateFromAny(&res.in(),&rds ) == false)
00116 Logger::log() <<Logger::Error << "Could not update DataSourceProxy from remote value!"<<Logger::endl;
00117 return last_value;
00118 }
00119
00120 virtual internal::DataSource<T>* clone() const {
00121 return new DataSourceProxy<T>( corba::CService::_duplicate( mserv.in() ), mname, misproperty );
00122 }
00123
00124 virtual internal::DataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
00125 alreadyCloned[this] = const_cast<DataSourceProxy<T>*>(this);
00126 return const_cast<DataSourceProxy<T>*>(this);
00127 }
00128
00129 virtual std::string getType() const {
00130
00131 return internal::DataSource<T>::GetType();
00132
00133 }
00134
00135 };
00136
00140 template<class T>
00141 class ValueDataSourceProxy
00142 : public internal::AssignableDataSource<T>
00143 {
00144 typedef typename internal::AssignableDataSource<T>::value_t value_t;
00145 corba::CService_var mserv;
00146 const std::string mname;
00147 bool misproperty;
00148 typename internal::AssignableDataSource<value_t>::shared_ptr storage;
00149 CorbaTypeTransporter* ctp;
00150
00151
00152 public:
00153 ValueDataSourceProxy( corba::CService_ptr serv, const std::string& name, bool isproperty)
00154 : mserv( corba::CService::_duplicate(serv) ), mname(name), misproperty(isproperty)
00155 {
00156 storage = new internal::ValueDataSource<value_t>();
00157 assert( serv );
00158 types::TypeTransporter* tp = this->getTypeInfo()->getProtocol(ORO_CORBA_PROTOCOL_ID);
00159 ctp = dynamic_cast<corba::CorbaTypeTransporter*>(tp);
00160 assert(ctp);
00161 if ( misproperty && !mserv->hasProperty( name.c_str()) )
00162 throw NonExistingDataSource();
00163 if ( !misproperty && ( !mserv->hasAttribute( name.c_str()) || !mserv->isAttributeAssignable( name.c_str()) ))
00164 throw NonExistingDataSource();
00165 this->get();
00166 }
00167
00168 typename internal::DataSource<T>::result_t value() const {
00169 return storage->rvalue();
00170 }
00171
00172 typename internal::AssignableDataSource<T>::const_reference_t rvalue() const {
00173 return storage->rvalue();
00174 }
00175
00176
00177 virtual typename internal::DataSource<T>::result_t get() const {
00178 CORBA::Any_var res;
00179 if ( misproperty ) {
00180 res = mserv->getProperty( mname.c_str() );
00181 } else {
00182 res = mserv->getAttribute( mname.c_str() );
00183 }
00184 internal::ReferenceDataSource<T> rds( storage->set() );
00185 rds.ref();
00186 if ( ctp->updateFromAny(&res.in(), &rds ) == false)
00187 Logger::log() <<Logger::Error << "Could not update ValueDataSourceProxy from remote value!"<<Logger::endl;
00188 return storage->rvalue();
00189 }
00190
00191 virtual void set( typename internal::AssignableDataSource<T>::param_t t ) {
00192 internal::ValueDataSource<T> vds(t);
00193 vds.ref();
00194 CORBA::Any_var toset = ctp->createAny(&vds);
00195 if ( misproperty ) {
00196 mserv->setProperty( mname.c_str(), toset.in() );
00197 } else {
00198 mserv->setAttribute( mname.c_str(), toset.in() );
00199 }
00200 storage->set( t );
00201 }
00202
00203 virtual typename internal::AssignableDataSource<T>::reference_t set() {
00204 this->get();
00205 return storage->set();
00206 }
00207
00208 virtual void updated()
00209 {
00210 this->set( storage->value() );
00211 }
00212
00213 virtual internal::AssignableDataSource<T>* clone() const {
00214 return new ValueDataSourceProxy<T>( corba::CService::_duplicate( mserv.in() ), mname, misproperty );
00215 }
00216
00217 virtual internal::AssignableDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
00218 alreadyCloned[this] = const_cast<ValueDataSourceProxy<T>*>(this);
00219 return const_cast<ValueDataSourceProxy<T>*>(this);
00220 }
00221 };
00222
00223 }
00224 }
00225
00226 #endif