cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mtsComponent.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): Ankur Kapoor, Peter Kazanzides, Anton Deguet, Min Yang Jung
7  Created on: 2004-04-30
8 
9  (C) Copyright 2004-2011 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 
22 #ifndef _mtsComponent_h
23 #define _mtsComponent_h
24 
29 
41 
42 // Always include last
44 
59 template <class T>
61 {
62  CMN_DECLARE_SERVICES_EXPORT(CMN_DYNAMIC_CREATION, CMN_LOG_ALLOW_DEFAULT);
63 public:
64  std::string Name;
65  T Arg;
66 
68  mtsComponentConstructorNameAndArg(const std::string &name, const T &arg) : mtsGenericObject(),
69  Name(name), Arg(arg) {}
71 
72  void SerializeRaw(std::ostream & outputStream) const {
73  mtsGenericObject::SerializeRaw(outputStream);
74  cmnSerializeRaw(outputStream, Name);
75  cmnSerializeRaw(outputStream, Arg);
76  }
77 
78  void DeSerializeRaw(std::istream & inputStream) {
80  cmnDeSerializeRaw(inputStream, Name);
81  cmnDeSerializeRaw(inputStream, Arg);
82  }
83 
84  void ToStream(std::ostream & outputStream) const {
85  outputStream << "Name: " << Name
86  << ", Arg: " << Arg << std::endl;
87  }
88 
90  virtual void ToStreamRaw(std::ostream & outputStream, const char delimiter = ' ',
91  bool headerOnly = false, const std::string & headerPrefix = "") const {
92  mtsGenericObject::ToStreamRaw(outputStream, delimiter, headerOnly, headerPrefix);
93  if (headerOnly) {
94  outputStream << headerPrefix << "-name" << delimiter
95  << headerPrefix << "-arg";
96  } else {
97  outputStream << this->Name << delimiter
98  << this->Arg;
99  }
100  }
101 
104  virtual bool FromStreamRaw(std::istream & inputStream, const char delimiter = ' ') {
105  mtsGenericObject::FromStreamRaw(inputStream, delimiter);
106  if (inputStream.fail())
107  return false;
108  inputStream >> Name >> Arg;
109  if (inputStream.fail())
110  return false;
111  return (typeid(*this) == typeid(mtsComponentConstructorNameAndArg<T>));
112  }
113 };
114 
117 
119 CMN_DECLARE_SERVICES_INSTANTIATION(mtsComponentConstructorNameAndUInt)
120 
122 CMN_DECLARE_SERVICES_INSTANTIATION(mtsComponentConstructorNameAndLong)
123 
125 CMN_DECLARE_SERVICES_INSTANTIATION(mtsComponentConstructorNameAndULong)
126 
128 CMN_DECLARE_SERVICES_INSTANTIATION(mtsComponentConstructorNameAndDouble)
129 
131 CMN_DECLARE_SERVICES_INSTANTIATION(mtsComponentConstructorNameAndString)
132 
151 {
153 
154  friend class mtsManagerLocal;
155  friend class mtsComponentProxy;
156 
157  protected:
158 
160  std::string Name;
161 
164 
167 
170  mtsComponent(void);
171 
175  mtsComponent(const mtsComponent & other);
176 
178  void Initialize(void);
179 
186  mtsInterfaceRequired * AddInterfaceRequiredExisting(const std::string & interfaceRequiredName,
187  mtsInterfaceRequired * interfaceRequired);
188 
192  mtsInterfaceRequired * AddInterfaceRequiredUsingMailbox(const std::string & interfaceRequiredName,
193  mtsMailBox * mailBox,
194  mtsRequiredType required);
195 
199  mtsInterfaceProvided * AddInterfaceProvidedUsingMailbox(const std::string & interfaceProvidedName,
200  mtsMailBox * mailBox);
201 
202  mtsInterfaceInput * AddInterfaceInputExisting(const std::string & interfaceInputName,
203  mtsInterfaceInput * interfaceInput);
204 
205  mtsInterfaceOutput * AddInterfaceOutputExisting(const std::string & interfaceOutputName,
206  mtsInterfaceOutput * interfaceOutput);
207 
210  void KillSeparateLogFile(void);
211 
212  public:
213 
215 
217  mtsComponent(const std::string & deviceName);
218 
220  virtual ~mtsComponent();
221 
223  const std::string & GetName(void) const;
224  void GetName(std::string & placeHolder) const;
225 
228  void SetName(const std::string & componentName);
229 
232  virtual void Configure(const std::string & filename = "");
233 
237  virtual void Create(void);
238 
240  bool CreateAndWait(double timeoutInSeconds);
241 
244  virtual void Start(void);
245 
247  bool StartAndWait(double timeoutInSeconds);
248 
250  virtual void Suspend(void);
251 
254  virtual void Kill(void);
255 
257  bool KillAndWait(double timeoutInSeconds);
258 
262  virtual void Startup(void) {}
263 
267  virtual void Cleanup(void) {}
268 
270  mtsInterfaceProvided * AddInterfaceProvided(const std::string & interfaceProvidedName,
272 
280  virtual mtsInterfaceProvided *
281  AddInterfaceProvidedWithoutSystemEvents(const std::string & interfaceProvidedName,
283  bool isProxy = false);
284 
285  // provided for backward compatibility
286  inline CISST_DEPRECATED mtsInterfaceProvided * AddProvidedInterface(const std::string & interfaceProvidedName) {
287  return this->AddInterfaceProvided(interfaceProvidedName);
288  }
289 
291  virtual mtsInterfaceOutput * AddInterfaceOutput(const std::string & interfaceOutputName);
292 
297  std::vector<std::string> GetNamesOfInterfacesProvidedOrOutput(void) const;
298  std::vector<std::string> GetNamesOfInterfacesProvided(void) const;
299  std::vector<std::string> GetNamesOfInterfacesOutput(void) const;
301 
303  bool InterfaceExists(const std::string & interfaceName, cmnLogLevel lod = CMN_LOG_LEVEL_INIT_VERBOSE) const;
304 
306  bool InterfaceProvidedOrOutputExists(const std::string & interfaceName, cmnLogLevel lod = CMN_LOG_LEVEL_INIT_VERBOSE) const;
307 
309  bool InterfaceRequiredOrInputExists(const std::string & interfaceName, cmnLogLevel lod = CMN_LOG_LEVEL_INIT_VERBOSE) const;
310 
312  mtsInterfaceProvided * GetInterfaceProvided(const std::string & interfaceProvidedName) const;
313 
315  mtsInterfaceOutput * GetInterfaceOutput(const std::string & interfaceOutputName) const;
316 
318  size_t GetNumberOfInterfacesProvided(void) const;
319 
321  size_t GetNumberOfInterfacesOutput(void) const;
322 
324  bool RemoveInterfaceProvided(const std::string & interfaceProvidedName, const bool skipDisconnect = false);
325 
327  //bool RemoveInterfaceOutput(const std::string & interfaceOutputName);
328 
333  mtsInterfaceRequired * AddInterfaceRequired(const std::string & interfaceRequiredName,
334  mtsRequiredType isRequired = MTS_REQUIRED);
335 
336  virtual mtsInterfaceRequired *
337  AddInterfaceRequiredWithoutSystemEventHandlers(const std::string & interfaceRequiredName,
338  mtsRequiredType isRequired = MTS_REQUIRED);
339 
340  // provided for backward compatibility
341  inline CISST_DEPRECATED mtsInterfaceRequired * AddRequiredInterface(const std::string & requiredInterfaceName) {
342  return this->AddInterfaceRequired(requiredInterfaceName);
343  }
344 
346  virtual mtsInterfaceInput * AddInterfaceInput(const std::string & interfaceInputName);
347 
350  std::vector<std::string> GetNamesOfInterfacesRequiredOrInput(void) const;
351  std::vector<std::string> GetNamesOfInterfacesRequired(void) const;
352  std::vector<std::string> GetNamesOfInterfacesInput(void) const;
354 
359  const mtsInterfaceProvided * GetInterfaceProvidedFor(const std::string & interfaceRequiredName);
360 
362  mtsInterfaceRequired * GetInterfaceRequired(const std::string & interfaceRequiredName);
363 
365  mtsInterfaceInput * GetInterfaceInput(const std::string & interfaceInputName) const;
366 
368  size_t GetNumberOfInterfacesRequired(void) const;
369 
371  size_t GetNumberOfInterfacesInput(void) const;
372 
374  bool RemoveInterfaceRequired(const std::string & interfaceRequiredName, const bool skipDisconnect = false);
375 
377  //bool RemoveInterfaceInput(const std::string & interfaceInputName);
378 
382  { return this->ManagerComponentServices; }
384  { return this->ManagerComponentServices; }
385 
387  mtsStateTable * GetStateTable(const std::string & stateTableName);
388 
398  bool AddStateTable(mtsStateTable * existingStateTable, bool addInterfaceProvided = true);
399 
404  void UseSeparateLogFileDefault(bool forwardToLogger = true);
405 
411  void UseSeparateLogFileDefaultWithDate(bool forwardToLogger = true);
412 
416  void UseSeparateLogFile(const std::string & filename, bool forwardToLogger = true);
417 
422  cmnLogger::StreamBufType * GetLogMultiplexer(void) const;
423 
424  /********************* Methods to query the task state ****************/
425 
427  bool IsRunning(void) const;
428  inline bool CISST_DEPRECATED Running(void) const {
429  return this->IsRunning();
430  }
431 
433  bool IsStarted(void) const;
434 
436  bool IsTerminated(void) const;
437 
439  bool IsEndTask(void) const;
440 
442  const mtsComponentState & GetState(void) const;
443  void GetState(mtsComponentState &state) const;
444 
445  protected:
446 
448  virtual bool WaitForState(mtsComponentState desiredState, double timeout);
449 
452 
459 
461  std::ofstream * LogFile;
463 
469  InterfacesProvidedMapType InterfacesProvided;
471  InterfacesOutputMapType InterfacesOutput;
473 
479  InterfacesRequiredMapType InterfacesRequired;
481  InterfacesInputMapType InterfacesInput;
483 
487  StateTableMapType StateTables;
488 
490  size_t ProcessMailBoxes(InterfacesProvidedMapType & interfaces);
491 
494  inline size_t ProcessQueuedCommands(void) {
495  return this->ProcessMailBoxes(InterfacesProvided);
496  }
497 
501  size_t ProcessQueuedEvents(void);
502 
505 
511  mtsInterfaceRequired * EnableDynamicComponentManagement(void);
512 
516 
523  bool AddInterfaceInternal(const bool useMangerComponentServices = false);
524 
526  void InterfaceInternalCommands_GetEndUserInterface(const mtsEndUserInterfaceArg & argin, mtsEndUserInterfaceArg & argout);
527  void InterfaceInternalCommands_AddObserverList(const mtsEventHandlerList & argin, mtsEventHandlerList & argout);
528  void InterfaceInternalCommands_RemoveEndUserInterface(const mtsEndUserInterfaceArg & argin, mtsEndUserInterfaceArg & argout);
529  void InterfaceInternalCommands_RemoveObserverList(const mtsEventHandlerList & argin, mtsEventHandlerList & argout);
530  void InterfaceInternalCommands_ComponentCreate(const mtsDescriptionComponent & componentDescription, bool & result);
531  void InterfaceInternalCommands_ComponentStartOther(const mtsComponentStatusControl & arg);
532 
533  public:
535  void ToStream(std::ostream & outputStream) const;
536 
538  bool SetReplayMode(void);
539 
540  bool SetReplayData(const std::string & stateTableName, const std::string & fileName);
541 
542  bool SetReplayTime(const double time);
543 
544  protected:
545 
547 
548 };
549 
550 
551 // overload mtsObjectName to retrieve the actual name
552 inline std::string mtsObjectName(const mtsComponent * object) {
553  return "mtsComponent: " + object->GetName();
554 }
555 
556 
558 
559 
560 #endif // _mtsComponent_h
561 
Defines the command interfaces.
Defines a function object to use a void command (mtsCommandVoid)
void DeSerializeRaw(std::istream &inputStream)
Definition: mtsComponent.h:78
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
std::string mtsObjectName(const mtsComponent *object)
Definition: mtsComponent.h:552
mtsComponentState State
Definition: mtsComponent.h:163
mtsInterfaceQueueingPolicy
Definition: mtsForwardDeclarations.h:39
Definition: mtsParameterTypesOld.h:41
Defines the command interfaces.
#define CISST_DEPRECATED
Definition: cmnPortability.h:310
mtsComponentConstructorNameAndArg()
Definition: mtsComponent.h:67
~mtsComponentConstructorNameAndArg()
Definition: mtsComponent.h:70
size_t ProcessQueuedCommands(void)
Definition: mtsComponent.h:494
Definition: mtsParameterTypes.h:100
cmnNamedMap< mtsInterfaceOutput > InterfacesOutputMapType
Definition: mtsComponent.h:470
bool ReplayMode
Definition: mtsComponent.h:546
cmnLogger::StreamBufType * LoDMultiplexerStreambuf
Definition: mtsComponent.h:458
Portability across compilers and operating systems tools.
Class registration macros.
Definition: mtsParameterTypes.h:652
Definition: mtsForwardDeclarations.h:52
InterfacesRequiredMapType InterfacesRequired
Definition: mtsComponent.h:479
Definition: mtsInterfaceRequired.h:85
virtual void SerializeRaw(std::ostream &outputStream) const
virtual void DeSerializeRaw(std::istream &inputStream)
std::ofstream * LogFile
Definition: mtsComponent.h:461
Declaration of cmnNamedMap.
const mtsManagerComponentServices * GetManagerComponentServices(void) const
Definition: mtsComponent.h:381
#define CMN_LOG_LEVEL_INIT_VERBOSE
Definition: cmnLogLoD.h:60
cmnNamedMap< mtsStateTable > StateTableMapType
Definition: mtsComponent.h:486
Base class for high level objects.
Definition: cmnGenericObject.h:51
Base class for data object in cisstMultiTask.
Definition: mtsGenericObject.h:56
virtual bool FromStreamRaw(std::istream &inputStream, const char delimiter= ' ')
Defines the command interfaces.
Defines a command with one argument sent to multiple interfaces.
virtual void ToStreamRaw(std::ostream &outputStream, const char delimiter= ' ', bool headerOnly=false, const std::string &headerPrefix="") const
Definition: mtsComponent.h:90
virtual void Startup(void)
Definition: mtsComponent.h:262
Definition: mtsManagerLocal.h:89
short cmnLogLevel
Definition: cmnLogLoD.h:55
Definition: mtsComponent.h:60
void ToStream(std::ostream &outputStream) const
Definition: mtsComponent.h:84
mtsRequiredType
Definition: mtsForwardDeclarations.h:52
Definition: mtsInterfaceInput.h:41
bool UseSeparateLogFileFlag
Definition: mtsComponent.h:451
void SerializeRaw(std::ostream &outputStream) const
Definition: mtsComponent.h:72
std::string Name
Definition: mtsComponent.h:160
Definition: mtsStateTable.h:67
Defines a function object to use a void command (mtsCommandWriteReturn)
Forward declarations and #define for cisstMultiTask.
bool CISST_DEPRECATED Running(void) const
Definition: mtsComponent.h:428
void cmnDeSerializeRaw(std::istream &inputStream, _elementType &data)
Definition: cmnDeSerializer.h:82
CISST_DEPRECATED mtsInterfaceProvided * AddProvidedInterface(const std::string &interfaceProvidedName)
Definition: mtsComponent.h:286
Definition: mtsComponent.h:150
CISST_DEPRECATED mtsInterfaceRequired * AddRequiredInterface(const std::string &requiredInterfaceName)
Definition: mtsComponent.h:341
Defines cmnGenericObject.
void cmnSerializeRaw(std::ostream &outputStream, const _elementType &data)
Definition: cmnSerializer.h:78
Definition: mtsComponentState.h:34
Defines a function object to use a void command (mtsCommandVoidReturn)
std::string Name
Definition: mtsComponent.h:64
Definition: mtsParameterTypes.h:974
#define CMN_DECLARE_SERVICES(hasDynamicCreation, lod)
Definition: cmnClassRegisterMacros.h:116
cmnNamedMap< mtsInterfaceRequired > InterfacesRequiredMapType
Definition: mtsComponent.h:478
cmnNamedMap< mtsInterfaceInput > InterfacesInputMapType
Definition: mtsComponent.h:480
mtsComponentConstructorNameAndArg(const std::string &name, const T &arg)
Definition: mtsComponent.h:68
Definition: mtsInterfaceProvided.h:96
InterfacesProvidedMapType InterfacesProvided
Definition: mtsComponent.h:469
mtsInterfaceProvided * InterfaceProvidedToManager
Definition: mtsComponent.h:166
T Arg
Definition: mtsComponent.h:65
InterfacesOutputMapType InterfacesOutput
Definition: mtsComponent.h:471
mtsComponentConstructorNameAndArg< int > mtsComponentConstructorNameAndInt
Definition: mtsComponent.h:115
InterfacesInputMapType InterfacesInput
Definition: mtsComponent.h:481
Rules of exporting.
Definition: mtsForwardDeclarations.h:39
StateTableMapType StateTables
Definition: mtsComponent.h:487
virtual bool FromStreamRaw(std::istream &inputStream, const char delimiter= ' ')
Definition: mtsComponent.h:104
mtsComponent ThisType
Definition: mtsComponent.h:214
Definition: mtsInterfaceOutput.h:39
Definition: mtsFunctionWrite.h:37
mtsManagerComponentServices * ManagerComponentServices
Definition: mtsComponent.h:504
mtsFunctionWrite EventGeneratorChangeState
Definition: mtsComponent.h:515
const int CMN_NO_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:328
const int CMN_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:331
mtsManagerComponentServices * GetManagerComponentServices(void)
Definition: mtsComponent.h:383
virtual void ToStreamRaw(std::ostream &outputStream, const char delimiter= ' ', bool headerOnly=false, const std::string &headerPrefix="") const
Definition: mtsMailBox.h:36
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition: cmnClassRegisterMacros.h:202
virtual void Cleanup(void)
Definition: mtsComponent.h:267
Definition: mtsManagerComponentServices.h:31
#define CMN_LOG_ALLOW_DEFAULT
Definition: cmnLogLoD.h:76
Defines a command with one argument sent to multiple interfaces.
cmnNamedMap< mtsInterfaceProvided > InterfacesProvidedMapType
Definition: mtsComponent.h:468