|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Adriano dos Santos Fernandes |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#ifndef FBCPP_CLIENT_H |
| 26 | +#define FBCPP_CLIENT_H |
| 27 | + |
| 28 | +#include "config.h" |
| 29 | +#include "fb-api.h" |
| 30 | +#include "SmartPtrs.h" |
| 31 | +#include <cassert> |
| 32 | +#include <concepts> |
| 33 | +#include <memory> |
| 34 | +#include <string> |
| 35 | +#include <type_traits> |
| 36 | + |
| 37 | +#if FB_CPP_USE_BOOST_DLL != 0 |
| 38 | +#include <boost/dll.hpp> |
| 39 | +#endif |
| 40 | + |
| 41 | + |
| 42 | +/// |
| 43 | +/// fb-cpp namespace. |
| 44 | +/// |
| 45 | +namespace fbcpp |
| 46 | +{ |
| 47 | + /// |
| 48 | + /// Represents a Firebird client library instance. |
| 49 | + /// The Client must exist and remain valid while there are other objects using it, such as Attachment, Transaction |
| 50 | + /// and Statement. |
| 51 | + /// |
| 52 | + class Client final |
| 53 | + { |
| 54 | + public: |
| 55 | + /// |
| 56 | + /// Constructs a Client object that uses the specified IMaster interface. |
| 57 | + /// |
| 58 | + explicit Client(fb::IMaster* master) |
| 59 | + : master{master} |
| 60 | + { |
| 61 | + assert(master); |
| 62 | + } |
| 63 | + |
| 64 | +#if FB_CPP_USE_BOOST_DLL != 0 |
| 65 | + /// |
| 66 | + /// Constructs a Client object that loads the Firebird client library (or embedded engine) |
| 67 | + /// from the specified path using Boost.DLL. |
| 68 | + /// |
| 69 | + explicit Client(const boost::dll::fs::path& fbclientLibPath, |
| 70 | + boost::dll::load_mode::type loadMode = boost::dll::load_mode::append_decorations | |
| 71 | + boost::dll::load_mode::search_system_folders) |
| 72 | + : Client{boost::dll::shared_library{fbclientLibPath, loadMode}} |
| 73 | + { |
| 74 | + } |
| 75 | + |
| 76 | + /// |
| 77 | + /// Constructs a Client object that uses the specified Boost.DLL shared_library |
| 78 | + /// representing the Firebird client library (or embedded engine). |
| 79 | + /// |
| 80 | + explicit Client(boost::dll::shared_library fbclientLib) |
| 81 | + : fbclientLib(fbclientLib) |
| 82 | + { |
| 83 | + const auto fbGetMasterInterface = |
| 84 | + fbclientLib.get<decltype(fb::fb_get_master_interface)>("fb_get_master_interface"); |
| 85 | + master = fbGetMasterInterface(); |
| 86 | + assert(master); |
| 87 | + } |
| 88 | +#endif |
| 89 | + |
| 90 | + /// |
| 91 | + /// Move constructor. |
| 92 | + /// A moved Client object becomes invalid. |
| 93 | + /// |
| 94 | + Client(Client&& o) noexcept |
| 95 | + : master{o.master}, |
| 96 | + util{o.util}, |
| 97 | + decFloat16Util{o.decFloat16Util}, |
| 98 | + decFloat34Util{o.decFloat34Util} |
| 99 | +#if FB_CPP_USE_BOOST_DLL != 0 |
| 100 | + , |
| 101 | + fbclientLib{std::move(o.fbclientLib)} |
| 102 | +#endif |
| 103 | + { |
| 104 | + o.master = nullptr; |
| 105 | + o.util = nullptr; |
| 106 | + o.decFloat16Util = nullptr; |
| 107 | + o.decFloat34Util = nullptr; |
| 108 | + } |
| 109 | + |
| 110 | + /// |
| 111 | + /// If the Client object was created using Boost.DLL, this destructor just releases the internal |
| 112 | + /// boost::dll::shared_library resource. |
| 113 | + /// It nevers automatically shuts down the Firebird client library (or embedded engine) instance. |
| 114 | + /// |
| 115 | + ~Client() noexcept = default; |
| 116 | + |
| 117 | + Client& operator=(Client&&) = delete; |
| 118 | + |
| 119 | + Client& operator=(const Client& o) = delete; |
| 120 | + Client(const Client&) = delete; |
| 121 | + |
| 122 | + public: |
| 123 | + /// |
| 124 | + /// Returns whether the Client object is valid. |
| 125 | + /// |
| 126 | + bool isValid() noexcept |
| 127 | + { |
| 128 | + return master != nullptr; |
| 129 | + } |
| 130 | + |
| 131 | + /// |
| 132 | + /// Returns the Firebird IMaster interface. |
| 133 | + /// |
| 134 | + fb::IMaster* getMaster() noexcept |
| 135 | + { |
| 136 | + return master; |
| 137 | + } |
| 138 | + |
| 139 | + /// |
| 140 | + /// Returns a Firebird IUtil interface. |
| 141 | + /// |
| 142 | + fb::IUtil* getUtil() |
| 143 | + { |
| 144 | + assert(master); |
| 145 | + |
| 146 | + if (!util) |
| 147 | + util = master->getUtilInterface(); |
| 148 | + |
| 149 | + return util; |
| 150 | + } |
| 151 | + |
| 152 | + /// |
| 153 | + /// Returns a Firebird IDecFloat16 interface. |
| 154 | + /// |
| 155 | + template <std::derived_from<fb::IStatus> StatusType> |
| 156 | + fb::IDecFloat16* getDecFloat16Util(StatusType* status) |
| 157 | + { |
| 158 | + assert(status); |
| 159 | + |
| 160 | + if (!decFloat16Util) |
| 161 | + decFloat16Util = getUtil()->getDecFloat16(status); |
| 162 | + |
| 163 | + return decFloat16Util; |
| 164 | + } |
| 165 | + |
| 166 | + /// |
| 167 | + /// Returns a Firebird IDecFloat34 interface. |
| 168 | + /// |
| 169 | + template <std::derived_from<fb::IStatus> StatusType> |
| 170 | + fb::IDecFloat34* getDecFloat34Util(StatusType* status) |
| 171 | + { |
| 172 | + assert(status); |
| 173 | + |
| 174 | + if (!decFloat34Util) |
| 175 | + decFloat34Util = getUtil()->getDecFloat34(status); |
| 176 | + |
| 177 | + return decFloat34Util; |
| 178 | + } |
| 179 | + |
| 180 | + /// |
| 181 | + /// Creates and returns a Firebird IStatus instance. |
| 182 | + /// |
| 183 | + FbUniquePtr<fb::IStatus> newStatus() |
| 184 | + { |
| 185 | + assert(master); |
| 186 | + return fbUnique(master->getStatus()); |
| 187 | + } |
| 188 | + |
| 189 | + /// |
| 190 | + /// Shuts down the Firebird client library (or embedded engine) instance. |
| 191 | + /// |
| 192 | + void shutdown(); |
| 193 | + |
| 194 | + private: |
| 195 | + fb::IMaster* master; |
| 196 | + fb::IUtil* util = nullptr; |
| 197 | + fb::IDecFloat16* decFloat16Util = nullptr; |
| 198 | + fb::IDecFloat34* decFloat34Util = nullptr; |
| 199 | +#if FB_CPP_USE_BOOST_DLL != 0 |
| 200 | + boost::dll::shared_library fbclientLib; |
| 201 | +#endif |
| 202 | + }; |
| 203 | +} // namespace fbcpp |
| 204 | + |
| 205 | + |
| 206 | +#endif // FBCPP_CLIENT_H |
0 commit comments