Orocos Real-Time Toolkit  2.8.3
signal_template.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Wed Jan 18 14:11:38 CET 2006 signal_template.hpp
3 
4  signal_template.hpp - description
5  -------------------
6  begin : Wed January 18 2006
7  copyright : (C) 2006 Peter Soetens
8  email : peter.soetens@mech.kuleuven.be
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 
40 #ifndef OROCOS_SIGNAL_TEMPLATE_HEADER_INCLUDED
41 #define OROCOS_SIGNAL_TEMPLATE_HEADER_INCLUDED
42 #include "SignalBase.hpp"
43 #include "NA.hpp"
44 
45 #ifdef ORO_SIGNAL_USE_LIST_LOCK_FREE
46 #include <boost/bind.hpp>
47 #else
48 #include "../os/MutexLock.hpp"
49 #endif
50 #endif // !OROCOS_SIGNAL_TEMPLATE_HEADER_INCLUDED
51 
52 // Define class names used
53 #define OROCOS_SIGNAL_N BOOST_JOIN(signal,OROCOS_SIGNATURE_NUM_ARGS)
54 #define OROCOS_SIGNAL_CONNECTION_N BOOST_JOIN(connection,OROCOS_SIGNATURE_NUM_ARGS)
55 
56 
57 namespace RTT {
58 
59  namespace internal {
60 
61  template<class SlotFunction>
63  {
64  public:
65  typedef SlotFunction slot_function;
66  typedef SlotFunction function_type;
68 
69  OROCOS_SIGNAL_CONNECTION_N(SignalBase* s, const slot_function& f)
70  : ConnectionBase(s), func(f)
71  {
72  }
73 
75  {
76  if (this->mconnected)
78  }
79  private:
80  slot_function func;
81  };
82 
84  class SlotFunctionT = OROCOS_SIGNATURE_FUNCTION_N< R OROCOS_SIGNATURE_COMMA_IF_NONZERO_ARGS OROCOS_SIGNATURE_TEMPLATE_ARGS> >
86  : public SignalBase
87  {
89 
90  public:
91  typedef SlotFunctionT slot_function_type;
93 
94  typedef R result_type;
96 
97 #if OROCOS_SIGNATURE_NUM_ARGS == 1
98  typedef arg1_type first_argument_type;
99 #endif
100 #if OROCOS_SIGNATURE_NUM_ARGS == 2
101  typedef arg1_type first_argument_type;
102  typedef arg2_type second_argument_type;
103 #endif
104 
106  {
107  }
108 
109  Handle connect(const slot_function_type& f )
110  {
111  Handle h = this->setup(f);
112  h.connect();
113  return h;
114  }
115 
116  Handle setup(const slot_function_type& f )
117  {
118  connection_t conn(
119  new connection_impl(this, f) );
120  this->conn_setup( conn );
121  return Handle(conn);
122  }
123 
124  private:
125  static void emitImpl(const connection_t& c
128 #endif
129  )
130  {
131  static_cast<connection_impl*>(c.get())->emit(OROCOS_SIGNATURE_ARGS);
132  }
133 
134  public:
136  {
137 #ifdef ORO_SIGNAL_USE_LIST_LOCK_FREE
138  this->emitting = true;
139 
140  // this code did initially not work under gcc 4.0/ubuntu breezy.
141  // connection_t::get() const becomes an undefined symbol.
142  // works under gcc 3.4
143  mconnections.apply( boost::bind(&OROCOS_SIGNAL_N::emitImpl,
144  _1
147 #endif
148  ) );
149  this->emitting = false;
150 #else
151  os::MutexLock lock(m);
152  if (this->emitting)
153  return NA<R>::na(); // avoid uglyness : Handlers calling emit.
154  this->emitting = true;
155  iterator it = mconnections.begin();
156  const_iterator end = mconnections.end();
157  for (; it != end; ++it ) {
158  connection_impl* ci = static_cast<connection_impl*>( it->get() );
159  if (ci)
160  ci->emit(OROCOS_SIGNATURE_ARGS); // this if... race is guarded by the mutex.
161  }
162  this->emitting = false;
163  this->cleanup();
164 #endif
165  return NA<R>::na();
166  }
167 
169  {
170  return this->emit(OROCOS_SIGNATURE_ARGS);
171  }
172 
174  {
175  return this->emit(OROCOS_SIGNATURE_ARGS);
176  }
177 
178  virtual int arity() const { return OROCOS_SIGNATURE_NUM_ARGS; }
179  };
180 
181 }} // namespace sigslot
182 
183 
184 #undef OROCOS_SIGNAL_N
185 #undef OROCOS_SIGNAL_CONNECTION_N
186 
virtual int arity() const
Returns the arity (the number of arguments) of this signal.
R fire(OROCOS_SIGNATURE_PARMS)
The base signal class which stores connection objects.
Definition: SignalBase.hpp:128
#define OROCOS_SIGNAL_N
Handle setup(const slot_function_type &f)
Handle connect(const slot_function_type &f)
OROCOS_SIGNATURE_TYPEDEFS OROCOS_SIGNAL_CONNECTION_N(SignalBase *s, const slot_function &f)
#define OROCOS_SIGNATURE_TYPEDEFS
Definition: signature0.hpp:45
#define OROCOS_SIGNATURE_PARMS
Definition: signature0.hpp:42
static T na()
Definition: NA.hpp:57
#define OROCOS_SIGNATURE_TEMPLATE_PARMS
Definition: signature0.hpp:40
R emit(OROCOS_SIGNATURE_PARMS)
#define OROCOS_SIGNATURE_ARGS
Definition: signature0.hpp:43
OROCOS_SIGNATURE_ARG_TYPES OROCOS_SIGNAL_N()
OROCOS_SIGNAL_CONNECTION_N< SlotFunctionT > connection_impl
R operator()(OROCOS_SIGNATURE_PARMS)
bool connect()
(Re-)Connect the slot with the signal.
Definition: Handle.cpp:65
#define OROCOS_SIGNATURE_COMMA_IF_NONZERO_ARGS
#define OROCOS_SIGNATURE_NUM_ARGS
Definition: signature0.hpp:39
#define OROCOS_SIGNATURE_ARG_TYPES
Definition: signature0.hpp:44
A connection &#39;memorises&#39; the connection between an event and an event handler function.
Definition: SignalBase.hpp:80
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
The Handle holds the information, and allows manipulation, of a connection between a internal::Signal...
Definition: Handle.hpp:66
ConnectionBase::shared_ptr connection_t
Definition: SignalBase.hpp:131
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...
Definition: MutexLock.hpp:51