ChimeraTK-ApplicationCore  04.01.00
EntityOwner.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 #pragma once
4 
5 #include "Flags.h"
6 #include "Utilities.h"
7 #include "VariableNetworkNode.h"
8 #include <unordered_set>
9 
10 #include <iostream>
11 #include <list>
12 #include <string>
13 
14 namespace ChimeraTK {
15 
16  /********************************************************************************************************************/
17 
18  class AccessorBase;
19  class Module;
20 
21  /********************************************************************************************************************/
22 
27  using TAGS = const std::unordered_set<std::string>;
28 
30  std::string negateTag(const std::string& tag);
31 
32  /********************************************************************************************************************/
33 
38  class EntityOwner {
39  public:
42  EntityOwner(std::string name, std::string description, std::unordered_set<std::string> tags = {});
43 
45  EntityOwner();
46 
48  virtual ~EntityOwner() = default;
49 
51  EntityOwner(EntityOwner&& other) noexcept { operator=(std::move(other)); }
52  EntityOwner(const EntityOwner& other) = delete;
53 
55  EntityOwner& operator=(EntityOwner&& other) noexcept;
56  EntityOwner& operator=(const EntityOwner& other) = delete;
57 
59  const std::string& getName() const { return _name; }
60 
63  virtual std::string getQualifiedName() const = 0;
64 
66  std::string getQualifiedNameWithType() const;
67 
69  const std::string& getDescription() const { return _description; }
70 
73  virtual std::string getFullDescription() const = 0;
74 
77  std::list<VariableNetworkNode> getAccessorList() const { return _accessorList; }
78 
80  std::list<Module*> getSubmoduleList() const { return _moduleList; }
81 
84  std::list<VariableNetworkNode> getAccessorListRecursive() const;
85 
88  std::list<Module*> getSubmoduleListRecursive() const;
89 
93 
96  void unregisterAccessor(const VariableNetworkNode& accessor) { _accessorList.remove(accessor); }
97 
103  void registerModule(Module* module, bool addTags = true);
104 
107  virtual void unregisterModule(Module* module);
108 
112  void addTag(const std::string& tag);
113 
115  void dump(const std::string& prefix = "", std::ostream& stream = std::cout) const;
116 
118 
121  virtual ModuleType getModuleType() const = 0;
122 
125  virtual VersionNumber getCurrentVersionNumber() const = 0;
126 
129  virtual void setCurrentVersionNumber(VersionNumber versionNumber) = 0;
130 
133  virtual DataValidity getDataValidity() const = 0;
134 
138  virtual void incrementDataFaultCounter() = 0;
139 
144  virtual void decrementDataFaultCounter() = 0;
145 
147  virtual std::list<EntityOwner*> getInputModulesRecursively(std::list<EntityOwner*> startList) = 0;
148 
152  virtual size_t getCircularNetworkHash() const = 0;
153 
155  bool hasReachedTestableMode();
156 
161  template<typename T>
162  std::string constant(T value);
163 
167  static constexpr std::string_view namePrefixConstant{"/@CONST@"};
168 
169  protected:
171  std::string _name;
172 
174  std::string _description;
175 
177  std::list<VariableNetworkNode> _accessorList;
178 
180  std::list<Module*> _moduleList;
181 
184  std::unordered_set<std::string> _tags;
185 
190  std::atomic<bool> _testableModeReached{false};
191  };
192 
193  /********************************************************************************************************************/
194 
195  template<typename T>
196  std::string EntityOwner::constant(T value) {
197  // Make sure every constant has its unique name to avoid differently typed inputs to be connected to the same
198  // constant. Note that the template type argument T may not match the expected type by the application, since the
199  // type of the constant is determined by the input accessor, not by the type of the value argument of this function.
200  // We hence need to use both the type name and a unique counter as part of the name. The unique counter lives inside
201  // this template function and hence counts for each type T separately!
202  static std::atomic<uint64_t> uid(0);
203  return std::string(namePrefixConstant) + "/" + DataType(typeid(T)).getAsString() + "/" + std::to_string(uid++) +
204  "/" + Utilities::escapeName(userTypeToUserType<std::string>(value), false);
205  }
206 
207  /********************************************************************************************************************/
208 
209 } /* namespace ChimeraTK */
ChimeraTK::EntityOwner::getAccessorListRecursive
std::list< VariableNetworkNode > getAccessorListRecursive() const
Obtain the list of accessors/variables associated with this instance and any submodules.
Definition: EntityOwner.cc:69
ChimeraTK::VariableNetworkNode
Class describing a node of a variable network.
Definition: VariableNetworkNode.h:37
ChimeraTK::EntityOwner::hasReachedTestableMode
bool hasReachedTestableMode()
Check whether this module has declared that it reached the testable mode.
Definition: EntityOwner.cc:141
Flags.h
ChimeraTK::EntityOwner::EntityOwner
EntityOwner(EntityOwner &&other) noexcept
Move constructor.
Definition: EntityOwner.h:51
ChimeraTK::EntityOwner::getQualifiedNameWithType
std::string getQualifiedNameWithType() const
Get the fully qualified name of the module instance, followed by the C++ data type (in pointy bracket...
Definition: EntityOwner.cc:156
ChimeraTK::EntityOwner::ModuleType::ApplicationModule
@ ApplicationModule
ChimeraTK::EntityOwner::getCircularNetworkHash
virtual size_t getCircularNetworkHash() const =0
Get the ID of the circular dependency network (0 if none).
ChimeraTK::EntityOwner::dump
void dump(const std::string &prefix="", std::ostream &stream=std::cout) const
Print the full hierarchy to given stream.
Definition: EntityOwner.cc:108
ChimeraTK::TAGS
const std::unordered_set< std::string > TAGS
Convenience type definition which can optionally be used as a shortcut for the type which defines a l...
Definition: EntityOwner.h:27
ChimeraTK::EntityOwner::namePrefixConstant
static constexpr std::string_view namePrefixConstant
Prefix for constants created by constant().
Definition: EntityOwner.h:167
Utilities.h
ChimeraTK::EntityOwner
Base class for owners of other EntityOwners (e.g.
Definition: EntityOwner.h:38
ChimeraTK::EntityOwner::ModuleType::VariableGroup
@ VariableGroup
ChimeraTK::negateTag
std::string negateTag(const std::string &tag)
negate tag using prefix '!'
Definition: EntityOwner.cc:147
ChimeraTK::EntityOwner::EntityOwner
EntityOwner()
Default constructor just for late initialisation.
Definition: EntityOwner.cc:23
pybind11::module
module_ module
Definition: PyModuleGroup.h:12
ChimeraTK::EntityOwner::constant
std::string constant(T value)
Create a variable name which will be automatically connected with a constant value.
Definition: EntityOwner.h:196
ChimeraTK::EntityOwner::_moduleList
std::list< Module * > _moduleList
List of modules owned by this instance.
Definition: EntityOwner.h:180
ChimeraTK::EntityOwner::getInputModulesRecursively
virtual std::list< EntityOwner * > getInputModulesRecursively(std::list< EntityOwner * > startList)=0
Use pointer to the module as unique identifier.
ChimeraTK::EntityOwner::_tags
std::unordered_set< std::string > _tags
List of tags to be added to all accessors and modules inside this module.
Definition: EntityOwner.h:184
ChimeraTK::EntityOwner::_description
std::string _description
The description of this instance.
Definition: EntityOwner.h:174
ChimeraTK::EntityOwner::getDescription
const std::string & getDescription() const
Get the description of the module instance.
Definition: EntityOwner.h:69
ChimeraTK::EntityOwner::getSubmoduleList
std::list< Module * > getSubmoduleList() const
Obtain the list of submodules associated with this instance.
Definition: EntityOwner.h:80
ChimeraTK::EntityOwner::registerModule
void registerModule(Module *module, bool addTags=true)
Register another module as a sub-module.
Definition: EntityOwner.cc:52
ChimeraTK::EntityOwner::ModuleType
ModuleType
Definition: EntityOwner.h:117
ChimeraTK::EntityOwner::registerAccessor
void registerAccessor(VariableNetworkNode accessor)
Called inside the constructor of Accessor: adds the accessor to the list.
Definition: EntityOwner.cc:97
ChimeraTK::EntityOwner::getCurrentVersionNumber
virtual VersionNumber getCurrentVersionNumber() const =0
Return the current version number which has been received with the last push-type read operation.
ChimeraTK::Utilities::escapeName
std::string escapeName(const std::string &name, bool allowDotsAndSlashes)
Convert all characters which are not allowed in variable or module names into underscores followed by...
Definition: Utilities.cc:44
ChimeraTK::EntityOwner::decrementDataFaultCounter
virtual void decrementDataFaultCounter()=0
Decrement the fault counter and set the data validity flag to ok if the counter has reached 0.
ChimeraTK::EntityOwner::addTag
void addTag(const std::string &tag)
Add a tag to all Application-type nodes inside this group.
Definition: EntityOwner.cc:126
ChimeraTK::EntityOwner::getDataValidity
virtual DataValidity getDataValidity() const =0
Return the data validity flag.
ChimeraTK::EntityOwner::getAccessorList
std::list< VariableNetworkNode > getAccessorList() const
Obtain the list of accessors/variables directly associated with this instance.
Definition: EntityOwner.h:77
ChimeraTK::EntityOwner::setCurrentVersionNumber
virtual void setCurrentVersionNumber(VersionNumber versionNumber)=0
Set the current version number.
ChimeraTK::EntityOwner::incrementDataFaultCounter
virtual void incrementDataFaultCounter()=0
Set the data validity flag to fault and increment the fault counter.
ChimeraTK::EntityOwner::getQualifiedName
virtual std::string getQualifiedName() const =0
Get the fully qualified name of the module instance, i.e.
ChimeraTK::EntityOwner::operator=
EntityOwner & operator=(EntityOwner &&other) noexcept
Move assignment operator.
Definition: EntityOwner.cc:28
VariableNetworkNode.h
ChimeraTK::EntityOwner::~EntityOwner
virtual ~EntityOwner()=default
Virtual destructor to make the type polymorphic.
ChimeraTK::EntityOwner::ModuleType::Device
@ Device
ChimeraTK::EntityOwner::_accessorList
std::list< VariableNetworkNode > _accessorList
List of accessors owned by this instance.
Definition: EntityOwner.h:177
ChimeraTK::EntityOwner::getModuleType
virtual ModuleType getModuleType() const =0
Return the module type of this module, or in case of a VirtualModule the module type this VirtualModu...
ChimeraTK::EntityOwner::_name
std::string _name
The name of this instance.
Definition: EntityOwner.h:171
ChimeraTK::EntityOwner::ModuleType::Invalid
@ Invalid
ChimeraTK::EntityOwner::ModuleType::ControlSystem
@ ControlSystem
ChimeraTK::EntityOwner::unregisterModule
virtual void unregisterModule(Module *module)
Unregister another module as a sub-module.
Definition: EntityOwner.cc:63
ChimeraTK
InvalidityTracer application module.
Definition: spec_dataValidityPropagation.dox:2
ChimeraTK::EntityOwner::getName
const std::string & getName() const
Get the name of the module instance.
Definition: EntityOwner.h:59
ChimeraTK::EntityOwner::getSubmoduleListRecursive
std::list< Module * > getSubmoduleListRecursive() const
Obtain the list of submodules associated with this instance and any submodules.
Definition: EntityOwner.cc:83
ChimeraTK::EntityOwner::getFullDescription
virtual std::string getFullDescription() const =0
Obtain the full description including the full description of the owner.
ChimeraTK::EntityOwner::ModuleType::ModuleGroup
@ ModuleGroup
ChimeraTK::EntityOwner::_testableModeReached
std::atomic< bool > _testableModeReached
Flag used by the testable mode to identify whether a thread within the EntityOwner has reached the po...
Definition: EntityOwner.h:190
ChimeraTK::EntityOwner::unregisterAccessor
void unregisterAccessor(const VariableNetworkNode &accessor)
Called inside the destructor of Accessor: removes the accessor from the list.
Definition: EntityOwner.h:96
ChimeraTK::Module
Base class for ApplicationModule and DeviceModule, to have a common interface for these module types.
Definition: Module.h:21