cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mtsEventReceiver.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Peter Kazanzides
7  Created on: 2010-09-24
8 
9  (C) Copyright 2010-2014 Johns Hopkins University (JHU), All Rights Reserved.
10 
11 --- begin cisst license - do not edit ---
12 
13 This software is provided "as is" under an open source license, with
14 no warranty. The complete license can be found in license.txt and
15 http://www.cisst.org/cisst/license.txt.
16 
17 --- end cisst license ---
18 */
19 
20 
21 #ifndef _mtsEventReceiver_h
22 #define _mtsEventReceiver_h
23 
87 
88 // Always include last
90 
91 
92 // EventReceivers must be added before Bind (should add check for InterfaceProvidedOrOutput==0)
93 // EventHandlers can be added at any time.
94 // When Bind called,
95 // if no EventReceiver, directly add EventHandler
96 // if EventReceiver, set handler in it
97 
99 protected:
100  std::string Name;
101  mtsInterfaceRequired * Required; // Pointer to the required interface
103  bool Waiting;
104  bool OwnEventSignal; // true if we created our own thread signal
105 
106  bool CheckRequired() const;
107  bool WaitCommon();
108 
109 public:
111  virtual ~mtsEventReceiverBase();
112 
113  void SetName(const std::string &name) { Name = name; }
114  virtual std::string GetName() const { return Name; }
115 
117  virtual void SetRequired(const std::string &name, mtsInterfaceRequired *req);
118 
119  virtual void SetThreadSignal(osaThreadSignal *signal);
120 
123  virtual bool Wait();
124 
127  virtual bool WaitWithTimeout(double timeoutInSec);
128 
129  virtual void Detach();
130 
132  virtual void ToStream(std::ostream & outputStream) const = 0;
133 };
134 
135 
137 inline std::ostream & operator << (std::ostream & output,
138  const mtsEventReceiverBase & receiver) {
139  receiver.ToStream(output);
140  return output;
141 }
142 
144 protected:
145  mtsCommandVoid * Command; // Command object for calling EventHandler method
146  mtsCommandVoid * UserHandler; // User supplied event handler
147 
148  // This one always gets added non-queued
149  void EventHandler(void);
150 
151 public:
154 
156  mtsCommandVoid * GetCommand(void);
157 
159  void SetHandlerCommand(mtsCommandVoid * commandHandler);
160 
161  // Same functionality as mtsInterfaceRequired::AddEventHandlerVoid.
162  template <class __classType>
163  inline mtsCommandVoid * SetHandler(void (__classType::*method)(void),
164  __classType * classInstantiation,
166  return CheckRequired() ? (Required->AddEventHandlerVoid(method, classInstantiation, this->GetName(), queueingPolicy)) : 0;
167  }
168 
169  inline mtsCommandVoid * SetHandler(void (*function)(void),
171  return CheckRequired() ? (Required->AddEventHandlerVoid(function, this->GetName(), queueingPolicy)) : 0;
172  }
173 
174  bool RemoveHandler(void);
175 
177  void ToStream(std::ostream & outputStream) const;
178 };
179 
181 protected:
182  mtsCommandWriteBase *Command; // Command object for calling EventHandler method
183  mtsCommandWriteBase *UserHandler; // User supplied event handler
185 
186  // This one always gets added non-queued
187  void EventHandler(const mtsGenericObject &arg);
188 
189 public:
192 
194  mtsCommandWriteBase *GetCommand();
195 
197  void SetHandlerCommand(mtsCommandWriteBase * commandHandler);
198 
199  // Same functionality as mtsInterfaceRequired::AddEventHandlerWrite.
200  template <class __classType, class __argumentType>
201  inline mtsCommandWriteBase * SetHandler(void (__classType::*method)(const __argumentType &),
202  __classType * classInstantiation,
204  return CheckRequired() ? (Required->AddEventHandlerWrite(method, classInstantiation, this->GetName(), queueingPolicy)) : 0;
205  }
206 
207  // PK: Do we need the "generic" version (AddEventHandlerWriteGeneric)?
208 
209 
210  // Note that we are using the Wait and WaitWithTimeout member functions from the base class.
213 
217  virtual bool Wait(mtsGenericObject &obj);
218  virtual bool WaitWithTimeout(double timeoutInSec, mtsGenericObject &obj);
219 
220  //PK: might be nice to have this
221  //const mtsGenericObject *GetArgumentPrototype() const;
222 
223  bool RemoveHandler(void);
224 
226  void ToStream(std::ostream & outputStream) const;
227 };
228 
229 
230 #endif // _mtsEventReceiver_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
mtsCommandWriteBase * Command
Definition: mtsEventReceiver.h:182
Definition: mtsEventReceiver.h:143
virtual std::string GetName() const
Definition: mtsEventReceiver.h:114
bool CheckRequired() const
Declaration of mtsInterfaceRequired.
Definition: mtsCommandVoid.h:44
void SetName(const std::string &name)
Definition: mtsEventReceiver.h:113
Definition: mtsForwardDeclarations.h:49
mtsCommandVoid * SetHandler(void(*function)(void), mtsEventQueueingPolicy queueingPolicy=MTS_INTERFACE_EVENT_POLICY)
Definition: mtsEventReceiver.h:169
Definition: mtsInterfaceRequired.h:85
bool Waiting
Definition: mtsEventReceiver.h:103
Definition: mtsEventReceiver.h:98
virtual bool Wait()
std::string Name
Definition: mtsEventReceiver.h:100
Base class for data object in cisstMultiTask.
Definition: mtsGenericObject.h:56
Definition: mtsEventReceiver.h:180
mtsEventQueueingPolicy
Definition: mtsForwardDeclarations.h:49
Definition: mtsCommandWriteBase.h:40
mtsGenericObject * ArgPtr
Definition: mtsEventReceiver.h:184
virtual bool WaitWithTimeout(double timeoutInSec)
osaThreadSignal * EventSignal
Definition: mtsEventReceiver.h:102
mtsCommandVoid * Command
Definition: mtsEventReceiver.h:145
mtsCommandWriteBase * SetHandler(void(__classType::*method)(const __argumentType &), __classType *classInstantiation, mtsEventQueueingPolicy queueingPolicy=MTS_INTERFACE_EVENT_POLICY)
Definition: mtsEventReceiver.h:201
virtual void ToStream(std::ostream &outputStream) const =0
Rules of exporting.
Definition: osaThreadSignal.h:40
mtsCommandVoid * UserHandler
Definition: mtsEventReceiver.h:146
mtsCommandVoid * SetHandler(void(__classType::*method)(void), __classType *classInstantiation, mtsEventQueueingPolicy queueingPolicy=MTS_INTERFACE_EVENT_POLICY)
Definition: mtsEventReceiver.h:163
std::ostream & operator<<(std::ostream &output, const mtsEventReceiverBase &receiver)
Definition: mtsEventReceiver.h:137
Forward declarations and #define for cisstOSAbstraction.
mtsCommandWriteBase * UserHandler
Definition: mtsEventReceiver.h:183
mtsInterfaceRequired * Required
Definition: mtsEventReceiver.h:101
bool OwnEventSignal
Definition: mtsEventReceiver.h:104