ChimeraTK-ApplicationCore 04.06.00
Loading...
Searching...
No Matches
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
14namespace 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
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 */
Base class for owners of other EntityOwners (e.g.
Definition EntityOwner.h:38
std::list< Module * > getSubmoduleListRecursive() const
Obtain the list of submodules associated with this instance and any submodules.
std::list< VariableNetworkNode > getAccessorList() const
Obtain the list of accessors/variables directly associated with this instance.
Definition EntityOwner.h:77
std::list< Module * > getSubmoduleList() const
Obtain the list of submodules associated with this instance.
Definition EntityOwner.h:80
void registerAccessor(VariableNetworkNode accessor)
Called inside the constructor of Accessor: adds the accessor to the list.
EntityOwner()
Default constructor just for late initialisation.
std::string _name
The name of this instance.
virtual void unregisterModule(Module *module)
Unregister another module as a sub-module.
void dump(const std::string &prefix="", std::ostream &stream=std::cout) const
Print the full hierarchy to given stream.
std::list< Module * > _moduleList
List of modules owned by this instance.
bool hasReachedTestableMode()
Check whether this module has declared that it reached the testable mode.
std::atomic< bool > _testableModeReached
Flag used by the testable mode to identify whether a thread within the EntityOwner has reached the po...
void registerModule(Module *module, bool addTags=true)
Register another module as a sub-module.
virtual std::string getQualifiedName() const =0
Get the fully qualified name of the module instance, i.e.
std::string getQualifiedNameWithType() const
Get the fully qualified name of the module instance, followed by the C++ data type (in pointy bracket...
void addTag(const std::string &tag)
Add a tag to all Application-type nodes inside this group.
EntityOwner & operator=(EntityOwner &&other) noexcept
Move assignment operator.
std::unordered_set< std::string > _tags
List of tags to be added to all accessors and modules inside this module.
std::list< VariableNetworkNode > _accessorList
List of accessors owned by this instance.
std::list< VariableNetworkNode > getAccessorListRecursive() const
Obtain the list of accessors/variables associated with this instance and any submodules.
Base class for ApplicationModule and DeviceModule, to have a common interface for these module types.
Definition Module.h:21
Class describing a node of a variable network.
void addTag(const std::string &tag) const
Add a tag.
InvalidityTracer application module.
std::string negateTag(const std::string &tag)
negate tag using prefix '!'