From efcea3a80da7c4479d5fe168435ecc9fd06bdc72 Mon Sep 17 00:00:00 2001 From: John Turner Date: Sun, 14 Sep 2025 00:16:10 -0400 Subject: Squashed 'subprojects/boost-sqlite/' content from commit 3378e35 git-subtree-dir: subprojects/boost-sqlite git-subtree-split: 3378e353705271e569cf4ba15c467b840a39798c --- include/boost/sqlite/detail/scalar_function.hpp | 172 ++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 include/boost/sqlite/detail/scalar_function.hpp (limited to 'include/boost/sqlite/detail/scalar_function.hpp') diff --git a/include/boost/sqlite/detail/scalar_function.hpp b/include/boost/sqlite/detail/scalar_function.hpp new file mode 100644 index 0000000..ee16a23 --- /dev/null +++ b/include/boost/sqlite/detail/scalar_function.hpp @@ -0,0 +1,172 @@ +// Copyright (c) 2023 Klemens D. Morgenstern +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +#ifndef BOOST_SQLITE_DETAIL_SCALAR_FUNCTION_HPP +#define BOOST_SQLITE_DETAIL_SCALAR_FUNCTION_HPP + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + + +BOOST_SQLITE_BEGIN_NAMESPACE + +template +struct context; + +namespace detail +{ + +template +auto create_scalar_function_impl(sqlite3 * db, + cstring_ref name, + Func && func, + int flags, + std::tuple, boost::span> * , + std::false_type /* void return */, + std::false_type /* is pointer */) -> int +{ + using func_type = typename std::decay::type; + return sqlite3_create_function_v2( + db, name.c_str(), + Extent == boost::dynamic_extent ? -1 : static_cast(Extent), + SQLITE_UTF8 | flags, + new (memory_tag{}) func_type(std::forward(func)), + +[](sqlite3_context* ctx, int len, sqlite3_value** args) + { + auto cc = context(ctx); + auto aa = reinterpret_cast(args); + auto &f = *reinterpret_cast(sqlite3_user_data(ctx)); + boost::span vals{aa, static_cast(len)}; + + execute_context_function(ctx, f, cc, vals); + }, nullptr, nullptr, + +[](void * ptr) noexcept {delete_(static_cast(ptr));} + ); +} + +template +auto create_scalar_function_impl(sqlite3 * db, + cstring_ref name, + Func && func, int flags, + std::tuple, boost::span> * , + std::true_type /* void return */, + std::false_type /* is pointer */) -> int +{ + using func_type = typename std::decay::type; + return sqlite3_create_function_v2( + db, + name.c_str(), + (Extent == boost::dynamic_extent) ? -1 : static_cast(Extent), + SQLITE_UTF8 | flags, + new (memory_tag{}) func_type(std::forward(func)), + +[](sqlite3_context* ctx, int len, sqlite3_value** args) + { + auto cc = context(ctx); + auto aa = reinterpret_cast(args); + auto &f = *reinterpret_cast(sqlite3_user_data(ctx)); + boost::span vals{aa, static_cast(len)}; + + execute_context_function( + ctx, + [&]() + { + f(cc, vals); + return result(); + }); + + }, nullptr, nullptr, + +[](void * ptr){delete_(static_cast(ptr));} + ); +} + + +template +auto create_scalar_function_impl(sqlite3 * db, + cstring_ref name, + Func * func, int flags, + std::tuple, boost::span> * , + std::false_type /* void return */, + std::true_type /* is pointer */) -> int +{ + return sqlite3_create_function_v2( + db, name.c_str(), + Extent == boost::dynamic_extent ? -1 : static_cast(Extent), + SQLITE_UTF8 | flags, + reinterpret_cast(func), + +[](sqlite3_context* ctx, int len, sqlite3_value** args) + { + auto cc = context(ctx); + auto aa = reinterpret_cast(args); + auto f = reinterpret_cast(sqlite3_user_data(ctx)); + + boost::span vals{aa, static_cast(len)}; + + execute_context_function( + ctx, f, cc, vals); + }, nullptr, nullptr, nullptr); +} + +template +auto create_scalar_function_impl(sqlite3 * db, + cstring_ref name, + Func * func, int flags, + std::tuple, boost::span> * , + std::true_type /* void return */, + std::true_type /* is pointer */) -> int +{ + return sqlite3_create_function_v2( + db, + name.c_str(), + (Extent == boost::dynamic_extent) ? -1 : static_cast(Extent), + SQLITE_UTF8 | flags, + reinterpret_cast(func), + +[](sqlite3_context* ctx, int len, sqlite3_value** args) + { + auto cc = context(ctx); + auto aa = reinterpret_cast(args); + auto f = *reinterpret_cast(sqlite3_user_data(ctx)); + boost::span vals{aa, static_cast(len)}; + execute_context_function( + ctx, + [&]() + { + f(cc, vals); + return result(); + }); + + }, nullptr, nullptr, nullptr); +} + +template +auto create_scalar_function(sqlite3 * db, + cstring_ref name, + Func && func, + int flags) + -> decltype(create_scalar_function_impl( + db, name, std::forward(func), flags, + static_cast*>(nullptr), + callable_traits::has_void_return{}, + std::is_pointer::type>{} + )) +{ + return create_scalar_function_impl(db, name, std::forward(func), flags, + static_cast*>(nullptr), + callable_traits::has_void_return{}, + std::is_pointer::type>{}); +} + + +} + +BOOST_SQLITE_END_NAMESPACE + +#endif //BOOST_SQLITE_DETAIL_SCALAR_FUNCTION_HPP -- cgit v1.2.3