1111#include " atlas/util/Factory.h"
1212
1313#include < iostream>
14+ #include < cstdlib>
1415
1516#include " atlas/runtime/Exception.h"
1617#include " atlas/runtime/Log.h"
@@ -22,6 +23,22 @@ using lock_guard = std::lock_guard<std::mutex>;
2223namespace atlas {
2324namespace util {
2425
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+
2542bool FactoryRegistry::has (const std::string& builder) const {
2643 lock_guard lock (mutex_);
2744 return (factories_.find (builder) != factories_.end ());
@@ -40,7 +57,19 @@ FactoryBase* FactoryRegistry::get(const std::string& builder) const {
4057 throw_Exception (std::string (" No " ) + factory_ + std::string (" called " ) + builder);
4158 }
4259 else {
43- return iterator->second ;
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;
4473 }
4574}
4675
@@ -81,25 +110,29 @@ std::vector<std::string> FactoryRegistry::keys() const {
81110 std::vector<std::string> _keys;
82111 _keys.reserve (factories_.size ());
83112 for (const auto & key_value : factories_) {
84- _keys.emplace_back (key_value.first );
113+ if (not key_value.second ->deprecated_ ) {
114+ _keys.emplace_back (key_value.first );
115+ }
85116 }
86117 return _keys;
87118}
88119
89120void FactoryRegistry::list (std::ostream& out) const {
90121 lock_guard lock (mutex_);
91122 const char * sep = " " ;
92- for (const auto & map_pair : factories_) {
93- out << sep << map_pair.first ;
94- sep = " , " ;
123+ for (const auto & key_value : factories_) {
124+ if (key_value.second ->deprecated_ ) {
125+ out << sep << key_value.first ;
126+ sep = " , " ;
127+ }
95128 }
96129}
97130
98131
99132// ----------------------------------------------------------------------------------------------------------------------
100133
101- FactoryBase::FactoryBase (FactoryRegistry& registry, const std::string& builder):
102- registry_ (registry), builder_(builder) {
134+ FactoryBase::FactoryBase (FactoryRegistry& registry, const std::string& builder, const FactoryDeprecated& deprecated ):
135+ registry_ (registry), builder_(builder), deprecated_(deprecated) {
103136 if (not builder_.empty ()) {
104137 registry_.add (builder, this );
105138 }
0 commit comments