Skip to content

Commit 9892c43

Browse files
authored
Merge pull request #296 from ecmwf/feature/factory_deprecation
Deprecate aliases for structured interpolation methods
2 parents 221dc2d + 672fd40 commit 9892c43

19 files changed

+147
-69
lines changed

src/atlas/interpolation/method/structured/Cubic2D.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ namespace method {
1818

1919
namespace {
2020

21-
MethodBuilder<Cubic2D> __builder1("structured-cubic2D");
22-
MethodBuilder<Cubic2D> __builder2("cubic2D");
23-
MethodBuilder<Cubic2D> __builder3("structured-bicubic");
24-
MethodBuilder<Cubic2D> __builder4("bicubic");
21+
using util::FactoryDeprecated;
22+
23+
MethodBuilder<Cubic2D> __builder0("structured-cubic");
24+
MethodBuilder<Cubic2D> __builder1("structured-bicubic", FactoryDeprecated("Please use structured-cubic"));
25+
MethodBuilder<Cubic2D> __builder2("structured-cubic2D", FactoryDeprecated("Please use structured-cubic"));
26+
MethodBuilder<Cubic2D> __builder3("cubic2D", FactoryDeprecated("Please use structured-cubic"));
27+
MethodBuilder<Cubic2D> __builder4("bicubic", FactoryDeprecated("Please use structured-cubic"));
2528

2629
} // namespace
2730

src/atlas/interpolation/method/structured/Cubic3D.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ namespace method {
1818

1919
namespace {
2020

21-
MethodBuilder<Cubic3D> __builder1("structured-cubic3D");
22-
MethodBuilder<Cubic3D> __builder2("cubic3D");
23-
MethodBuilder<Cubic3D> __builder3("structured-tricubic");
24-
MethodBuilder<Cubic3D> __builder4("tricubic");
21+
using util::FactoryDeprecated;
22+
23+
MethodBuilder<Cubic3D> __builder0("structured-3d-cubic");
24+
MethodBuilder<Cubic3D> __builder1("structured-tricubic", FactoryDeprecated("Please use structured-3d-cubic"));
25+
MethodBuilder<Cubic3D> __builder2("structured-cubic3D", FactoryDeprecated("Please use structured-3d-cubic"));
26+
MethodBuilder<Cubic3D> __builder3("cubic3D", FactoryDeprecated("Please use structured-3d-cubic"));
27+
MethodBuilder<Cubic3D> __builder4("tricubic", FactoryDeprecated("Please use structured-3d-cubic"));
2528

2629
} // namespace
2730

src/atlas/interpolation/method/structured/Linear2D.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ namespace method {
1818

1919
namespace {
2020

21-
MethodBuilder<Linear2D> __builder1("structured-linear2D");
22-
MethodBuilder<Linear2D> __builder2("linear2D");
23-
MethodBuilder<Linear2D> __builder3("structured-bilinear");
24-
MethodBuilder<Linear2D> __builder4("bilinear");
21+
using util::FactoryDeprecated;
22+
23+
MethodBuilder<Linear2D> __builder0("structured-linear");
24+
MethodBuilder<Linear2D> __builder1("structured-bilinear", FactoryDeprecated("Please use structured-linear"));
25+
MethodBuilder<Linear2D> __builder2("structured-linear2D", FactoryDeprecated("Please use structured-linear"));
26+
MethodBuilder<Linear2D> __builder3("linear2D", FactoryDeprecated("Please use structured-linear"));
27+
MethodBuilder<Linear2D> __builder4("bilinear", FactoryDeprecated("Please use structured-linear"));
2528

2629
} // namespace
2730

src/atlas/interpolation/method/structured/Linear3D.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ namespace method {
1818

1919
namespace {
2020

21-
MethodBuilder<Linear3D> __builder1("structured-linear3D");
22-
MethodBuilder<Linear3D> __builder2("linear3D");
23-
MethodBuilder<Linear3D> __builder3("structured-trilinear");
24-
MethodBuilder<Linear3D> __builder4("trilinear");
21+
using util::FactoryDeprecated;
22+
23+
MethodBuilder<Linear3D> __builder0("structured-3d-linear");
24+
MethodBuilder<Linear3D> __builder1("structured-trilinear", FactoryDeprecated("Please use structured-3d-linear"));
25+
MethodBuilder<Linear3D> __builder2("structured-linear3D", FactoryDeprecated("Please use structured-3d-linear"));
26+
MethodBuilder<Linear3D> __builder3("linear3D", FactoryDeprecated("Please use structured-3d-linear"));
27+
MethodBuilder<Linear3D> __builder4("trilinear", FactoryDeprecated("Please use structured-3d-linear"));
2528

2629
} // namespace
2730

src/atlas/interpolation/method/structured/QuasiCubic2D.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ namespace method {
1818

1919
namespace {
2020

21-
MethodBuilder<QuasiCubic2D> __builder1("structured-quasicubic2D");
22-
MethodBuilder<QuasiCubic2D> __builder2("quasicubic2D");
23-
MethodBuilder<QuasiCubic2D> __builder3("structured-biquasicubic");
24-
MethodBuilder<QuasiCubic2D> __builder4("biquasicubic");
21+
using util::FactoryDeprecated;
22+
23+
MethodBuilder<QuasiCubic2D> __builder0("structured-quasicubic");
24+
MethodBuilder<QuasiCubic2D> __builder1("structured-biquasicubic", FactoryDeprecated("Please use structured-quasicubic"));
25+
MethodBuilder<QuasiCubic2D> __builder2("structured-quasicubic2D", FactoryDeprecated("Please use structured-quasicubic"));
26+
MethodBuilder<QuasiCubic2D> __builder3("quasicubic2D", FactoryDeprecated("Please use structured-quasicubic"));
27+
MethodBuilder<QuasiCubic2D> __builder4("biquasicubic", FactoryDeprecated("Please use structured-quasicubic"));
2528

2629
} // namespace
2730

src/atlas/interpolation/method/structured/QuasiCubic3D.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ namespace method {
1818

1919
namespace {
2020

21-
MethodBuilder<QuasiCubic3D> __builder1("structured-quasicubic3D");
22-
MethodBuilder<QuasiCubic3D> __builder2("quasicubic3D");
23-
MethodBuilder<QuasiCubic3D> __builder3("structured-triquasicubic");
24-
MethodBuilder<QuasiCubic3D> __builder4("triquasicubic");
21+
using util::FactoryDeprecated;
22+
23+
MethodBuilder<QuasiCubic3D> __builder0("structured-3d-quasicubic");
24+
MethodBuilder<QuasiCubic3D> __builder1("structured-triquasicubic", FactoryDeprecated("Please use structured-3d-quasicubic"));
25+
MethodBuilder<QuasiCubic3D> __builder2("structured-quasicubic3D", FactoryDeprecated("Please use structured-3d-quasicubic"));
26+
MethodBuilder<QuasiCubic3D> __builder3("quasicubic3D", FactoryDeprecated("Please use structured-3d-quasicubic"));
27+
MethodBuilder<QuasiCubic3D> __builder4("triquasicubic", FactoryDeprecated("Please use structured-3d-quasicubic"));
2528

2629
} // namespace
2730

src/atlas/interpolation/method/structured/RegionalLinear2D.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ namespace interpolation {
1919
namespace method {
2020

2121
namespace {
22-
MethodBuilder<RegionalLinear2D> __builder("regional-linear-2d");
22+
23+
using util::FactoryDeprecated;
24+
25+
MethodBuilder<RegionalLinear2D> __builder0("regional-linear");
26+
MethodBuilder<RegionalLinear2D> __builder1("regional-linear-2d", FactoryDeprecated("Please use regional-linear"));
27+
2328
}
2429

2530
void RegionalLinear2D::print(std::ostream&) const { ATLAS_NOTIMPLEMENTED; }

src/atlas/util/Factory.cc

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
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>;
2223
namespace atlas {
2324
namespace 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+
2542
bool 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

89120
void 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
}

src/atlas/util/Factory.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,43 @@ class FactoryRegistry {
5252
static std::shared_ptr<FactoryRegistry> instance(const std::string& factory);
5353
};
5454

55+
class FactoryDeprecated {
56+
public:
57+
operator bool() const { return value_; }
58+
FactoryDeprecated(bool v) :
59+
value_(v) {
60+
}
61+
FactoryDeprecated() :
62+
value_(true) {
63+
}
64+
FactoryDeprecated(const char* message) :
65+
value_(true),
66+
message_{message} {
67+
}
68+
69+
const std::string& message() const { return message_; }
70+
private:
71+
bool value_;
72+
std::string message_;
73+
};
74+
5575
class FactoryBase {
5676
private:
5777
FactoryRegistry& registry_;
5878
std::string builder_;
5979
std::shared_ptr<FactoryRegistry> attached_registry_;
80+
FactoryDeprecated deprecated_;
6081

6182
protected:
62-
FactoryBase(FactoryRegistry&, const std::string& builder);
83+
FactoryBase(FactoryRegistry&, const std::string& builder, const FactoryDeprecated& deprecated = FactoryDeprecated(false));
6384
virtual ~FactoryBase();
6485
void attach_registry(const std::shared_ptr<FactoryRegistry>& registry) { attached_registry_ = registry; }
6586
friend class FactoryRegistry;
6687

6788
public:
6889
const std::string& factoryBuilder() const { return builder_; }
6990
const std::string& factoryName() const { return registry_.factory(); }
91+
const FactoryDeprecated& deprecated() const { return deprecated_; }
7092
};
7193

7294
template <typename T>
@@ -77,7 +99,7 @@ class Factory : public FactoryBase {
7799
static bool has(const std::string& builder) { return registry().has(builder); }
78100
static T* get(const std::string& builder) { return dynamic_cast<T*>(registry().get(builder)); }
79101

80-
Factory(const std::string& builder = ""): FactoryBase(registry(), builder) {
102+
Factory(const std::string& builder = "", const FactoryDeprecated& deprecated = FactoryDeprecated(false)): FactoryBase(registry(), builder, deprecated) {
81103
if (not builder.empty()) {
82104
attach_registry(FactoryRegistry::instance(T::className()));
83105
}

src/sandbox/interpolation/atlas-parallel-structured-interpolation.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ static Config processed_config(const eckit::Configuration& _config) {
7171
}
7272
std::string scheme_str = _config.getString("method", "linear");
7373
if (scheme_str == "linear") {
74-
config.set("type", "structured-linear2D");
74+
config.set("type", "structured-linear");
7575
config.set("halo", 1);
7676
// The stencil does not require any halo, but we set it to 1 for pole treatment!
7777
}
7878
if (scheme_str == "cubic") {
79-
config.set("type", "structured-cubic2D");
79+
config.set("type", "structured-cubic");
8080
config.set("halo", 2);
8181
}
8282
if (scheme_str == "quasicubic") {
83-
config.set("type", "structured-quasicubic2D");
83+
config.set("type", "structured-quasicubic");
8484
config.set("halo", 2);
8585
}
8686
config.set("name", scheme_str);

0 commit comments

Comments
 (0)