ChimeraTK-ApplicationCore  04.01.00
EntityOwner.cc
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 #include "EntityOwner.h"
4 
5 #include "Module.h"
6 
7 #include <boost/core/demangle.hpp>
8 
9 #include <cassert>
10 #include <fstream>
11 #include <iostream>
12 #include <regex>
13 
14 namespace ChimeraTK {
15 
16  /********************************************************************************************************************/
17 
18  EntityOwner::EntityOwner(std::string name, std::string description, std::unordered_set<std::string> tags)
19  : _name(std::move(name)), _description(std::move(description)), _tags(std::move(tags)) {}
20 
21  /********************************************************************************************************************/
22 
24  : _name("**INVALID**"), _description("Invalid EntityOwner created by default constructor just as a place holder") {}
25 
26  /********************************************************************************************************************/
27 
29  _name = std::move(other._name);
30  _description = std::move(other._description);
31  _accessorList = std::move(other._accessorList);
32  _moduleList = std::move(other._moduleList);
33  _tags = std::move(other._tags);
34  for(auto* mod : _moduleList) {
35  mod->setOwner(this);
36  }
37  for(auto& node : _accessorList) {
38  node.setOwningModule(this);
39  }
40 
41  other._name = "**INVALID**";
42  other._description = "This EntityOwner was moved from.";
43  assert(other._accessorList.empty());
44  assert(other._moduleList.empty());
45  assert(other._tags.empty());
46 
47  return *this;
48  }
49 
50  /********************************************************************************************************************/
51 
52  void EntityOwner::registerModule(Module* module, bool addTags) {
53  if(addTags) {
54  for(const auto& tag : _tags) {
55  module->addTag(tag);
56  }
57  }
58  _moduleList.push_back(module);
59  }
60 
61  /********************************************************************************************************************/
62 
64  _moduleList.remove(module);
65  }
66 
67  /********************************************************************************************************************/
68 
69  std::list<VariableNetworkNode> EntityOwner::getAccessorListRecursive() const {
70  // add accessors of this instance itself
71  std::list<VariableNetworkNode> list = getAccessorList();
72 
73  // iterate through submodules
74  for(const auto* submodule : getSubmoduleList()) {
75  auto sublist = submodule->getAccessorListRecursive();
76  list.insert(list.end(), sublist.begin(), sublist.end());
77  }
78  return list;
79  }
80 
81  /********************************************************************************************************************/
82 
83  std::list<Module*> EntityOwner::getSubmoduleListRecursive() const {
84  // add modules of this instance itself
85  std::list<Module*> list = getSubmoduleList();
86 
87  // iterate through submodules
88  for(const auto* submodule : getSubmoduleList()) {
89  auto sublist = submodule->getSubmoduleListRecursive();
90  list.insert(list.end(), sublist.begin(), sublist.end());
91  }
92  return list;
93  }
94 
95  /********************************************************************************************************************/
96 
98  for(const auto& tag : _tags) {
99  accessor.addTag(tag);
100  }
101  _accessorList.push_back(std::move(accessor));
102  }
103 
104  /********************************************************************************************************************/
105 
106  /********************************************************************************************************************/
107 
108  void EntityOwner::dump(const std::string& prefix, std::ostream& stream) const {
109  if(prefix.empty()) {
110  stream << "==== Hierarchy dump of module '" << _name << "':" << std::endl;
111  }
112 
113  for(auto& node : getAccessorList()) {
114  stream << prefix << "+ ";
115  node.dump(stream);
116  }
117 
118  for(auto& submodule : getSubmoduleList()) {
119  stream << prefix << "| " << submodule->getName() << std::endl;
120  submodule->dump(prefix + "| ", stream);
121  }
122  }
123 
124  /********************************************************************************************************************/
125 
126  void EntityOwner::addTag(const std::string& tag) {
127  for(auto& node : getAccessorList()) {
128  node.addTag(tag);
129  }
130  for(auto& submodule : getSubmoduleList()) {
131  submodule->addTag(tag);
132  }
133  if(_tags.erase(negateTag(tag)) == 0) {
134  // negated tag was not found, so insert the tag
135  _tags.insert(tag);
136  }
137  }
138 
139  /********************************************************************************************************************/
140 
142  return _testableModeReached;
143  }
144 
145  /********************************************************************************************************************/
146 
147  std::string negateTag(const std::string& tag) {
148  if(!tag.empty() && tag[0] == '!') {
149  return tag.substr(1);
150  }
151  return '!' + tag;
152  }
153 
154  /********************************************************************************************************************/
155 
157  return getQualifiedName() + "<" + boost::core::demangle(typeid(*this).name()) + ">";
158  }
159 
160  /********************************************************************************************************************/
161 
162 } /* 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
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::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::EntityOwner
Base class for owners of other EntityOwners (e.g.
Definition: EntityOwner.h:38
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::_moduleList
std::list< Module * > _moduleList
List of modules owned by this instance.
Definition: EntityOwner.h:180
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::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::registerAccessor
void registerAccessor(VariableNetworkNode accessor)
Called inside the constructor of Accessor: adds the accessor to the list.
Definition: EntityOwner.cc:97
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::VariableNetworkNode::addTag
void addTag(const std::string &tag) const
Add a tag.
Definition: VariableNetworkNode.cc:295
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::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
ChimeraTK::EntityOwner::_accessorList
std::list< VariableNetworkNode > _accessorList
List of accessors owned by this instance.
Definition: EntityOwner.h:177
ChimeraTK::EntityOwner::_name
std::string _name
The name of this instance.
Definition: EntityOwner.h:171
Module.h
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::getSubmoduleListRecursive
std::list< Module * > getSubmoduleListRecursive() const
Obtain the list of submodules associated with this instance and any submodules.
Definition: EntityOwner.cc:83
EntityOwner.h
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::Module
Base class for ApplicationModule and DeviceModule, to have a common interface for these module types.
Definition: Module.h:21