Alexandria  2.19
Please provide a description of the project.
ConfigManager.icpp
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 
16 /**
17  * @file Configuration/_impl/ConfigManager.icpp
18  * @date 11/05/15
19  * @author nikoapos
20  */
21 
22 #include "Configuration/Configuration.h"
23 #include "ElementsKernel/Exception.h"
24 
25 namespace Euclid {
26 namespace Configuration {
27 
28 template <typename T>
29 void ConfigManager::registerConfiguration() {
30  static_assert(std::is_base_of<Configuration, T>::value, "T template parameter must inherit from Configuration");
31  if (m_state != State::REGISTRATION) {
32  throw Elements::Exception() << "Manager with id '" << m_id << "' is closed "
33  << "for configuration registration";
34  }
35  // If the configuration is already registered we do nothing
36  if (m_config_dictionary[{typeid(T)}] == nullptr) {
37  m_config_dictionary[{typeid(T)}].reset(new T{m_id});
38  }
39 }
40 
41 template <typename T1, typename T2>
42 void ConfigManager::registerDependency() {
43  static_assert(std::is_base_of<Configuration, T1>::value, "T1 template parameter must inherit from Configuration");
44  static_assert(std::is_base_of<Configuration, T2>::value, "T2 template parameter must inherit from Configuration");
45  if (m_state != State::REGISTRATION) {
46  throw Elements::Exception() << "Manager with id '" << m_id << "' is closed "
47  << "for dependency registration";
48  }
49  m_dependency_map[typeid(T1)].emplace(typeid(T2));
50 }
51 
52 template <typename T>
53 T& ConfigManager::getConfiguration() {
54  static_assert(std::is_base_of<Configuration, T>::value, "T template parameter must inherit from Configuration");
55  if (m_state != State::INITIALIZED) {
56  throw Elements::Exception() << "Method getConfiguration() cannot be called on "
57  << "uninitialized manager with id '" << m_id << "'";
58  }
59  if (m_config_dictionary.find(typeid(T)) == m_config_dictionary.end()) {
60  throw Elements::Exception() << "No configuration with type " << typeid(T).name() << " has been registered (manager with id '"
61  << m_id << "')";
62  }
63  return static_cast<T&>(*m_config_dictionary.at(typeid(T)));
64 }
65 
66 } // namespace Configuration
67 } // namespace Euclid