MySQL 8.4.2
Source Code Documentation
my_service.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MY_SERVICE_H
25#define MY_SERVICE_H
26
29
30/**
31 Wraps my_h_service struct conforming ABI into RAII C++ object with ability to
32 cast to desired service type.
33*/
34template <typename TService>
36 public:
37 /**
38 Acquires service by name.
39
40 @param name Name of service, with or without component name, to acquire.
41 @param registry Handle to registry service to use. The registry service
42 must be valid (i.e. not released) up to the moment when this instance
43 dies.
44 */
45 my_service(const char *name, SERVICE_TYPE(registry) * registry)
46 : m_registry(registry) {
47 if (registry->acquire(name, &m_service)) {
48 /* NULLed service handle means no valid service managed. */
49 m_service = {};
50 }
51 }
52 /**
53 Acquires service by name.
54
55 @param name Name of service, with or without component name, to acquire.
56 @param related_service Handle to service to acquire related to.
57 @param registry Handle to registry service to use.
58 */
59 my_service(const char *name, my_h_service related_service,
60 SERVICE_TYPE(registry) * registry)
61 : m_registry(registry) {
62 if (registry->acquire_related(name, related_service, &m_service)) {
63 /* NULLed service handle means no valid service managed. */
64 m_service = nullptr;
65 }
66 }
67 /**
68 Wraps service implementation already acquired.
69
70 @param service Service handle to manage.
71 @param registry Handle to registry service to use.
72 */
73 my_service(my_h_service service, SERVICE_TYPE(registry) * registry)
74 : m_service(service), m_registry(registry) {}
75
76 my_service(const my_service<TService> &other) = delete;
77
79 : m_service(other.m_service), m_registry(other.m_registry) {
80 other.m_service = nullptr;
81 }
82
84 if (this->is_valid()) {
86 }
87 }
88
89 operator TService *() const {
90 return reinterpret_cast<TService *>(m_service);
91 }
92
93 operator my_h_service() const { return m_service; }
94 /**
95 Returns managed service typed as desired service type to execute
96 operations specified after -> on it.
97 */
98 TService *operator->() const { return static_cast<TService *>(*this); }
99 /**
100 @retval false Object manages valid service.
101 @retval true Object does not manage any service.
102 */
103 operator bool() const { return !is_valid(); }
104 bool is_valid() const {
105 /* NULLed service handle means no valid service managed. */
106 return static_cast<const my_h_service_imp *>(this->m_service) != nullptr;
107 }
108
109 /**
110 Unties and returns the underlying service handle.
111
112 It will not be released by the destructor.
113
114 @retval the handle
115 */
116 TService *untie() {
117 TService *save = reinterpret_cast<TService *>(m_service);
118 m_service = nullptr;
119 return save;
120 }
121
122 private:
125};
126
127#endif /* MY_SERVICE_H */
Wraps my_h_service struct conforming ABI into RAII C++ object with ability to cast to desired service...
Definition: my_service.h:35
my_service(const char *name, const mysql_service_registry_t *registry)
Acquires service by name.
Definition: my_service.h:45
my_service(my_h_service service, const mysql_service_registry_t *registry)
Wraps service implementation already acquired.
Definition: my_service.h:73
my_h_service m_service
Definition: my_service.h:123
bool is_valid() const
Definition: my_service.h:104
TService * untie()
Unties and returns the underlying service handle.
Definition: my_service.h:116
~my_service()
Definition: my_service.h:83
TService * operator->() const
Returns managed service typed as desired service type to execute operations specified after -> on it.
Definition: my_service.h:98
my_service(my_service< TService > &&other)
Definition: my_service.h:78
my_service(const char *name, my_h_service related_service, const mysql_service_registry_t *registry)
Acquires service by name.
Definition: my_service.h:59
my_service(const my_service< TService > &other)=delete
const mysql_service_registry_t * m_registry
Definition: my_service.h:124
struct my_h_service_imp * my_h_service
A handle type for acquired Service.
Definition: registry.h:33
#define SERVICE_TYPE(name)
Generates the standard Service type name.
Definition: service.h:76
case opt name
Definition: sslopt-case.h:29
mysql_service_status_t(* release)(my_h_service service)
Releases the Service Implementation previously acquired.
Definition: registry.h:88