cisst-saw
Loading...
Searching...
No Matches
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 Author(s): Ankur Kapoor, Peter Kazanzides, Anton Deguet, Min Yang Jung
6 Created on: 2004-04-30
7
8 (C) Copyright 2004-2026 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19
20#ifndef _mtsComponent_h
21#define _mtsComponent_h
22
27
40
41// Always include last
43
48
49
57
58template <class T>
60{
61 CMN_DECLARE_SERVICES_EXPORT(CMN_DYNAMIC_CREATION, CMN_LOG_ALLOW_DEFAULT);
62public:
63 std::string Name;
64 T Arg;
65
67 mtsComponentConstructorNameAndArg(const std::string &name, const T &arg) : mtsGenericObject(),
68 Name(name), Arg(arg) {}
70
71 void SerializeRaw(std::ostream & outputStream) const override {
73 cmnSerializeRaw(outputStream, Name);
74 cmnSerializeRaw(outputStream, Arg);
75 }
76
77 void DeSerializeRaw(std::istream & inputStream) override {
79 cmnDeSerializeRaw(inputStream, Name);
80 cmnDeSerializeRaw(inputStream, Arg);
81 }
82
83 void ToStream(std::ostream & outputStream) const override {
84 outputStream << "Name: " << Name
85 << ", Arg: " << Arg << std::endl;
86 }
87
89 virtual void ToStreamRaw(std::ostream & outputStream, const char delimiter = ' ',
90 bool headerOnly = false, const std::string & headerPrefix = "") const override {
91 mtsGenericObject::ToStreamRaw(outputStream, delimiter, headerOnly, headerPrefix);
92 if (headerOnly) {
93 outputStream << headerPrefix << "-name" << delimiter
94 << headerPrefix << "-arg";
95 } else {
96 outputStream << this->Name << delimiter
97 << this->Arg;
98 }
99 }
100
103 virtual bool FromStreamRaw(std::istream & inputStream, const char delimiter = ' ') override {
104 mtsGenericObject::FromStreamRaw(inputStream, delimiter);
105 if (inputStream.fail())
106 return false;
107 inputStream >> Name >> Arg;
108 if (inputStream.fail())
109 return false;
110 return (typeid(*this) == typeid(mtsComponentConstructorNameAndArg<T>));
111 }
112};
113
116
119
122
125
128
131
150{
152
154 friend class mtsManagerLocal;
155
156 protected:
157
159 std::string Name;
160
163
165 std::set<std::string> mTags;
166
169
173
178
180 void Initialize(void);
181
188 mtsInterfaceRequired * AddInterfaceRequiredExisting(const std::string & interfaceRequiredName,
189 mtsInterfaceRequired * interfaceRequired);
190
194 mtsInterfaceRequired * AddInterfaceRequiredUsingMailbox(const std::string & interfaceRequiredName,
195 mtsMailBox * mailBox,
196 mtsRequiredType required);
197
201 mtsInterfaceProvided * AddInterfaceProvidedUsingMailbox(const std::string & interfaceProvidedName,
202 mtsMailBox * mailBox);
203
204 mtsInterfaceInput * AddInterfaceInputExisting(const std::string & interfaceInputName,
205 mtsInterfaceInput * interfaceInput);
206
207 mtsInterfaceOutput * AddInterfaceOutputExisting(const std::string & interfaceOutputName,
208 mtsInterfaceOutput * interfaceOutput);
209
213
214 public:
215
217
219 mtsComponent(const std::string & deviceName);
220
222 virtual ~mtsComponent();
223
224#if CISST_HAS_JSON
247 virtual void ConfigureJSON(const Json::Value & configuration);
248#endif
249
251 const std::string & GetName(void) const;
252 void GetName(std::string & placeHolder) const;
253
255 void AddTag(const std::string & tag);
256
258 const std::set<std::string> & GetTags(void) const;
259
262 void SetName(const std::string & componentName);
263
266 virtual void Configure(const std::string & filename = "");
267
271 virtual void Create(void);
272
274 bool CreateAndWait(double timeoutInSeconds);
275
278 virtual void Start(void);
279
281 bool StartAndWait(double timeoutInSeconds);
282
284 virtual void Suspend(void);
285
288 virtual void Kill(void);
289
291 bool KillAndWait(double timeoutInSeconds);
292
296 virtual void Startup(void) {}
297
301 virtual void Cleanup(void) {}
302
304 mtsInterfaceProvided * AddInterfaceProvided(const std::string & interfaceProvidedName,
306
314 virtual mtsInterfaceProvided *
315 AddInterfaceProvidedWithoutSystemEvents(const std::string & interfaceProvidedName,
317
318 // provided for backward compatibility
319 inline CISST_DEPRECATED mtsInterfaceProvided * AddProvidedInterface(const std::string & interfaceProvidedName) {
320 return this->AddInterfaceProvided(interfaceProvidedName);
321 }
322
324 virtual mtsInterfaceOutput * AddInterfaceOutput(const std::string & interfaceOutputName);
325
330 std::vector<std::string> GetNamesOfInterfacesProvidedOrOutput(void) const;
331 std::vector<std::string> GetNamesOfInterfacesProvided(void) const;
332 std::vector<std::string> GetNamesOfInterfacesOutput(void) const;
334
336 bool InterfaceExists(const std::string & interfaceName, cmnLogLevel lod = CMN_LOG_LEVEL_INIT_VERBOSE) const;
337
339 bool InterfaceProvidedOrOutputExists(const std::string & interfaceName, cmnLogLevel lod = CMN_LOG_LEVEL_INIT_VERBOSE) const;
340
342 bool InterfaceRequiredOrInputExists(const std::string & interfaceName, cmnLogLevel lod = CMN_LOG_LEVEL_INIT_VERBOSE) const;
343
345 mtsInterfaceProvided * GetInterfaceProvided(const std::string & interfaceProvidedName) const;
346
348 mtsInterfaceOutput * GetInterfaceOutput(const std::string & interfaceOutputName) const;
349
352
354 size_t GetNumberOfInterfacesOutput(void) const;
355
357 bool RemoveInterfaceProvided(const std::string & interfaceProvidedName);
358
360 //bool RemoveInterfaceOutput(const std::string & interfaceOutputName);
361
366 mtsInterfaceRequired * AddInterfaceRequired(const std::string & interfaceRequiredName,
367 mtsRequiredType isRequired = MTS_REQUIRED);
368
369 virtual mtsInterfaceRequired *
370 AddInterfaceRequiredWithoutSystemEventHandlers(const std::string & interfaceRequiredName,
371 mtsRequiredType isRequired = MTS_REQUIRED);
372
373 // provided for backward compatibility
374 inline CISST_DEPRECATED mtsInterfaceRequired * AddRequiredInterface(const std::string & requiredInterfaceName) {
375 return this->AddInterfaceRequired(requiredInterfaceName);
376 }
377
379 virtual mtsInterfaceInput * AddInterfaceInput(const std::string & interfaceInputName);
380
383 std::vector<std::string> GetNamesOfInterfacesRequiredOrInput(void) const;
384 std::vector<std::string> GetNamesOfInterfacesRequired(void) const;
385 std::vector<std::string> GetNamesOfInterfacesInput(void) const;
387
392 const mtsInterfaceProvided * GetInterfaceProvidedFor(const std::string & interfaceRequiredName);
393
395 mtsInterfaceRequired * GetInterfaceRequired(const std::string & interfaceRequiredName);
396
398 mtsInterfaceInput * GetInterfaceInput(const std::string & interfaceInputName) const;
399
402
404 size_t GetNumberOfInterfacesInput(void) const;
405
407 bool RemoveInterfaceRequired(const std::string & interfaceRequiredName);
408
410 //bool RemoveInterfaceInput(const std::string & interfaceInputName);
411
413 mtsStateTable * GetStateTable(const std::string & stateTableName);
414
424 bool AddStateTable(mtsStateTable * existingStateTable, bool addInterfaceProvided = true);
425
430 void UseSeparateLogFileDefault(bool forwardToLogger = true);
431
437 void UseSeparateLogFileDefaultWithDate(bool forwardToLogger = true);
438
442 void UseSeparateLogFile(const std::string & filename, bool forwardToLogger = true);
443
449
450 /********************* Methods to query the task state ****************/
451
455 bool AreAllInterfacesRequiredConnected(const bool log = false);
456
458 bool IsRunning(void) const;
459 inline bool CISST_DEPRECATED Running(void) const {
460 return this->IsRunning();
461 }
462
464 bool IsStarted(void) const;
465
467 bool IsTerminated(void) const;
468
470 bool IsEndTask(void) const;
471
473 const mtsComponentState & GetState(void) const;
474 void GetState(mtsComponentState &state) const;
475
477 virtual bool WaitForState(mtsComponentState desiredState, double timeout);
478
479 protected:
480
483
490
492 std::ofstream * LogFile;
494
504
514
519
522
525 inline size_t ProcessQueuedCommands(void) {
527 }
528
533
536
543
547
554 bool AddInterfaceInternal(const bool useMangerComponentServices = false);
555
562
568
569 public:
571 void ToStream(std::ostream & outputStream) const override;
572
574 bool SetReplayMode(void);
575
576 bool SetReplayData(const std::string & stateTableName, const std::string & fileName);
577
578 bool SetReplayTime(const double time);
579
580 protected:
581
583
584};
585
586
587// overload mtsObjectName to retrieve the actual name
588inline std::string mtsObjectName(const mtsComponent * object) {
589 return "mtsComponent: " + object->GetName();
590}
591
592
594
595
596// Following derived class enables dynamic component management.
598{
600
601public:
603 mtsComponentWithManagement(const std::string &deviceName) : mtsComponent(deviceName)
605
608};
609
611
612#endif // _mtsComponent_h
Base class for high level objects.
Definition cmnGenericObject.h:53
cmnLODMultiplexerStreambuf< char > StreamBufType
Definition cmnLogger.h:246
Definition cmnNamedMap.h:54
Definition mtsComponent.h:60
void SerializeRaw(std::ostream &outputStream) const override
Definition mtsComponent.h:71
void ToStream(std::ostream &outputStream) const override
Definition mtsComponent.h:83
virtual bool FromStreamRaw(std::istream &inputStream, const char delimiter=' ') override
Definition mtsComponent.h:103
mtsComponentConstructorNameAndArg(const std::string &name, const T &arg)
Definition mtsComponent.h:67
virtual void ToStreamRaw(std::ostream &outputStream, const char delimiter=' ', bool headerOnly=false, const std::string &headerPrefix="") const override
Definition mtsComponent.h:89
void DeSerializeRaw(std::istream &inputStream) override
Definition mtsComponent.h:77
mtsComponentConstructorNameAndArg()
Definition mtsComponent.h:66
int Arg
Definition mtsComponent.h:64
~mtsComponentConstructorNameAndArg()
Definition mtsComponent.h:69
std::string Name
Definition mtsComponent.h:63
Definition mtsComponent.h:150
virtual mtsInterfaceInput * AddInterfaceInput(const std::string &interfaceInputName)
CISST_DEPRECATED mtsInterfaceRequired * AddRequiredInterface(const std::string &requiredInterfaceName)
Definition mtsComponent.h:374
void UseSeparateLogFile(const std::string &filename, bool forwardToLogger=true)
size_t ProcessMailBoxes(InterfacesProvidedMapType &interfaces)
InterfacesOutputMapType InterfacesOutput
Definition mtsComponent.h:502
bool RemoveInterfaceProvided(const std::string &interfaceProvidedName)
bool UseSeparateLogFileFlag
Definition mtsComponent.h:482
bool SetReplayMode(void)
mtsInterfaceRequired * AddInterfaceRequiredExisting(const std::string &interfaceRequiredName, mtsInterfaceRequired *interfaceRequired)
bool ReplayMode
Definition mtsComponent.h:582
bool CreateAndWait(double timeoutInSeconds)
mtsInterfaceOutput * GetInterfaceOutput(const std::string &interfaceOutputName) const
mtsInterfaceRequired * EnableDynamicComponentManagement(void)
Enable support for dynamic component management services.
cmnNamedMap< mtsInterfaceProvided > InterfacesProvidedMapType
Definition mtsComponent.h:499
size_t GetNumberOfInterfacesOutput(void) const
mtsComponent(void)
bool StartAndWait(double timeoutInSeconds)
mtsInterfaceOutput * AddInterfaceOutputExisting(const std::string &interfaceOutputName, mtsInterfaceOutput *interfaceOutput)
void ToStream(std::ostream &outputStream) const override
friend class mtsManagerLocal
Definition mtsComponent.h:154
std::vector< std::string > GetNamesOfInterfacesInput(void) const
bool KillAndWait(double timeoutInSeconds)
void GetName(std::string &placeHolder) const
const std::set< std::string > & GetTags(void) const
friend class mtsManagerComponent
Definition mtsComponent.h:153
InterfacesRequiredMapType InterfacesRequired
Definition mtsComponent.h:510
CISST_DEPRECATED mtsInterfaceProvided * AddProvidedInterface(const std::string &interfaceProvidedName)
Definition mtsComponent.h:319
void InterfaceInternalCommands_GetEndUserInterface(const mtsEndUserInterfaceArg &argin, mtsEndUserInterfaceArg &argout)
const mtsInterfaceProvided * GetInterfaceProvidedFor(const std::string &interfaceRequiredName)
std::set< std::string > mTags
Definition mtsComponent.h:165
mtsInterfaceProvided * AddInterfaceProvidedUsingMailbox(const std::string &interfaceProvidedName, mtsMailBox *mailBox)
const std::string & GetName(void) const
std::string Name
Definition mtsComponent.h:159
std::ofstream * LogFile
Definition mtsComponent.h:492
mtsInterfaceInput * GetInterfaceInput(const std::string &interfaceInputName) const
bool InterfaceProvidedOrOutputExists(const std::string &interfaceName, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_VERBOSE) const
std::vector< std::string > GetNamesOfInterfacesRequired(void) const
bool AddStateTable(mtsStateTable *existingStateTable, bool addInterfaceProvided=true)
mtsStateTable * GetStateTable(const std::string &stateTableName)
size_t ProcessQueuedCommands(void)
Definition mtsComponent.h:525
InterfacesInputMapType InterfacesInput
Definition mtsComponent.h:512
mtsInterfaceInput * AddInterfaceInputExisting(const std::string &interfaceInputName, mtsInterfaceInput *interfaceInput)
bool IsStarted(void) const
mtsComponent(const mtsComponent &other)
std::vector< std::string > GetNamesOfInterfacesProvided(void) const
mtsInterfaceProvided * InterfaceProvidedToManager
Definition mtsComponent.h:168
virtual void Suspend(void)
void InterfaceInternalCommands_RemoveObserverList(const mtsEventHandlerList &argin, mtsEventHandlerList &argout)
size_t GetNumberOfInterfacesProvided(void) const
virtual void Create(void)
bool IsTerminated(void) const
void AddTag(const std::string &tag)
cmnLogger::StreamBufType * LoDMultiplexerStreambuf
Definition mtsComponent.h:489
mtsInterfaceRequired * GetInterfaceRequired(const std::string &interfaceRequiredName)
void KillSeparateLogFile(void)
std::vector< std::string > GetNamesOfInterfacesOutput(void) const
virtual void Cleanup(void)
Definition mtsComponent.h:301
virtual mtsInterfaceRequired * AddInterfaceRequiredWithoutSystemEventHandlers(const std::string &interfaceRequiredName, mtsRequiredType isRequired=MTS_REQUIRED)
virtual void Kill(void)
virtual bool WaitForState(mtsComponentState desiredState, double timeout)
mtsComponent ThisType
Definition mtsComponent.h:216
bool SetReplayData(const std::string &stateTableName, const std::string &fileName)
size_t GetNumberOfInterfacesInput(void) const
size_t ProcessQueuedEvents(void)
mtsInterfaceRequired * AddInterfaceRequiredUsingMailbox(const std::string &interfaceRequiredName, mtsMailBox *mailBox, mtsRequiredType required)
void GetState(mtsComponentState &state) const
cmnNamedMap< mtsInterfaceRequired > InterfacesRequiredMapType
Definition mtsComponent.h:509
bool InterfaceExists(const std::string &interfaceName, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_VERBOSE) const
virtual void Startup(void)
Definition mtsComponent.h:296
cmnLogger::StreamBufType * GetLogMultiplexer(void) const override
virtual const mtsManagerComponentServices * GetManagerComponentServices(void) const
void UseSeparateLogFileDefault(bool forwardToLogger=true)
mtsFunctionWrite EventGeneratorChangeState
Definition mtsComponent.h:546
void InterfaceInternalCommands_RemoveEndUserInterface(const mtsEndUserInterfaceArg &argin, mtsEndUserInterfaceArg &argout)
StateTableMapType StateTables
Definition mtsComponent.h:518
virtual void Start(void)
virtual mtsInterfaceProvided * AddInterfaceProvidedWithoutSystemEvents(const std::string &interfaceProvidedName, mtsInterfaceQueueingPolicy queueingPolicy=MTS_COMPONENT_POLICY)
mtsInterfaceProvided * AddInterfaceProvided(const std::string &interfaceProvidedName, mtsInterfaceQueueingPolicy queueingPolicy=MTS_COMPONENT_POLICY)
bool IsEndTask(void) const
mtsComponent(const std::string &deviceName)
mtsComponentState State
Definition mtsComponent.h:162
bool RemoveInterfaceRequired(const std::string &interfaceRequiredName)
std::vector< std::string > GetNamesOfInterfacesProvidedOrOutput(void) const
bool CISST_DEPRECATED Running(void) const
Definition mtsComponent.h:459
std::vector< std::string > GetNamesOfInterfacesRequiredOrInput(void) const
virtual void Configure(const std::string &filename="")
virtual mtsInterfaceOutput * AddInterfaceOutput(const std::string &interfaceOutputName)
void InterfaceInternalCommands_AddObserverList(const mtsEventHandlerList &argin, mtsEventHandlerList &argout)
virtual ~mtsComponent()
void SetName(const std::string &componentName)
cmnNamedMap< mtsInterfaceInput > InterfacesInputMapType
Definition mtsComponent.h:511
void Initialize(void)
void UseSeparateLogFileDefaultWithDate(bool forwardToLogger=true)
mtsManagerComponentServices * ManagerComponentServices
Definition mtsComponent.h:535
InterfacesProvidedMapType InterfacesProvided
Definition mtsComponent.h:500
const mtsComponentState & GetState(void) const
bool SetReplayTime(const double time)
mtsInterfaceProvided * GetInterfaceProvided(const std::string &interfaceProvidedName) const
bool AddInterfaceInternal(const bool useMangerComponentServices=false)
Add internal interfaces.
mtsInterfaceRequired * AddInterfaceRequired(const std::string &interfaceRequiredName, mtsRequiredType isRequired=MTS_REQUIRED)
bool InterfaceRequiredOrInputExists(const std::string &interfaceName, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_VERBOSE) const
cmnNamedMap< mtsInterfaceOutput > InterfacesOutputMapType
Definition mtsComponent.h:501
size_t GetNumberOfInterfacesRequired(void) const
bool AreAllInterfacesRequiredConnected(const bool log=false)
cmnNamedMap< mtsStateTable > StateTableMapType
Definition mtsComponent.h:517
bool IsRunning(void) const
Definition mtsComponentState.h:34
Definition mtsComponent.h:598
virtual ~mtsComponentWithManagement()
Definition mtsComponent.h:607
mtsComponentWithManagement(const std::string &deviceName)
Definition mtsComponent.h:603
Definition mtsParameterTypes.h:1076
Definition mtsParameterTypesOld.h:41
Definition mtsFunctionWrite.h:37
virtual void SerializeRaw(std::ostream &outputStream) const
mtsGenericObject(void)
Definition mtsGenericObject.h:77
virtual bool FromStreamRaw(std::istream &inputStream, const char delimiter=' ')
virtual void ToStreamRaw(std::ostream &outputStream, const char delimiter=' ', bool headerOnly=false, const std::string &headerPrefix="") const
virtual void DeSerializeRaw(std::istream &inputStream)
Definition mtsInterfaceInput.h:42
Definition mtsInterfaceOutput.h:40
Definition mtsInterfaceProvided.h:98
Definition mtsInterfaceRequired.h:83
Definition mtsMailBox.h:36
Definition mtsManagerComponentServices.h:52
Definition mtsStateTable.h:67
Class registration macros.
const int CMN_DYNAMIC_CREATION_ONEARG
Definition cmnClassRegisterMacros.h:330
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition cmnClassRegisterMacros.h:199
const int CMN_DYNAMIC_CREATION
Definition cmnClassRegisterMacros.h:328
void cmnDeSerializeRaw(std::istream &inputStream, _elementType &data) CISST_THROW(std
Definition cmnDeSerializer.h:82
#define CISST_EXPORT
Definition cmnExportMacros.h:50
Defines cmnGenericObject.
short cmnLogLevel
Definition cmnLogLoD.h:55
#define CMN_LOG_ALLOW_DEFAULT
Definition cmnLogLoD.h:76
#define CMN_LOG_LEVEL_INIT_VERBOSE
Definition cmnLogLoD.h:60
Declaration of cmnNamedMap.
Portability across compilers and operating systems tools.
#define CISST_DEPRECATED
Definition cmnPortability.h:314
void cmnSerializeRaw(std::ostream &outputStream, const _elementType &data) CISST_THROW(std
Definition cmnSerializer.h:76
mtsComponentConstructorNameAndArg< int > mtsComponentConstructorNameAndInt
Definition mtsComponent.h:114
mtsComponentConstructorNameAndArg< unsigned long > mtsComponentConstructorNameAndULong
Definition mtsComponent.h:123
mtsComponentConstructorNameAndArg< long > mtsComponentConstructorNameAndLong
Definition mtsComponent.h:120
std::string mtsObjectName(const mtsComponent *object)
Definition mtsComponent.h:588
mtsComponentConstructorNameAndArg< std::string > mtsComponentConstructorNameAndString
Definition mtsComponent.h:129
mtsComponentConstructorNameAndArg< unsigned int > mtsComponentConstructorNameAndUInt
Definition mtsComponent.h:117
mtsComponentConstructorNameAndArg< double > mtsComponentConstructorNameAndDouble
Definition mtsComponent.h:126
Rules of exporting.
Forward declarations and #define for cisstMultiTask.
mtsRequiredType
Definition mtsForwardDeclarations.h:51
@ MTS_REQUIRED
Definition mtsForwardDeclarations.h:51
mtsInterfaceQueueingPolicy
Definition mtsForwardDeclarations.h:38
@ MTS_COMPONENT_POLICY
Definition mtsForwardDeclarations.h:38
Defines the command interfaces.
Defines the command interfaces.
Defines a function object to use a void command (mtsCommandVoid).
Defines a function object to use a void command (mtsCommandVoidReturn).
Defines the command interfaces.
Defines a function object to use a void command (mtsCommandWriteReturn).
Declaration of mtsInterfaceRequired.
Defines a command with one argument sent to multiple interfaces.
Defines a command with one argument sent to multiple interfaces.
STL namespace.