Debian Codegraph @

src/atlas/util/Factory.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include "atlas/util/Factory.h"
12
13 #include <iostream>
14 #include <cstdlib>
15
16 #include "atlas/runtime/Exception.h"
17 #include "atlas/runtime/Log.h"
18
19 // #define DEBUG_FACTORY_REGISTRATION
20
21 using lock_guard = std::lock_guard<std::mutex>;
22
23 namespace atlas {
24 namespace util {
25
26 static bool ATLAS_DEPRECATION_WARNINGS() {
27 const char* val = std::getenv("ATLAS_DEPRECATION_WARNINGS");
28 if (val != nullptr) {
29 return std::atoi(val);
30 }
31 return false;
32 }
33
34 static bool ATLAS_DEPRECATION_ERRORS() {
35 const char* val = std::getenv("ATLAS_DEPRECATION_ERRORS");
36 if (val != nullptr) {
37 return std::atoi(val);
38 }
39 return false;
40 }
41
42 bool FactoryRegistry::has(const std::string& builder) const {
43 lock_guard lock(mutex_);
44 return (factories_.find(builder) != factories_.end());
45 }
46
47 FactoryBase* FactoryRegistry::get(const std::string& builder) const {
48 lock_guard lock(mutex_);
49 auto iterator = factories_.find(builder);
50
51 if (iterator == factories_.end()) {
52 Log::error() << "No " << factory_ << " for [" << builder << "]" << std::endl;
53 Log::error() << "Factories are:" << std::endl;
54 for (const auto& map_pair : factories_) {
55 Log::error() << " " << map_pair.first << std::endl;
56 }
57 throw_Exception(std::string("No ") + factory_ + std::string(" called ") + builder);
58 }
59 else {
60 auto* factory = iterator->second;
61 if (factory->deprecated()) {
62 if (ATLAS_DEPRECATION_WARNINGS()) {
63 const std::string& message = factory->deprecated().message();
64 Log::warning() << "[ATLAS_DEPRECATION_WARNING] The builder " << builder << " should no longer be used. " << message << '\n';
65 Log::warning() << "[ATLAS_DEPRECATION_WARNING] This warning can be disabled with `export ATLAS_DEPRECATION_WARNINGS=0`" << std::endl;
66 }
67 if (ATLAS_DEPRECATION_ERRORS()) {
68 const std::string& message = factory->deprecated().message();
69 ATLAS_THROW_EXCEPTION("[ATLAS_DEPRECATION_ERROR] The builder " << builder << " should no longer be used. " << message);
70 }
71 }
72 return factory;
73 }
74 }
75
76 void FactoryRegistry::add(const std::string& builder, FactoryBase* factory) {
77 lock_guard lock(mutex_);
78 ATLAS_ASSERT(factories_.find(builder) == factories_.end(), "Cannot find builder in factory");
79 factories_[builder] = factory;
80 #ifdef DEBUG_FACTORY_REGISTRATION
81 std::cout << "Registered " << builder << " (" << factory << ") in " << factory_ << std::endl << std::flush;
82 #endif
83 }
84
85 void FactoryRegistry::remove(const std::string& builder) {
86 lock_guard lock(mutex_);
87 ATLAS_ASSERT(factories_.find(builder) != factories_.end());
88 #ifdef DEBUG_FACTORY_REGISTRATION
89 FactoryBase* factory = factories_[builder];
90 std::cout << "Unregistered " << builder << " (" << factory << ") from " << factory_ << std::endl << std::flush;
91 #endif
92 factories_.erase(builder);
93 }
94
95 FactoryRegistry::FactoryRegistry(const std::string& factory, FactoryRegistry::Private): factory_(factory) {
96 #ifdef DEBUG_FACTORY_REGISTRATION
97 std::cout << "Created " << factory << std::endl;
98 #endif
99 }
100
101 FactoryRegistry::~FactoryRegistry() {
102 ATLAS_ASSERT(factories_.empty(), "Registry should only be destroyed once all builders have been unregistrered");
103 #ifdef DEBUG_FACTORY_REGISTRATION
104 std::cout << "Destroyed " << factory_ << std::endl << std::flush;
105 #endif
106 }
107
108 std::vector<std::string> FactoryRegistry::keys() const {
109 lock_guard lock(mutex_);
110 std::vector<std::string> _keys;
111 _keys.reserve(factories_.size());
112 for (const auto& key_value : factories_) {
113 if (not key_value.second->deprecated_) {
114 _keys.emplace_back(key_value.first);
115 }
116 }
117 return _keys;
118 }
119
120 void FactoryRegistry::list(std::ostream& out) const {
121 lock_guard lock(mutex_);
122 const char* sep = "";
123 for (const auto& key_value : factories_) {
124 if (key_value.second->deprecated_) {
125 out << sep << key_value.first;
126 sep = ", ";
127 }
128 }
129 }
130
131
132 //----------------------------------------------------------------------------------------------------------------------
133
134 FactoryBase::FactoryBase(FactoryRegistry& registry, const std::string& builder, const FactoryDeprecated& deprecated):
135 registry_(registry), builder_(builder), deprecated_(deprecated) {
136 if (not builder_.empty()) {
137 registry_.add(builder, this);
138 }
139 }
140
141 FactoryBase::~FactoryBase() {
142 if (not builder_.empty()) {
143 registry_.remove(builder_);
144 }
145 }
146
147 //----------------------------------------------------------------------------------------------------------------------
148
149 std::shared_ptr<FactoryRegistry> FactoryRegistry::instance(const std::string& factory) {
150 static std::map<std::string,std::shared_ptr<FactoryRegistry>> registries;
151 if (registries.find(factory) == registries.end()) {
152 auto [it_pair, inserted] = registries.emplace(factory, new FactoryRegistry(factory,Private()));
153 return it_pair->second;
154 }
155 return registries.at(factory);
156 }
157
158 } // namespace util
159 } // namespace atlas