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/aggregate_function.hpp | 166 +++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 include/boost/sqlite/detail/aggregate_function.hpp (limited to 'include/boost/sqlite/detail/aggregate_function.hpp') diff --git a/include/boost/sqlite/detail/aggregate_function.hpp b/include/boost/sqlite/detail/aggregate_function.hpp new file mode 100644 index 0000000..de0c447 --- /dev/null +++ b/include/boost/sqlite/detail/aggregate_function.hpp @@ -0,0 +1,166 @@ +// 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_AGGREGATE_FUNCTION_HPP +#define BOOST_SQLITE_DETAIL_AGGREGATE_FUNCTION_HPP + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + + +BOOST_SQLITE_BEGIN_NAMESPACE + +namespace detail +{ + +template +struct aggregate_function_maker +{ + void * mem; + + template + Func* operator()(Args && ... args) + { + return new (mem) Func(std::forward(args)...); + } +}; + +template +int create_aggregate_function(sqlite3 * db, cstring_ref name, Args && args, int flags, + std::true_type /* void return */) +{ + using args_type = callable_traits::args_t; + using span_type = typename std::tuple_element<1U, args_type>::type; + using func_type = typename std::decay::type; + using func_args_type = typename std::decay::type; + + return sqlite3_create_function_v2( + db, name.c_str(), + span_type::extent == boost::dynamic_extent ? -1 : static_cast(span_type::extent), + SQLITE_UTF8 | flags, + new (memory_tag{}) func_args_type(std::forward(args)), + nullptr, + +[](sqlite3_context* ctx, int len, sqlite3_value** args) + { + auto aa = reinterpret_cast(args); + auto fa = reinterpret_cast(sqlite3_user_data(ctx)); + auto c = static_cast(sqlite3_aggregate_context(ctx, 0)); + + execute_context_function( + ctx, + [&]() -> result + { + if (c == nullptr) + { + auto p = sqlite3_aggregate_context(ctx, sizeof(func_type)); + if (!p) + return error(SQLITE_NOMEM); + c = mp11::tuple_apply(aggregate_function_maker{p}, *fa); + } + c->step(span_type{aa, static_cast(len)}); + return {}; + }); + }, + [](sqlite3_context* ctx) + { + auto fa = reinterpret_cast(sqlite3_user_data(ctx)); + auto c = static_cast(sqlite3_aggregate_context(ctx, 0)); + + execute_context_function( + ctx, + [&]() -> resultfinal())> + { + if (c == nullptr) + { + auto p = sqlite3_aggregate_context(ctx, sizeof(func_type)); + if (!p) + return error(SQLITE_NOMEM); + c = mp11::tuple_apply(aggregate_function_maker{p}, *fa); + } + struct reaper {void operator()(func_type * c) { c->~func_type();}}; + std::unique_ptr cl{c}; + return c->final(); + }); + }, + [](void * ptr) noexcept { delete_(static_cast(ptr));} + ); +} + + +template +int create_aggregate_function(sqlite3 * db, cstring_ref name, Args && args, int flags, + std::false_type /* void return */) +{ + using args_type = callable_traits::args_t; + using span_type = typename std::tuple_element<1U, args_type>::type; + using func_type = typename std::decay::type; + using func_args_type = typename std::decay::type; + + return sqlite3_create_function_v2( + db, name.c_str(), + span_type::extent == boost::dynamic_extent ? -1 : static_cast(span_type::extent), + SQLITE_UTF8 | flags, + new (memory_tag{}) func_args_type(std::forward(args)), + nullptr, + +[](sqlite3_context* ctx, int len, sqlite3_value** args) + { + auto aa = reinterpret_cast(args); + auto fa = reinterpret_cast(sqlite3_user_data(ctx)); + auto c = static_cast(sqlite3_aggregate_context(ctx, 0)); + + execute_context_function( + ctx, + [&]() -> result + { + if (c == nullptr) + { + auto p = sqlite3_aggregate_context(ctx, sizeof(func_type)); + if (!p) + return {system::in_place_error, error(SQLITE_NOMEM)}; + c = mp11::tuple_apply(aggregate_function_maker{p}, *fa); + } + return c->step(span_type{aa, static_cast(len)}); + }); + }, + [](sqlite3_context* ctx) + { + auto fa = reinterpret_cast(sqlite3_user_data(ctx)); + auto c = static_cast(sqlite3_aggregate_context(ctx, 0)); + + execute_context_function( + ctx, + [&]() -> resultfinal())> + { + if (c == nullptr) + { + auto p = sqlite3_aggregate_context(ctx, sizeof(func_type)); + if (!p) + return {system::in_place_error, error(SQLITE_NOMEM)}; + c = mp11::tuple_apply(aggregate_function_maker{p}, *fa); + } + struct reaper {void operator()(func_type * c) { c->~func_type();}}; + std::unique_ptr cl{c}; + return c->final(); + }); + }, + [](void * ptr) noexcept { delete_(static_cast(ptr));} + ); +} + +} + +BOOST_SQLITE_END_NAMESPACE + + +#endif //BOOST_SQLITE_DETAIL_AGGREGATE_FUNCTION_HPP -- cgit v1.2.3