diff --git a/include/sqlpp11/compat/make_unique.h b/include/sqlpp11/compat/make_unique.h new file mode 100644 index 00000000..b030c736 --- /dev/null +++ b/include/sqlpp11/compat/make_unique.h @@ -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 + +#ifdef _MSVC_LANG +#define CXX_STD_VER _MSVC_LANG +#else +#define CXX_STD_VER __cplusplus +#endif + +namespace sqlpp +{ + namespace compat + { + template + std::unique_ptr make_unique(Args&&... args) + { +#if CXX_STD_VER >= 201402L + return std::make_unique(std::forward(args)...); +#else + return std::unique_ptr(new T(std::forward(args)...)); +#endif + } + } // namespace compat +} // namespace sqlpp diff --git a/include/sqlpp11/connection.h b/include/sqlpp11/connection.h index bb3b8f98..8e83e3a2 100644 --- a/include/sqlpp11/connection.h +++ b/include/sqlpp11/connection.h @@ -27,6 +27,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include + #include #include @@ -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} { } diff --git a/include/sqlpp11/postgresql/connection.h b/include/sqlpp11/postgresql/connection.h index e09d4ab9..4302e160 100644 --- a/include/sqlpp11/postgresql/connection.h +++ b/include/sqlpp11/postgresql/connection.h @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -71,8 +72,7 @@ namespace sqlpp std::cerr << "PostgreSQL debug: preparing: " << stmt << std::endl; } - return std::unique_ptr(new detail::prepared_statement_handle_t - (*handle, stmt, param_count)); + return sqlpp::compat::make_unique(*handle, stmt, param_count); } inline void execute_prepared_statement(std::unique_ptr& handle, std::shared_ptr& prepared) diff --git a/tests/core/CMakeLists.txt b/tests/core/CMakeLists.txt index 0dfec986..37e5b30e 100644 --- a/tests/core/CMakeLists.txt +++ b/tests/core/CMakeLists.txt @@ -25,4 +25,5 @@ add_subdirectory(usage) add_subdirectory(serialize) add_subdirectory(constraints) add_subdirectory(static_asserts) -add_subdirectory(types) \ No newline at end of file +add_subdirectory(types) +add_subdirectory(compat) diff --git a/tests/core/compat/CMakeLists.txt b/tests/core/compat/CMakeLists.txt new file mode 100644 index 00000000..3441a1ff --- /dev/null +++ b/tests/core/compat/CMakeLists.txt @@ -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) diff --git a/tests/core/compat/make_unique.cpp b/tests/core/compat/make_unique.cpp new file mode 100644 index 00000000..6c86abae --- /dev/null +++ b/tests/core/compat/make_unique.cpp @@ -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 + +int main(int, char* []) +{ + auto var_1 = sqlpp::compat::make_unique(); + static_assert(std::is_same>::value, "make_unique returns wrong type"); + + auto var_2 = sqlpp::compat::make_unique>(1, true); + static_assert(std::is_same>>::value, "make_unique> returns wrong type"); +} diff --git a/tests/include/ConnectionPoolTests.h b/tests/include/ConnectionPoolTests.h index 081592ce..f54a3408 100644 --- a/tests/include/ConnectionPoolTests.h +++ b/tests/include/ConnectionPoolTests.h @@ -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 + #include #include #include @@ -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(config, 5); + auto pool = sqlpp::compat::make_unique(config, 5); auto conn = pool->get(); pool = nullptr; }