2024-06-21 22:04:57 -04:00
|
|
|
#pragma once
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2024 Brett Terpstra
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLT_GP_OPERATIONS_H
|
|
|
|
#define BLT_GP_OPERATIONS_H
|
|
|
|
|
|
|
|
#include <blt/std/types.h>
|
|
|
|
#include <blt/gp/typesystem.h>
|
|
|
|
#include <blt/gp/stack.h>
|
|
|
|
#include <functional>
|
2024-06-24 04:01:29 -04:00
|
|
|
#include <type_traits>
|
2024-07-03 21:27:57 -04:00
|
|
|
#include <optional>
|
2024-06-21 22:04:57 -04:00
|
|
|
|
|
|
|
namespace blt::gp
|
|
|
|
{
|
2024-06-24 04:01:29 -04:00
|
|
|
namespace detail
|
|
|
|
{
|
2024-06-24 14:00:08 -04:00
|
|
|
template<typename T>
|
|
|
|
using remove_cv_ref = std::remove_cv_t<std::remove_reference_t<T>>;
|
|
|
|
|
|
|
|
|
|
|
|
template<typename...>
|
|
|
|
struct first_arg;
|
|
|
|
|
|
|
|
template<typename First, typename... Args>
|
|
|
|
struct first_arg<First, Args...>
|
|
|
|
{
|
|
|
|
using type = First;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct first_arg<>
|
|
|
|
{
|
|
|
|
using type = void;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<bool b, typename... types>
|
|
|
|
struct is_same;
|
|
|
|
|
|
|
|
template<typename... types>
|
|
|
|
struct is_same<true, types...> : public std::false_type
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... types>
|
|
|
|
struct is_same<false, types...> : public std::is_same<types...>
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... types>
|
|
|
|
constexpr bool is_same_v = is_same<sizeof...(types) == 0, types...>::value;
|
|
|
|
|
2024-06-24 04:01:29 -04:00
|
|
|
struct empty_t
|
|
|
|
{
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Return, typename... Args>
|
|
|
|
struct call_with
|
|
|
|
{
|
|
|
|
template<blt::u64 index>
|
|
|
|
[[nodiscard]] inline constexpr static blt::size_t getByteOffset()
|
|
|
|
{
|
|
|
|
blt::size_t offset = 0;
|
|
|
|
blt::size_t current_index = 0;
|
2024-08-04 19:13:38 -04:00
|
|
|
((offset += (current_index++ > index ? stack_allocator::aligned_size<detail::remove_cv_ref<Args>>() : 0)), ...);
|
2024-08-10 04:41:41 -04:00
|
|
|
// BLT_INFO("offset %ld for argument %ld", offset, index);
|
2024-06-24 04:01:29 -04:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2024-08-10 16:43:43 -04:00
|
|
|
template<blt::u64... indices>
|
|
|
|
void print_args(std::integer_sequence<blt::u64, indices...>)
|
|
|
|
{
|
|
|
|
BLT_INFO("Arguments:");
|
|
|
|
(BLT_INFO("%ld: %s", indices, blt::type_string<Args>().c_str()), ...);
|
|
|
|
}
|
|
|
|
|
2024-06-24 04:01:29 -04:00
|
|
|
template<typename Func, blt::u64... indices, typename... ExtraArgs>
|
|
|
|
inline static constexpr Return exec_sequence_to_indices(Func&& func, stack_allocator& allocator, std::integer_sequence<blt::u64, indices...>,
|
2024-07-03 21:27:57 -04:00
|
|
|
ExtraArgs&& ... args)
|
2024-06-24 04:01:29 -04:00
|
|
|
{
|
2024-08-03 19:51:38 -04:00
|
|
|
//blt::size_t arg_size = (stack_allocator::aligned_size<detail::remove_cv_ref<Args>>() + ...);
|
|
|
|
//BLT_TRACE(arg_size);
|
2024-06-24 04:01:29 -04:00
|
|
|
// expands Args and indices, providing each argument with its index calculating the current argument byte offset
|
2024-07-16 00:25:23 -04:00
|
|
|
return std::forward<Func>(func)(std::forward<ExtraArgs>(args)...,
|
|
|
|
allocator.from<detail::remove_cv_ref<Args>>(getByteOffset<indices>())...);
|
2024-06-24 04:01:29 -04:00
|
|
|
}
|
|
|
|
|
2024-08-11 20:21:53 -04:00
|
|
|
template<typename context = void, typename... NoCtxArgs>
|
2024-08-21 20:40:42 -04:00
|
|
|
void call_destructors_without_first(stack_allocator& read_allocator)
|
2024-08-11 20:21:53 -04:00
|
|
|
{
|
|
|
|
if constexpr (sizeof...(NoCtxArgs) > 0)
|
|
|
|
{
|
2024-08-21 20:40:42 -04:00
|
|
|
read_allocator.call_destructors<detail::remove_cv_ref<NoCtxArgs>...>();
|
2024-08-11 20:21:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 04:01:29 -04:00
|
|
|
template<typename Func, typename... ExtraArgs>
|
2024-08-21 20:40:42 -04:00
|
|
|
Return operator()(bool has_context, Func&& func, stack_allocator& read_allocator, ExtraArgs&& ... args)
|
2024-06-24 04:01:29 -04:00
|
|
|
{
|
|
|
|
constexpr auto seq = std::make_integer_sequence<blt::u64, sizeof...(Args)>();
|
2024-08-10 16:43:43 -04:00
|
|
|
#if BLT_DEBUG_LEVEL > 0
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#endif
|
2024-08-11 20:21:53 -04:00
|
|
|
Return ret = exec_sequence_to_indices(std::forward<Func>(func), read_allocator, seq, std::forward<ExtraArgs>(args)...);
|
2024-08-12 13:48:06 -04:00
|
|
|
if (has_context)
|
2024-08-21 20:40:42 -04:00
|
|
|
call_destructors_without_first<Args...>(read_allocator);
|
2024-08-11 20:21:53 -04:00
|
|
|
else
|
2024-08-21 20:40:42 -04:00
|
|
|
read_allocator.call_destructors<detail::remove_cv_ref<Args>...>();
|
2024-08-11 20:21:53 -04:00
|
|
|
read_allocator.pop_bytes((stack_allocator::aligned_size<detail::remove_cv_ref<Args>>() + ...));
|
|
|
|
return ret;
|
2024-08-10 16:43:43 -04:00
|
|
|
#if BLT_DEBUG_LEVEL > 0
|
|
|
|
} catch (const std::runtime_error& e)
|
|
|
|
{
|
|
|
|
print_args(seq);
|
|
|
|
throw std::runtime_error(e.what());
|
|
|
|
}
|
|
|
|
#endif
|
2024-06-24 04:01:29 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Return, typename, typename... Args>
|
|
|
|
struct call_without_first : public call_with<Return, Args...>
|
|
|
|
{
|
|
|
|
using call_with<Return, Args...>::call_with;
|
|
|
|
};
|
|
|
|
|
2024-07-08 22:20:51 -04:00
|
|
|
template<typename, typename>
|
2024-06-21 22:04:57 -04:00
|
|
|
class operation_t;
|
|
|
|
|
2024-08-20 13:07:33 -04:00
|
|
|
template<typename RawFunction, typename Return, typename... Args>
|
|
|
|
class operation_t<RawFunction, Return(Args...)>
|
2024-06-21 22:04:57 -04:00
|
|
|
{
|
|
|
|
public:
|
2024-08-20 13:07:33 -04:00
|
|
|
using function_t = RawFunction;
|
|
|
|
using First_Arg = typename blt::meta::arg_helper<Args...>::First;
|
2024-06-21 22:04:57 -04:00
|
|
|
|
|
|
|
constexpr operation_t(const operation_t& copy) = default;
|
|
|
|
|
|
|
|
constexpr operation_t(operation_t&& move) = default;
|
|
|
|
|
|
|
|
template<typename Functor>
|
2024-07-03 21:27:57 -04:00
|
|
|
constexpr explicit operation_t(const Functor& functor, std::optional<std::string_view> name = {}): func(functor), name(name)
|
2024-06-21 22:04:57 -04:00
|
|
|
{}
|
|
|
|
|
2024-08-21 20:40:42 -04:00
|
|
|
[[nodiscard]] constexpr inline Return operator()(stack_allocator& read_allocator) const
|
2024-06-21 22:04:57 -04:00
|
|
|
{
|
2024-06-24 04:01:29 -04:00
|
|
|
if constexpr (sizeof...(Args) == 0)
|
|
|
|
{
|
|
|
|
return func();
|
|
|
|
} else
|
|
|
|
{
|
2024-08-21 20:40:42 -04:00
|
|
|
return call_with<Return, Args...>()(false, func, read_allocator);
|
2024-06-24 04:01:29 -04:00
|
|
|
}
|
2024-06-21 22:04:57 -04:00
|
|
|
}
|
|
|
|
|
2024-08-21 20:40:42 -04:00
|
|
|
[[nodiscard]] constexpr inline Return operator()(void* context, stack_allocator& read_allocator) const
|
2024-06-21 22:04:57 -04:00
|
|
|
{
|
2024-06-24 04:01:29 -04:00
|
|
|
// should be an impossible state
|
2024-06-21 22:04:57 -04:00
|
|
|
if constexpr (sizeof...(Args) == 0)
|
|
|
|
{
|
2024-06-24 04:01:29 -04:00
|
|
|
BLT_ABORT("Cannot pass context to function without arguments!");
|
|
|
|
}
|
2024-06-24 14:00:08 -04:00
|
|
|
auto& ctx_ref = *static_cast<detail::remove_cv_ref<typename detail::first_arg<Args...>::type>*>(context);
|
2024-06-24 04:01:29 -04:00
|
|
|
if constexpr (sizeof...(Args) == 1)
|
|
|
|
{
|
|
|
|
return func(ctx_ref);
|
2024-06-21 22:04:57 -04:00
|
|
|
} else
|
|
|
|
{
|
2024-08-21 20:40:42 -04:00
|
|
|
return call_without_first<Return, Args...>()(true, func, read_allocator, ctx_ref);
|
2024-06-21 22:04:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 13:52:06 -04:00
|
|
|
template<typename Context>
|
|
|
|
[[nodiscard]] detail::operator_func_t make_callable() const
|
|
|
|
{
|
|
|
|
return [this](void* context, stack_allocator& read_allocator, stack_allocator& write_allocator) {
|
|
|
|
if constexpr (detail::is_same_v<Context, detail::remove_cv_ref<typename detail::first_arg<Args...>::type>>)
|
|
|
|
{
|
|
|
|
// first arg is context
|
2024-08-21 20:40:42 -04:00
|
|
|
write_allocator.push(this->operator()(context, read_allocator));
|
2024-08-20 13:52:06 -04:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
// first arg isn't context
|
2024-08-21 20:40:42 -04:00
|
|
|
write_allocator.push(this->operator()(read_allocator));
|
2024-08-20 13:52:06 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2024-06-21 22:04:57 -04:00
|
|
|
|
2024-07-03 21:27:57 -04:00
|
|
|
[[nodiscard]] inline constexpr std::optional<std::string_view> get_name() const
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
2024-08-03 19:51:38 -04:00
|
|
|
|
2024-08-06 21:34:03 -04:00
|
|
|
inline constexpr const auto& get_function() const
|
|
|
|
{
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2024-08-20 13:07:33 -04:00
|
|
|
inline auto set_ephemeral()
|
|
|
|
{
|
|
|
|
is_ephemeral_ = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool is_ephemeral()
|
|
|
|
{
|
|
|
|
return is_ephemeral_;
|
|
|
|
}
|
|
|
|
|
2024-08-03 19:51:38 -04:00
|
|
|
operator_id id = -1;
|
2024-06-21 22:04:57 -04:00
|
|
|
private:
|
|
|
|
function_t func;
|
2024-07-03 21:27:57 -04:00
|
|
|
std::optional<std::string_view> name;
|
2024-08-20 13:07:33 -04:00
|
|
|
bool is_ephemeral_ = false;
|
2024-06-21 22:04:57 -04:00
|
|
|
};
|
|
|
|
|
2024-08-20 13:07:33 -04:00
|
|
|
template<typename RawFunction, typename Return, typename Class, typename... Args>
|
|
|
|
class operation_t<RawFunction, Return (Class::*)(Args...) const> : public operation_t<RawFunction, Return(Args...)>
|
2024-06-21 22:04:57 -04:00
|
|
|
{
|
|
|
|
public:
|
2024-08-20 13:07:33 -04:00
|
|
|
using operation_t<RawFunction, Return(Args...)>::operation_t;
|
2024-06-21 22:04:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Lambda>
|
2024-08-20 13:07:33 -04:00
|
|
|
operation_t(Lambda) -> operation_t<Lambda, decltype(&Lambda::operator())>;
|
2024-06-21 22:04:57 -04:00
|
|
|
|
|
|
|
template<typename Return, typename... Args>
|
2024-08-20 13:07:33 -04:00
|
|
|
operation_t(Return(*)(Args...)) -> operation_t<Return(*)(Args...), Return(Args...)>;
|
2024-07-03 21:27:57 -04:00
|
|
|
|
|
|
|
template<typename Lambda>
|
2024-08-20 13:07:33 -04:00
|
|
|
operation_t(Lambda, std::optional<std::string_view>) -> operation_t<Lambda, decltype(&Lambda::operator())>;
|
2024-07-03 21:27:57 -04:00
|
|
|
|
|
|
|
template<typename Return, typename... Args>
|
2024-08-20 13:07:33 -04:00
|
|
|
operation_t(Return(*)(Args...), std::optional<std::string_view>) -> operation_t<Return(*)(Args...), Return(Args...)>;
|
2024-06-21 22:04:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_GP_OPERATIONS_H
|