Orocos Real-Time Toolkit  2.9.0
DataSourceProxy.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 DataSourceProxy.hpp
3 
4  DataSourceProxy.hpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
8  email : peter@thesourceworks.com
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #ifndef ORO_CORBA_DATASOURCE_PROXY_HPP
40 #define ORO_CORBA_DATASOURCE_PROXY_HPP
41 
42 #include "../../internal/DataSource.hpp"
43 #include "../../Logger.hpp"
44 #include "../../base/ActionInterface.hpp"
45 #include "CorbaConversion.hpp"
46 #include "CorbaTypeTransporter.hpp"
47 #include "CorbaLib.hpp"
48 #include <cassert>
49 
50 namespace RTT
51 {
52  namespace corba
53  {
60  {
61  };
65  template<class T>
67  : public internal::DataSource<T>
68  {
69  corba::CService_var mserv;
70  const std::string mname;
71  bool misproperty;
72  mutable typename internal::DataSource<T>::value_t last_value;
74 
75  public:
85  DataSourceProxy( corba::CService_ptr s, const std::string& name, bool isproperty )
86  : mserv( corba::CService::_duplicate( s ) ), mname(name), misproperty(isproperty)
87  {
88  assert( !CORBA::is_nil(s) );
89  types::TypeTransporter* tp = this->getTypeInfo()->getProtocol(ORO_CORBA_PROTOCOL_ID);
90  ctp = dynamic_cast<corba::CorbaTypeTransporter*>(tp);
91  assert( ctp ); // only call this from CorbaTempateTypeInfo.
92 #if 0
93  if ( misproperty && ! mserv->hasProperty( name.c_str()))
94  throw NonExistingDataSource();
95  if ( !misproperty && ! mserv->hasAttribute( name.c_str()))
96  throw NonExistingDataSource();
97 #endif
98  }
99 
101  return last_value;
102  }
103 
105  return last_value;
106  }
107 
108  virtual typename internal::DataSource<T>::result_t get() const {
109  CORBA::Any_var res;
110  if ( misproperty ) {
111  res = mserv->getProperty( mname.c_str() );
112  } else {
113  res = mserv->getAttribute( mname.c_str() );
114  }
115  internal::ReferenceDataSource<T> rds(last_value);
116  rds.ref();
117  if ( ctp->updateFromAny(&res.in(),&rds ) == false)
118  Logger::log() <<Logger::Error << "Could not update DataSourceProxy from remote value!"<<Logger::endl;
119  return last_value;
120  }
121 
122  virtual internal::DataSource<T>* clone() const {
123  return new DataSourceProxy<T>( corba::CService::_duplicate( mserv.in() ), mname, misproperty );
124  }
125 
126  virtual internal::DataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
127  alreadyCloned[this] = const_cast<DataSourceProxy<T>*>(this);
128  return const_cast<DataSourceProxy<T>*>(this);
129  }
130 
131  virtual std::string getType() const {
132  // both should be equivalent, but we display the local type.
134  //return std::string( mserv->getType() );
135  }
136 
137  };
138 
142  template<class T>
145  {
146  typedef typename internal::AssignableDataSource<T>::value_t value_t;
147  corba::CService_var mserv;
148  const std::string mname;
149  bool misproperty;
152  //mutable typename internal::DataSource<T>::value_t last_value;
153 
154  public:
155  ValueDataSourceProxy( corba::CService_ptr serv, const std::string& name, bool isproperty)
156  : mserv( corba::CService::_duplicate(serv) ), mname(name), misproperty(isproperty)
157  {
158  storage = new internal::ValueDataSource<value_t>();
159  assert( serv );
160  types::TypeTransporter* tp = this->getTypeInfo()->getProtocol(ORO_CORBA_PROTOCOL_ID);
161  ctp = dynamic_cast<corba::CorbaTypeTransporter*>(tp);
162  assert(ctp);
163 #if 0
164  if ( misproperty && !mserv->hasProperty( name.c_str()) )
165  throw NonExistingDataSource();
166  if ( !misproperty && ( !mserv->hasAttribute( name.c_str()) || !mserv->isAttributeAssignable( name.c_str()) ))
167  throw NonExistingDataSource();
168  this->get(); // initialize such that value()/rvalue() return a sane value !
169 #endif
170  }
171 
173  return storage->rvalue();
174  }
175 
177  return storage->rvalue();
178  }
179 
180 
181  virtual typename internal::DataSource<T>::result_t get() const {
182  CORBA::Any_var res;
183  if ( misproperty ) {
184  res = mserv->getProperty( mname.c_str() );
185  } else {
186  res = mserv->getAttribute( mname.c_str() );
187  }
188  internal::ReferenceDataSource<T> rds( storage->set() );
189  rds.ref();
190  if ( ctp->updateFromAny(&res.in(), &rds ) == false)
191  Logger::log() <<Logger::Error << "Could not update ValueDataSourceProxy from remote value!"<<Logger::endl;
192  return storage->rvalue();
193  }
194 
195  virtual void set( typename internal::AssignableDataSource<T>::param_t t ) {
197  vds.ref();
198  CORBA::Any_var toset = ctp->createAny(&vds);
199  if ( misproperty ) {
200  mserv->setProperty( mname.c_str(), toset.in() );
201  } else {
202  mserv->setAttribute( mname.c_str(), toset.in() );
203  }
204  storage->set( t );
205  }
206 
208  this->get();
209  return storage->set();
210  }
211 
212  virtual void updated()
213  {
214  this->set( storage->value() );
215  }
216 
218  return new ValueDataSourceProxy<T>( corba::CService::_duplicate( mserv.in() ), mname, misproperty );
219  }
220 
221  virtual internal::AssignableDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
222  alreadyCloned[this] = const_cast<ValueDataSourceProxy<T>*>(this);
223  return const_cast<ValueDataSourceProxy<T>*>(this);
224  }
225  };
226 
227  }
228 }
229 
230 #endif
virtual internal::AssignableDataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
Create a deep copy of this internal::DataSource, unless it is already cloned.
DataSource is a base class representing a generic way to read data of type T.
Definition: DataSource.hpp:94
internal::DataSource< T >::result_t value() const
Return the result of the last evaluate() function.
virtual internal::DataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
Create a deep copy of this internal::DataSource, unless it is already cloned.
This interface defines the function a transport protocol must support in order to allow Orocos compon...
boost::call_traits< value_t >::reference reference_t
Definition: DataSource.hpp:193
virtual void set(param_t t)=0
Set this DataSource with a value.
virtual CORBA::Any_ptr createAny(base::DataSourceBase::shared_ptr source) const =0
Evaluate source and create an any which contains the value of source.
T value_t
The bare type of T is extracted into value_t.
Definition: DataSource.hpp:105
virtual void updated()
In case the internal::DataSource returns a &#39;reference&#39; type, call this method to notify it that the d...
A DataSource which is used to manipulate a reference to an external value.
ValueDataSourceProxy(corba::CService_ptr serv, const std::string &name, bool isproperty)
An Orocos Service which hosts operations, attributes and properties.
Definition: Service.idl:44
Mirrors a remote assignable value datasource.
virtual internal::DataSource< T > * clone() const
Return a shallow clone of this DataSource.
DataSourceProxy(corba::CService_ptr s, const std::string &name, bool isproperty)
Creates a new DataSource proxy for an attribute or property.
internal::AssignableDataSource< T >::const_reference_t rvalue() const
Get a const reference to the value of this DataSource.
virtual const_reference_t rvalue() const =0
Get a const reference to the value of this DataSource.
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
Gets thrown by the constructor of DataSourceProxy or ValueDataSourceProxy when invalid service or pro...
virtual std::string getType() const
Return useful type info in a human readable format.
internal::AssignableDataSource< T >::const_reference_t rvalue() const
Get a const reference to the value of this DataSource.
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Use this type to store a pointer to an AssignableDataSource.
Definition: DataSource.hpp:198
A DataSource which has set() methods.
Definition: DataSource.hpp:184
#define ORO_CORBA_PROTOCOL_ID
Definition: CorbaLib.hpp:45
static Logger & log()
As Instance(), but more userfriendly.
Definition: Logger.cpp:117
virtual result_t value() const =0
Return the result of the last evaluate() function.
DataSource< T >::value_t value_t
Definition: DataSource.hpp:190
virtual bool updateFromAny(const CORBA::Any *blob, base::DataSourceBase::shared_ptr target) const =0
Update an assignable datasource target with the contents of blob.
DataSource< T >::const_reference_t const_reference_t
Definition: DataSource.hpp:191
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
virtual internal::AssignableDataSource< T > * clone() const
Return a shallow clone of this DataSource.
void ref() const
Increase the reference count by one.
Definition: DataSource.cpp:80
A simple, yet very useful DataSource, which keeps a value, and returns it in its get() method...
Definition: DataSources.hpp:60
Mirrors a remote DataSource.
static std::string GetType()
Return usefull type info in a human readable format.
Definition: DataSource.inl:32
Extends the TypeTransporter in order to allow the creation of channel elements or output halves for a...
boost::call_traits< value_t >::param_type param_t
Definition: DataSource.hpp:192
internal::DataSource< T >::result_t value() const
Return the result of the last evaluate() function.