MySQL 9.1.0
Source Code Documentation
dim.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_DIMANAGER_INCLUDED
27#define MYSQL_HARNESS_DIMANAGER_INCLUDED
28
29#include "harness_export.h"
30
31#include "unique_ptr.h"
32
33#include <functional>
34#include <mutex>
35#include <shared_mutex>
36#include <string>
37
38namespace mysql_harness {
39
40// forward declarations
41class RandomGeneratorInterface;
42namespace logging {
43class Registry;
44} // namespace logging
45class LoaderConfig;
46class DynamicState;
47
48class HARNESS_EXPORT DIM { // DIM = Dependency Injection Manager
49
50 // this class is a singleton
51 protected:
52 DIM();
54
55 public:
56 DIM(const DIM &) = delete;
57 DIM &operator=(const DIM &) = delete;
58 static DIM &instance();
59
60 // Logging Registry
61 //
63 logging_registry_.set_static(instance);
64 }
65
68 const std::function<void(mysql_harness::logging::Registry *)> &deleter) {
69 logging_registry_.set(instance, deleter);
70 }
71
72 bool has_LoggingRegistry() const {
73 return static_cast<bool>(logging_registry_);
74 }
75
77 return logging_registry_.get();
78 }
79
80 // RandomGenerator
83 random_generator_.set_static(inst);
84 }
85
88 const std::function<void(mysql_harness::RandomGeneratorInterface *)>
89 &deleter) {
90 random_generator_.set(inst, deleter);
91 }
92
94 return random_generator_.get();
95 }
96
97 // LoaderConfig
98
101 const std::function<void(mysql_harness::LoaderConfig *)> &deleter) {
102 loader_config_.set(instance, deleter);
103 }
104
105 bool has_Config() const { return static_cast<bool>(loader_config_); }
106
108 return loader_config_.get();
109 }
110
111 // DynamicState
112
115 const std::function<void(mysql_harness::DynamicState *)> &deleter) {
116 dynamic_state_.set(instance, deleter);
117 }
118
119 bool is_DynamicState() const { return static_cast<bool>(dynamic_state_); }
120
122 return dynamic_state_.get();
123 }
124
125 private:
126 template <class T>
128 public:
129 using value_type = T;
130
131 void set_static(value_type *inst) {
132 std::unique_lock lk(mtx_);
133
134 inst_ = {inst, [](value_type *) {}};
135 }
136
137 void set(value_type *inst, std::function<void(value_type *)> deleter) {
138 std::unique_lock lk(mtx_);
139
140 inst_ = {inst, deleter};
141 }
142
143 value_type &get() const {
144 std::shared_lock lk(mtx_);
145
146 return *inst_;
147 }
148
149 void reset() {
150 std::unique_lock lk(mtx_);
151
152 inst_.reset();
153 }
154
155 explicit operator bool() const {
156 std::shared_lock lk(mtx_);
157
158 return static_cast<bool>(inst_);
159 }
160
161 private:
163
164 mutable std::shared_mutex mtx_;
165 };
166
168
170
172
174
175}; // class DIM
176
177} // namespace mysql_harness
178#endif // #ifndef MYSQL_HARNESS_DIMANAGER_INCLUDED
ConstSectionList get(const std::string &section) const
Get a list of sections having a name.
Definition: config_parser.cc:237
value_type & get() const
Definition: dim.h:143
UniquePtr< value_type > inst_
Definition: dim.h:162
void set(value_type *inst, std::function< void(value_type *)> deleter)
Definition: dim.h:137
void reset()
Definition: dim.h:149
std::shared_mutex mtx_
Definition: dim.h:164
void set_static(value_type *inst)
Definition: dim.h:131
T value_type
Definition: dim.h:129
Definition: dim.h:48
void set_LoggingRegistry(mysql_harness::logging::Registry *instance, const std::function< void(mysql_harness::logging::Registry *)> &deleter)
Definition: dim.h:66
mysql_harness::RandomGeneratorInterface & get_RandomGenerator() const
Definition: dim.h:93
void set_DynamicState(mysql_harness::DynamicState *instance, const std::function< void(mysql_harness::DynamicState *)> &deleter)
Definition: dim.h:113
RWLockedUniquePtr< mysql_harness::logging::Registry > logging_registry_
Definition: dim.h:167
mysql_harness::DynamicState & get_DynamicState() const
Definition: dim.h:121
void set_static_LoggingRegistry(mysql_harness::logging::Registry *instance)
Definition: dim.h:62
DIM(const DIM &)=delete
RWLockedUniquePtr< mysql_harness::RandomGeneratorInterface > random_generator_
Definition: dim.h:169
bool has_Config() const
Definition: dim.h:105
void set_Config(mysql_harness::LoaderConfig *instance, const std::function< void(mysql_harness::LoaderConfig *)> &deleter)
Definition: dim.h:99
mysql_harness::logging::Registry & get_LoggingRegistry() const
Definition: dim.h:76
void set_static_RandomGenerator(mysql_harness::RandomGeneratorInterface *inst)
Definition: dim.h:81
RWLockedUniquePtr< mysql_harness::LoaderConfig > loader_config_
Definition: dim.h:171
RWLockedUniquePtr< mysql_harness::DynamicState > dynamic_state_
Definition: dim.h:173
mysql_harness::LoaderConfig & get_Config() const
Definition: dim.h:107
bool is_DynamicState() const
Definition: dim.h:119
DIM & operator=(const DIM &)=delete
void set_RandomGenerator(mysql_harness::RandomGeneratorInterface *inst, const std::function< void(mysql_harness::RandomGeneratorInterface *)> &deleter)
Definition: dim.h:86
bool has_LoggingRegistry() const
Definition: dim.h:72
DynamicState represents a MySQLRouter dynamic state object.
Definition: dynamic_state.h:64
Configuration file handler for the loader.
Definition: loader_config.h:46
Definition: random_generator.h:36
Definition: unique_ptr.h:75
Definition: registry.h:47
Definition: common.h:42