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