Skip to content

Commit bc211bb

Browse files
committed
Init lib and test projects
1 parent 0a8fb07 commit bc211bb

17 files changed

Lines changed: 1092 additions & 1 deletion

CMakeLists.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
3+
set(VCPKG_ROOT "vcpkg")
4+
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
5+
6+
set(CMAKE_CXX_STANDARD 20)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
9+
set(CMAKE_CXX_EXTENSIONS OFF)
10+
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
11+
12+
set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR} ${CMAKE_MODULE_PATH})
13+
set(CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR} ${CMAKE_PREFIX_PATH})
14+
15+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/out/lib)
16+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/out/bin)
17+
18+
project(fb-cpp-main
19+
VERSION 0.0.1
20+
DESCRIPTION "C++ client library for Firebird"
21+
HOMEPAGE_URL "https://github.com/asfernandes/fb-cpp"
22+
LANGUAGES CXX
23+
)
24+
25+
enable_language(CXX)
26+
27+
28+
if(MSVC)
29+
add_compile_options(
30+
/W4
31+
/permissive-
32+
/w44062 # enumerator 'identifier' in switch of enum 'enumeration' is not handled
33+
/w44265 # 'class': class has virtual functions, but destructor is not virtual
34+
# disable
35+
/wd4100 # 'identifier': unreferenced parameter
36+
)
37+
else()
38+
add_compile_options(
39+
-Wall
40+
-Wextra
41+
-Wpedantic
42+
-Wconversion
43+
-Wno-unused-parameter
44+
)
45+
endif()
46+
47+
add_subdirectory(src/lib)
48+
add_subdirectory(src/test)

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Adriano dos Santos Fernandes
3+
Copyright (c) 2025 Adriano dos Santos Fernandes
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

gen-linux-debug.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
cmake -S . -B build/Debug -DCMAKE_BUILD_TYPE=Debug -DCMAKE_VERBOSE_MAKEFILE=ON -G Ninja

gen-linux-release.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
cmake -S . -B build/Release -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=ON -G Ninja

gen-windows.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@echo off
2+
cmake -S . -B build -G "Visual Studio 17 2022"

src/lib/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2+
3+
project(fb-cpp CXX)
4+
5+
file(GLOB_RECURSE SRC
6+
"*.h"
7+
"*.cpp"
8+
)
9+
10+
11+
add_library(${PROJECT_NAME}
12+
${SRC}
13+
)
14+
15+
target_include_directories(${PROJECT_NAME}
16+
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/
17+
)
18+
19+
target_link_libraries(${PROJECT_NAME}
20+
${CMAKE_DL_LIBS}
21+
)

src/lib/Client.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
#include "Client.h"
26+
#include "Exception.h"
27+
#include <cassert>
28+
29+
using namespace fbcpp;
30+
using namespace fbcpp::impl;
31+
32+
33+
void Client::shutdown()
34+
{
35+
assert(isValid());
36+
37+
auto dispatcher = fbRef(master->getDispatcher());
38+
const auto status = newStatus();
39+
StatusWrapper statusWrapper{*this, status.get()};
40+
41+
dispatcher->shutdown(&statusWrapper, 0, fb_shutrsn_app_stopped);
42+
43+
master = nullptr;
44+
}

src/lib/Client.h

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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

Comments
 (0)