Alexandria  2.16
Please provide a description of the project.
ConfigManager.icpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 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 "ElementsKernel/Exception.h"
23 #include "Configuration/Configuration.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,
31  "T template parameter must inherit from Configuration");
32  if (m_state != State::REGISTRATION) {
33  throw Elements::Exception() << "Manager with id '" << m_id << "' is closed "
34  << "for configuration registration";
35  }
36  // If the configuration is already registered we do nothing
37  if (m_config_dictionary[{typeid(T)}] == nullptr) {
38  m_config_dictionary[{typeid(T)}].reset(new T{m_id});
39  }
40 }
41 
42 template <typename T1, typename T2>
43 void ConfigManager::registerDependency() {
44  static_assert(std::is_base_of<Configuration, T1>::value,
45  "T1 template parameter must inherit from Configuration");
46  static_assert(std::is_base_of<Configuration, T2>::value,
47  "T2 template parameter must inherit from Configuration");
48  if (m_state != State::REGISTRATION) {
49  throw Elements::Exception() << "Manager with id '" << m_id << "' is closed "
50  << "for dependency registration";
51  }
52  m_dependency_map[typeid(T1)].emplace(typeid(T2));
53 }
54 
55 template <typename T>
56 T& ConfigManager::getConfiguration() {
57  static_assert(std::is_base_of<Configuration, T>::value,
58  "T template parameter must inherit from Configuration");
59  if (m_state != State::INITIALIZED) {
60  throw Elements::Exception() << "Method getConfiguration() cannot be called on "
61  << "uninitialized manager with id '" << m_id << "'";
62  }
63  if (m_config_dictionary.find(typeid(T)) == m_config_dictionary.end()) {
64  throw Elements::Exception() << "No configuration with type " << typeid(T).name()
65  << " has been registered (manager with id '" << m_id << "')";
66  }
67  return static_cast<T&>(*m_config_dictionary.at(typeid(T)));
68 }
69 
70 } // Configuration namespace
71 } // Euclid namespace