Alexandria  2.19
Please provide a description of the project.
ConfigManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
5  * Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option)
6  * any later version.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
10  * details.
11  *
12  * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
13  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14  */
15 
25 #include "ElementsKernel/Logging.h"
26 
27 namespace po = boost::program_options;
28 
29 namespace Euclid {
30 namespace Configuration {
31 
33 
36  auto& manager_ptr = manager_map[id];
37  if (manager_ptr == nullptr) {
38  manager_ptr.reset(new ConfigManager{id});
39  }
40  return *manager_ptr;
41 }
42 
43 ConfigManager::ConfigManager(long id) : m_id{id} {}
44 
47  const std::pair<const std::type_index, std::set<std::type_index>>& config_pair) {
48 
49  if (config_pair.second.find(root) != config_pair.second.end()) {
50  return {root};
51  }
52  for (auto& config : config_pair.second) {
53  auto found = hasCircularDependencies(dependency_map, root, *dependency_map.find(config));
54  if (!found.empty()) {
55  std::vector<std::type_index> result{config};
56  for (auto& type : found) {
57  result.emplace_back(type);
58  }
59  return result;
60  }
61  }
62  return {};
63 }
64 
67  logger.debug() << "Cleaning dependencies of unregistered configurations...";
68  std::vector<std::type_index> unregistered_keys{};
69  for (auto& pair : dep_map) {
70  if (dict.find(pair.first) == dict.end()) {
71  unregistered_keys.emplace_back(pair.first);
72  continue;
73  }
74  std::vector<std::type_index> unregistered_values{};
75  for (auto& value : pair.second) {
76  if (dict.find(value) == dict.end()) {
77  unregistered_values.emplace_back(value);
78  }
79  }
80  for (auto& to_remove : unregistered_values) {
81  logger.info() << "Removing configuration dependency " << pair.first.name() << " -> " << to_remove.name();
82  pair.second.erase(to_remove);
83  }
84  }
85  for (auto& to_remove : unregistered_keys) {
86  for (auto& value : dep_map.at(to_remove)) {
87  logger.info() << "Removing configuration dependency " << to_remove.name() << " -> " << value.name();
88  }
89  dep_map.erase(to_remove);
90  }
91 }
92 
93 po::options_description ConfigManager::closeRegistration() {
95 
96  // Populate the dependencies map
97  for (auto& pair : m_config_dictionary) {
98  m_dependency_map[pair.first].insert(pair.second->getDependencies().begin(), pair.second->getDependencies().end());
99  }
100 
101  // Cleanup any dependencies related with non register configurations
103 
104  // Check for circular dependencies
105  for (auto& pair : m_config_dictionary) {
106  auto found = hasCircularDependencies(m_dependency_map, pair.first, *m_dependency_map.find(pair.first));
107  if (!found.empty()) {
108  logger.error() << "Found circular dependency between configurations:";
109  int count = 0;
110  logger.error() << " " << ++count << " : " << pair.first.name();
111  for (auto& type : found) {
112  logger.error() << " " << ++count << " : " << type.name();
113  }
114  throw Elements::Exception() << "Circular dependency between configurations";
115  }
116  }
117 
119  for (auto& config : m_config_dictionary) {
120  for (auto& pair : config.second->getProgramOptions()) {
121  if (all_options.find(pair.first) == all_options.end()) {
122  all_options.emplace(pair.first, po::options_description{pair.first});
123  }
124  auto& group = all_options.at(pair.first);
125  for (auto& option : pair.second) {
126  group.add(boost::shared_ptr<po::option_description>{new po::option_description{option}});
127  }
128  }
129  }
130 
131  po::options_description result{};
132  for (auto& pair : all_options) {
133  result.add(pair.second);
134  }
135 
136  return result;
137 }
138 
140  const std::map<std::type_index, std::set<std::type_index>>& dependency_map,
141  const std::map<std::string, po::variable_value>& user_values, const std::type_index& config) {
142  if (dictionary.at(config)->getCurrentState() >= Configuration::State::INITIALIZED) {
143  return;
144  }
145 
146  for (auto& dependency : dependency_map.at(config)) {
147  recursiveInitialization(dictionary, dependency_map, user_values, dependency);
148  }
149 
150  dictionary.at(config)->initialize(user_values);
151  dictionary.at(config)->getCurrentState() = Configuration::State::INITIALIZED;
152 }
153 
156  for (auto& pair : m_config_dictionary) {
157  logger.debug() << "Pre-Initializing configuration :" << pair.first.name();
158  pair.second->preInitialize(user_values);
159  pair.second->getCurrentState() = Configuration::State::PRE_INITIALIZED;
160  }
161  for (auto& pair : m_config_dictionary) {
162  logger.debug() << "Initializing configuration :" << pair.first.name();
164  }
165  for (auto& pair : m_config_dictionary) {
166  logger.debug() << "Post-Initializing configuration :" << pair.first.name();
167  pair.second->postInitialize(user_values);
168  pair.second->getCurrentState() = Configuration::State::FINAL;
169  }
170 }
171 
172 } // namespace Configuration
173 } // namespace Euclid
void error(const std::string &logMessage)
void debug(const std::string &logMessage)
static Logging getLogger(const std::string &name="")
void info(const std::string &logMessage)
Manages a set of configuration classes.
Definition: ConfigManager.h:81
std::map< std::type_index, std::unique_ptr< Configuration > > m_config_dictionary
void initialize(const std::map< std::string, boost::program_options::variable_value > &user_values)
Initialize the manager.
std::map< std::type_index, std::set< std::type_index > > m_dependency_map
boost::program_options::options_description closeRegistration()
Terminates the manager registration phase.
static ConfigManager & getInstance(long id)
Returns a reference to the ConfigManager with the given ID.
@ FINAL
The postInitialize() method has been called.
@ PRE_INITIALIZED
The preInitialize() method has been called and waits for initialization.
T emplace_back(T... args)
T emplace(T... args)
T find(T... args)
T insert(T... args)
std::vector< std::type_index > hasCircularDependencies(const std::map< std::type_index, std::set< std::type_index >> &dependency_map, const std::type_index &root, const std::pair< const std::type_index, std::set< std::type_index >> &config_pair)
static void recursiveInitialization(const std::map< std::type_index, std::unique_ptr< Configuration >> &dictionary, const std::map< std::type_index, std::set< std::type_index >> &dependency_map, const std::map< std::string, po::variable_value > &user_values, const std::type_index &config)
static Elements::Logging logger
static void cleanupNonRegisteredDependencies(std::map< std::type_index, std::set< std::type_index >> &dep_map, const std::map< std::type_index, std::unique_ptr< Configuration >> &dict)