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