MySQL 8.4.3
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
73 return logging_registry_.get();
74 }
75
76 // RandomGenerator
79 random_generator_.set_static(inst);
80 }
81
84 const std::function<void(mysql_harness::RandomGeneratorInterface *)>
85 &deleter) {
86 random_generator_.set(inst, deleter);
87 }
88
90 return random_generator_.get();
91 }
92
93 // LoaderConfig
94
97 const std::function<void(mysql_harness::LoaderConfig *)> &deleter) {
98 loader_config_.set(instance, deleter);
99 }
100
101 bool has_Config() const { return static_cast<bool>(loader_config_); }
102
104 return loader_config_.get();
105 }
106
107 // DynamicState
108
111 const std::function<void(mysql_harness::DynamicState *)> &deleter) {
112 dynamic_state_.set(instance, deleter);
113 }
114
115 bool is_DynamicState() const { return static_cast<bool>(dynamic_state_); }
116
118 return dynamic_state_.get();
119 }
120
121 private:
122 template <class T>
124 public:
125 using value_type = T;
126
127 void set_static(value_type *inst) {
128 std::unique_lock lk(mtx_);
129
130 inst_ = {inst, [](value_type *) {}};
131 }
132
133 void set(value_type *inst, std::function<void(value_type *)> deleter) {
134 std::unique_lock lk(mtx_);
135
136 inst_ = {inst, deleter};
137 }
138
139 value_type &get() const {
140 std::shared_lock lk(mtx_);
141
142 return *inst_;
143 }
144
145 void reset() {
146 std::unique_lock lk(mtx_);
147
148 inst_.reset();
149 }
150
151 explicit operator bool() const {
152 std::shared_lock lk(mtx_);
153
154 return static_cast<bool>(inst_);
155 }
156
157 private:
159
160 mutable std::shared_mutex mtx_;
161 };
162
164
166
168
170
171}; // class DIM
172
173} // namespace mysql_harness
174#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:139
UniquePtr< value_type > inst_
Definition: dim.h:158
void set(value_type *inst, std::function< void(value_type *)> deleter)
Definition: dim.h:133
void reset()
Definition: dim.h:145
std::shared_mutex mtx_
Definition: dim.h:160
void set_static(value_type *inst)
Definition: dim.h:127
T value_type
Definition: dim.h:125
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:89
void set_DynamicState(mysql_harness::DynamicState *instance, const std::function< void(mysql_harness::DynamicState *)> &deleter)
Definition: dim.h:109
RWLockedUniquePtr< mysql_harness::logging::Registry > logging_registry_
Definition: dim.h:163
mysql_harness::DynamicState & get_DynamicState() const
Definition: dim.h:117
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:165
bool has_Config() const
Definition: dim.h:101
void set_Config(mysql_harness::LoaderConfig *instance, const std::function< void(mysql_harness::LoaderConfig *)> &deleter)
Definition: dim.h:95
mysql_harness::logging::Registry & get_LoggingRegistry() const
Definition: dim.h:72
void set_static_RandomGenerator(mysql_harness::RandomGeneratorInterface *inst)
Definition: dim.h:77
RWLockedUniquePtr< mysql_harness::LoaderConfig > loader_config_
Definition: dim.h:167
RWLockedUniquePtr< mysql_harness::DynamicState > dynamic_state_
Definition: dim.h:169
mysql_harness::LoaderConfig & get_Config() const
Definition: dim.h:103
bool is_DynamicState() const
Definition: dim.h:115
DIM & operator=(const DIM &)=delete
void set_RandomGenerator(mysql_harness::RandomGeneratorInterface *inst, const std::function< void(mysql_harness::RandomGeneratorInterface *)> &deleter)
Definition: dim.h:82
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