Orocos Real-Time Toolkit  2.9.0
SharedConnection.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Intermodalics Thu Jul 30 18:28:12 CEST 2015 SharedConnection.hpp
3 
4  SharedConnection.hpp - description
5  -------------------
6  begin : Thu July 30 2015
7  copyright : (C) 2015 Intermodalics
8  email : johannes@intermodalics.eu
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 #ifndef ORO_SHARED_CONNECTION_HPP
39 #define ORO_SHARED_CONNECTION_HPP
40 
41 #include "ConnID.hpp"
42 #include "../base/ChannelElement.hpp"
43 #include "../ConnPolicy.hpp"
44 
45 #include <map>
46 
47 
48 std::ostream &operator<<(std::ostream &, const RTT::internal::SharedConnID &);
49 
50 namespace RTT {
51 namespace internal {
52 
56  struct RTT_API SharedConnID : public ConnID
57  {
58  boost::intrusive_ptr< SharedConnectionBase > connection;
59  SharedConnID(boost::intrusive_ptr< SharedConnectionBase > connection)
60  : connection(connection) {}
61  virtual ConnID* clone() const;
62  virtual bool isSameID(ConnID const& id) const;
63  template <typename T> boost::intrusive_ptr< SharedConnection<T> > getConnection() const
64  {
65  return boost::static_pointer_cast< SharedConnection<T> >(connection);
66  }
67  };
68 
73  {
74  public:
75  typedef boost::intrusive_ptr< SharedConnectionBase > shared_ptr;
76 
77  private:
78  ConnPolicy policy;
79 
80  public:
81  SharedConnectionBase(const ConnPolicy &policy);
82  virtual ~SharedConnectionBase();
83 
87  template <typename T> static SharedConnection<T> *narrow(ChannelElementBase *e)
88  {
89  return dynamic_cast<SharedConnection<T> *>(e);
90  }
91 
95  template <typename T> SharedConnection<T> *narrow()
96  {
97  return dynamic_cast<SharedConnection<T> *>(this);
98  }
99 
100  virtual SharedConnID *getConnID();
101  virtual const std::string &getName() const;
102 
103  virtual const ConnPolicy *getConnPolicy() const;
104  };
105 
110  {
111  public:
112  typedef boost::shared_ptr<SharedConnectionRepository> shared_ptr;
113 
114  typedef std::string key_t;
115  typedef std::map<key_t, SharedConnectionBase*> Map;
116 
117  private:
118  mutable RTT::os::SharedMutex mutex;
119  Map map;
120 
121  public:
122  static shared_ptr Instance();
123 
124  bool add(const key_t &key, SharedConnectionBase* connection);
125  void remove(SharedConnectionBase* connection);
126 
127  bool has(const key_t &key) const;
128  SharedConnectionBase::shared_ptr get(const key_t &key) const;
129 
130  private:
132  };
133 
134  template <typename T>
136  public:
137  typedef boost::intrusive_ptr< SharedConnection<T> > shared_ptr;
141 
142  private:
143  typename base::ChannelElement<T>::shared_ptr mstorage;
144  bool mstorage_initialized;
145 
146  public:
147  SharedConnection(typename base::ChannelElement<T> *storage, const ConnPolicy &policy)
148  : SharedConnectionBase(policy)
149  , mstorage(storage)
150  , mstorage_initialized(false)
151  {
152  assert(policy.buffer_policy == Shared);
153  }
154  virtual ~SharedConnection() {}
155 
157 
164  virtual WriteStatus write(param_t sample)
165  {
166  WriteStatus result = mstorage->write(sample);
167  if (result == WriteSuccess) {
168  if (!this->signal()) {
169  return WriteFailure;
170  }
171  }
172  return result;
173  }
174 
180  virtual FlowStatus read(reference_t sample, bool copy_old_data = true)
181  {
182  return mstorage->read(sample, copy_old_data);
183  }
184 
189  virtual void clear()
190  {
191  mstorage->clear();
193  }
194 
203  virtual WriteStatus data_sample(param_t sample, bool reset = true)
204  {
205  // ignore reset, only the first caller can initialize the data/buffer object
206  if (!mstorage_initialized) {
207  mstorage->data_sample(sample, reset);
208  mstorage_initialized = true;
209  }
211  }
212 
213  virtual value_t data_sample()
214  {
215  return mstorage->data_sample();
216  }
217  };
218 
219  template <typename T>
221  public:
222  typedef boost::intrusive_ptr< SharedRemoteConnection<T> > shared_ptr;
223 
224  public:
226  : SharedConnectionBase(policy)
227  {}
229  };
230 }}
231 
232 #endif
boost::call_traits< T >::param_type param_t
boost::intrusive_ptr< ChannelElement< T > > shared_ptr
A repository which stores pointers to all shared connections within the process.
boost::intrusive_ptr< SharedConnectionBase > shared_ptr
virtual void clear()
Clears any data stored by the channel.
virtual FlowStatus read(reference_t sample, bool copy_old_data=true)
Reads the last sample given to write()
boost::intrusive_ptr< SharedConnection< T > > getConnection() const
FlowStatus
Returns the status of a data flow read operation.
Definition: FlowStatus.hpp:56
#define RTT_API
Definition: rtt-config.h:97
static SharedConnection< T > * narrow(ChannelElementBase *e)
Return a pointer to the typed instance of a SharedConnectionBase.
A connection policy object describes how a given connection should behave.
Definition: ConnPolicy.hpp:107
std::map< key_t, SharedConnectionBase * > Map
virtual WriteStatus write(param_t sample)
Writes a new sample on this connection.
boost::intrusive_ptr< SharedConnectionBase > connection
virtual void clear()
Resets the stored sample.
virtual value_t data_sample()
Overridden implementation of MultipleInputsChannelElementBase::data_sample() that gets a sample from ...
A typed version of ChannelElementBase.
virtual WriteStatus data_sample(param_t sample, bool reset=true)
Provides a data sample to initialize this connection.
SharedRemoteConnection(const ConnPolicy &policy)
SharedConnection< T > * narrow()
Return a pointer to the typed variant of this SharedConnectionBase.
base::ChannelElement< T >::param_t param_t
virtual FlowStatus read(reference_t sample, bool copy_old_data=true)
Reads a sample from the connection.
Represents a shared connection created by the ConnFactory.
An object oriented wrapper around a shared mutex (multiple readers allowed, but only one writer with ...
Definition: Mutex.hpp:346
boost::call_traits< T >::reference reference_t
boost::intrusive_ptr< SharedRemoteConnection< T > > shared_ptr
base::ChannelElement< T >::reference_t reference_t
boost::intrusive_ptr< SharedConnection< T > > shared_ptr
SharedConnID(boost::intrusive_ptr< SharedConnectionBase > connection)
int buffer_policy
The policy on how buffer elements will be installed for this connection, which influences the behavio...
Definition: ConnPolicy.hpp:216
boost::shared_ptr< SharedConnectionRepository > shared_ptr
virtual value_t data_sample()
Overridden implementation of MultipleInputsChannelElementBase::data_sample() that gets a sample from ...
virtual WriteStatus data_sample(param_t sample, bool reset=true)
Provides a data sample to initialize this connection.
This class is used in places where a permanent representation of a reference to a connection is neede...
Definition: ConnID.hpp:58
SharedConnection(typename base::ChannelElement< T > *storage, const ConnPolicy &policy)
virtual WriteStatus write(param_t sample)
Writes a new sample on this connection.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
base::ChannelElement< T >::value_t value_t
In the data flow implementation, a channel is created by chaining ChannelElementBase objects...
A typed version of MultipleInputsMultipleOutputsChannelElementBase.
std::ostream & operator<<(std::ostream &, const RTT::internal::SharedConnID &)
WriteStatus
Returns the status of a data flow write operation.
Definition: FlowStatus.hpp:66
Base class for shared connection elements.