0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Add sqlpp::compat::make_unique (#527)

* Add sqlpp::compat::make_unique which calls std::make_unique in C++14 or newer and falls back to a custom implementation in C++11 mode.

* Add tests for sqlpp::compat::make_unique
This commit is contained in:
MeanSquaredError 2023-09-16 10:21:05 +03:00 committed by GitHub
parent 8dde3bbfcc
commit 8d92e7bb81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 136 additions and 7 deletions

View File

@ -0,0 +1,51 @@
#pragma once
/*
* Copyright (c) 2023, Vesselin Atanasov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <memory>
#ifdef _MSVC_LANG
#define CXX_STD_VER _MSVC_LANG
#else
#define CXX_STD_VER __cplusplus
#endif
namespace sqlpp
{
namespace compat
{
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
#if CXX_STD_VER >= 201402L
return std::make_unique<T>(std::forward<Args>(args)...);
#else
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
#endif
}
} // namespace compat
} // namespace sqlpp

View File

@ -27,6 +27,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/compat/make_unique.h>
#include <functional>
#include <memory>
@ -51,7 +53,7 @@ namespace sqlpp
{
}
normal_connection(const _config_ptr_t& config) : ConnectionBase{std::make_unique<_handle_t>(config)}
normal_connection(const _config_ptr_t& config) : ConnectionBase{compat::make_unique<_handle_t>(config)}
{
}
@ -65,7 +67,7 @@ namespace sqlpp
// creates a connection handle and connects to database
void connectUsing(const _config_ptr_t& config) noexcept(false)
{
ConnectionBase::_handle = std::make_unique<_handle_t>(config);
ConnectionBase::_handle = compat::make_unique<_handle_t>(config);
}
private:
@ -121,7 +123,7 @@ namespace sqlpp
}
pooled_connection(const _config_ptr_t& config, _pool_core_ptr_t pool_core)
: ConnectionBase{std::make_unique<_handle_t>(config)}, _pool_core{pool_core}
: ConnectionBase{compat::make_unique<_handle_t>(config)}, _pool_core{pool_core}
{
}

View File

@ -32,6 +32,7 @@
#include <iostream>
#include <sstream>
#include <sqlpp11/compat/make_unique.h>
#include <sqlpp11/connection.h>
#include <sqlpp11/detail/float_safe_ostringstream.h>
#include <sqlpp11/postgresql/bind_result.h>
@ -71,8 +72,7 @@ namespace sqlpp
std::cerr << "PostgreSQL debug: preparing: " << stmt << std::endl;
}
return std::unique_ptr<detail::prepared_statement_handle_t>(new detail::prepared_statement_handle_t
(*handle, stmt, param_count));
return sqlpp::compat::make_unique<detail::prepared_statement_handle_t>(*handle, stmt, param_count);
}
inline void execute_prepared_statement(std::unique_ptr<connection_handle>& handle, std::shared_ptr<detail::prepared_statement_handle_t>& prepared)

View File

@ -26,3 +26,4 @@ add_subdirectory(serialize)
add_subdirectory(constraints)
add_subdirectory(static_asserts)
add_subdirectory(types)
add_subdirectory(compat)

View File

@ -0,0 +1,38 @@
# Copyright (c) 2013-2015, Roland Bock
# Copyright (c) 2023, Vesselin Atanasov
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
function(test_compile name)
set(target sqlpp11_compat_${name})
add_executable(${target} ${name}.cpp)
target_link_libraries(${target} PRIVATE sqlpp11::sqlpp11 sqlpp11_testing)
# conditionally bump to a higher C++ standard to test compatibility
if (SQLPP11_TESTS_CXX_STD)
set_property(TARGET sqlpp11_compat_${name} PROPERTY CXX_STANDARD ${SQLPP11_TESTS_CXX_STD})
set_property(TARGET sqlpp11_compat_${name} PROPERTY CXX_STANDARD_REQUIRED yes)
set_property(TARGET sqlpp11_compat_${name} PROPERTY CXX_EXTENSIONS no)
endif()
endfunction()
test_compile(make_unique)

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2023, Vesselin Atanasov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/compat/make_unique.h>
int main(int, char* [])
{
auto var_1 = sqlpp::compat::make_unique<int>();
static_assert(std::is_same<decltype(var_1), std::unique_ptr<int>>::value, "make_unique<int> returns wrong type");
auto var_2 = sqlpp::compat::make_unique<std::pair<int, bool>>(1, true);
static_assert(std::is_same<decltype(var_2), std::unique_ptr<std::pair<int, bool>>>::value, "make_unique<std::pair<int, bool>> returns wrong type");
}

View File

@ -27,6 +27,8 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/compat/make_unique.h>
#include <random>
#include <set>
#include <thread>
@ -238,7 +240,7 @@ namespace sqlpp
void test_destruction_order(typename Pool::_config_ptr_t config)
{
// Create a pool, get a connection from it and then destroy the pool before the connection
auto pool = std::make_unique<Pool>(config, 5);
auto pool = sqlpp::compat::make_unique<Pool>(config, 5);
auto conn = pool->get();
pool = nullptr;
}