Orocos Real-Time Toolkit  2.8.3
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 ( misproperty && ! mserv->hasProperty( name.c_str()))
93  throw NonExistingDataSource();
94  if ( !misproperty && ! mserv->hasAttribute( name.c_str()))
95  throw NonExistingDataSource();
96  }
97 
99  return last_value;
100  }
101 
103  return last_value;
104  }
105 
106  virtual typename internal::DataSource<T>::result_t get() const {
107  CORBA::Any_var res;
108  if ( misproperty ) {
109  res = mserv->getProperty( mname.c_str() );
110  } else {
111  res = mserv->getAttribute( mname.c_str() );
112  }
113  internal::ReferenceDataSource<T> rds(last_value);
114  rds.ref();
115  if ( ctp->updateFromAny(&res.in(),&rds ) == false)
116  Logger::log() <<Logger::Error << "Could not update DataSourceProxy from remote value!"<<Logger::endl;
117  return last_value;
118  }
119 
120  virtual internal::DataSource<T>* clone() const {
121  return new DataSourceProxy<T>( corba::CService::_duplicate( mserv.in() ), mname, misproperty );
122  }
123 
124  virtual internal::DataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
125  alreadyCloned[this] = const_cast<DataSourceProxy<T>*>(this);
126  return const_cast<DataSourceProxy<T>*>(this);
127  }
128 
129  virtual std::string getType() const {
130  // both should be equivalent, but we display the local type.
132  //return std::string( mserv->getType() );
133  }
134 
135  };
136 
140  template<class T>
143  {
144  typedef typename internal::AssignableDataSource<T>::value_t value_t;
145  corba::CService_var mserv;
146  const std::string mname;
147  bool misproperty;
150  //mutable typename internal::DataSource<T>::value_t last_value;
151 
152  public:
153  ValueDataSourceProxy( corba::CService_ptr serv, const std::string& name, bool isproperty)
154  : mserv( corba::CService::_duplicate(serv) ), mname(name), misproperty(isproperty)
155  {
156  storage = new internal::ValueDataSource<value_t>();
157  assert( serv );
158  types::TypeTransporter* tp = this->getTypeInfo()->getProtocol(ORO_CORBA_PROTOCOL_ID);
159  ctp = dynamic_cast<corba::CorbaTypeTransporter*>(tp);
160  assert(ctp);
161  if ( misproperty && !mserv->hasProperty( name.c_str()) )
162  throw NonExistingDataSource();
163  if ( !misproperty && ( !mserv->hasAttribute( name.c_str()) || !mserv->isAttributeAssignable( name.c_str()) ))
164  throw NonExistingDataSource();
165  this->get(); // initialize such that value()/rvalue() return a sane value !
166  }
167 
169  return storage->rvalue();
170  }
171 
173  return storage->rvalue();
174  }
175 
176 
177  virtual typename internal::DataSource<T>::result_t get() const {
178  CORBA::Any_var res;
179  if ( misproperty ) {
180  res = mserv->getProperty( mname.c_str() );
181  } else {
182  res = mserv->getAttribute( mname.c_str() );
183  }
184  internal::ReferenceDataSource<T> rds( storage->set() );
185  rds.ref();
186  if ( ctp->updateFromAny(&res.in(), &rds ) == false)
187  Logger::log() <<Logger::Error << "Could not update ValueDataSourceProxy from remote value!"<<Logger::endl;
188  return storage->rvalue();
189  }
190 
191  virtual void set( typename internal::AssignableDataSource<T>::param_t t ) {
193  vds.ref();
194  CORBA::Any_var toset = ctp->createAny(&vds);
195  if ( misproperty ) {
196  mserv->setProperty( mname.c_str(), toset.in() );
197  } else {
198  mserv->setAttribute( mname.c_str(), toset.in() );
199  }
200  storage->set( t );
201  }
202 
204  this->get();
205  return storage->set();
206  }
207 
208  virtual void updated()
209  {
210  this->set( storage->value() );
211  }
212 
214  return new ValueDataSourceProxy<T>( corba::CService::_duplicate( mserv.in() ), mname, misproperty );
215  }
216 
217  virtual internal::AssignableDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
218  alreadyCloned[this] = const_cast<ValueDataSourceProxy<T>*>(this);
219  return const_cast<ValueDataSourceProxy<T>*>(this);
220  }
221  };
222 
223  }
224 }
225 
226 #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:22
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:51
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.