Debian Codegraph @

src/atlas/util/Factory.h

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 #pragma once
12
13 #include <map>
14 #include <memory>
15 #include <mutex>
16 #include <string>
17 #include <vector>
18
19 namespace eckit {
20 class Parametrisation;
21 }
22
23 namespace atlas {
24 namespace util {
25
26 class FactoryBase;
27
28 class FactoryRegistry {
29 private:
30 struct Private{ explicit Private() = default; };
31 public:
32 // Constructor, essentially private because nobody can create an explicit Private object.
33 // Construction must happen via instance() function below
34 FactoryRegistry(const std::string& factory, Private);
35
36 virtual ~FactoryRegistry();
37
38 private:
39 mutable std::mutex mutex_;
40 std::string factory_;
41 std::map<std::string, FactoryBase*> factories_;
42
43 public:
44 const std::string& factory() const { return factory_; }
45 std::vector<std::string> keys() const;
46 void list(std::ostream&) const;
47 bool has(const std::string& builder) const;
48 void add(const std::string& builder, FactoryBase*);
49 void remove(const std::string& builder);
50 FactoryBase* get(const std::string& builder) const;
51
52 static std::shared_ptr<FactoryRegistry> instance(const std::string& factory);
53 };
54
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
75 class FactoryBase {
76 private:
77 FactoryRegistry& registry_;
78 std::string builder_;
79 std::shared_ptr<FactoryRegistry> attached_registry_;
80 FactoryDeprecated deprecated_;
81
82 protected:
83 FactoryBase(FactoryRegistry&, const std::string& builder, const FactoryDeprecated& deprecated = FactoryDeprecated(false));
84 virtual ~FactoryBase();
85 void attach_registry(const std::shared_ptr<FactoryRegistry>& registry) { attached_registry_ = registry; }
86 friend class FactoryRegistry;
87
88 public:
89 const std::string& factoryBuilder() const { return builder_; }
90 const std::string& factoryName() const { return registry_.factory(); }
91 const FactoryDeprecated& deprecated() const { return deprecated_; }
92 };
93
94 template <typename T>
95 class Factory : public FactoryBase {
96 public:
97 static std::vector<std::string> keys() { return registry().keys(); }
98 static void list(std::ostream& out) { return registry().list(out); }
99 static bool has(const std::string& builder) { return registry().has(builder); }
100 static T* get(const std::string& builder) { return dynamic_cast<T*>(registry().get(builder)); }
101
102 Factory(const std::string& builder = "", const FactoryDeprecated& deprecated = FactoryDeprecated(false)): FactoryBase(registry(), builder, deprecated) {
103 if (not builder.empty()) {
104 attach_registry(FactoryRegistry::instance(T::className()));
105 }
106 }
107
108 protected:
109 virtual ~Factory(){};
110 static FactoryRegistry& registry() { return *FactoryRegistry::instance(T::className()).get(); }
111 };
112
113 //----------------------------------------------------------------------------------------------------------------------
114
115 } // namespace util
116 } // namespace atlas