#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 .
*/
#ifndef BLT_GP_OPERATIONS_H
#define BLT_GP_OPERATIONS_H
#include
#include
#include
#include
#include
#include
namespace blt::gp
{
namespace detail
{
template
using remove_cv_ref = std::remove_cv_t>;
template
struct first_arg;
template
struct first_arg
{
using type = First;
};
template<>
struct first_arg<>
{
using type = void;
};
template
struct is_same;
template
struct is_same : public std::false_type
{
};
template
struct is_same : public std::is_same
{
};
template
constexpr bool is_same_v = is_same::value;
struct empty_t
{
};
}
template
struct call_with
{
template
[[nodiscard]] inline constexpr static blt::size_t getByteOffset()
{
blt::size_t offset = 0;
blt::size_t current_index = 0;
((offset += (current_index++ > index ? stack_allocator::aligned_size>() : 0)), ...);
// BLT_INFO("offset %ld for argument %ld", offset, index);
return offset;
}
template
void print_args(std::integer_sequence)
{
BLT_INFO("Arguments:");
(BLT_INFO("%ld: %s", indices, blt::type_string().c_str()), ...);
}
template
inline static constexpr Return exec_sequence_to_indices(Func&& func, stack_allocator& allocator, std::integer_sequence,
ExtraArgs&& ... args)
{
//blt::size_t arg_size = (stack_allocator::aligned_size>() + ...);
//BLT_TRACE(arg_size);
// expands Args and indices, providing each argument with its index calculating the current argument byte offset
return std::forward(func)(std::forward(args)...,
allocator.from>(getByteOffset())...);
}
template
void call_destructors_without_first(stack_allocator& read_allocator)
{
if constexpr (sizeof...(NoCtxArgs) > 0)
{
read_allocator.call_destructors...>();
}
}
template
Return operator()(bool has_context, Func&& func, stack_allocator& read_allocator, ExtraArgs&& ... args)
{
constexpr auto seq = std::make_integer_sequence();
#if BLT_DEBUG_LEVEL > 0
try
{
#endif
Return ret = exec_sequence_to_indices(std::forward(func), read_allocator, seq, std::forward(args)...);
if (has_context)
call_destructors_without_first(read_allocator);
else
read_allocator.call_destructors...>();
read_allocator.pop_bytes((stack_allocator::aligned_size>() + ...));
return ret;
#if BLT_DEBUG_LEVEL > 0
} catch (const std::runtime_error& e)
{
print_args(seq);
throw std::runtime_error(e.what());
}
#endif
}
};
template
struct call_without_first : public call_with
{
using call_with::call_with;
};
template
class operation_t;
template
class operation_t
{
public:
using function_t = RawFunction;
using First_Arg = typename blt::meta::arg_helper::First;
constexpr operation_t(const operation_t& copy) = default;
constexpr operation_t(operation_t&& move) = default;
template
constexpr explicit operation_t(const Functor& functor, std::optional name = {}): func(functor), name(name)
{}
[[nodiscard]] constexpr inline Return operator()(stack_allocator& read_allocator) const
{
if constexpr (sizeof...(Args) == 0)
{
return func();
} else
{
return call_with()(false, func, read_allocator);
}
}
[[nodiscard]] constexpr inline Return operator()(void* context, stack_allocator& read_allocator) const
{
// should be an impossible state
if constexpr (sizeof...(Args) == 0)
{
BLT_ABORT("Cannot pass context to function without arguments!");
}
auto& ctx_ref = *static_cast::type>*>(context);
if constexpr (sizeof...(Args) == 1)
{
return func(ctx_ref);
} else
{
return call_without_first()(true, func, read_allocator, ctx_ref);
}
}
template
[[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::type>>)
{
// first arg is context
write_allocator.push(this->operator()(context, read_allocator));
} else
{
// first arg isn't context
write_allocator.push(this->operator()(read_allocator));
}
};
}
[[nodiscard]] inline constexpr std::optional get_name() const
{
return name;
}
inline constexpr const auto& get_function() const
{
return func;
}
inline auto set_ephemeral()
{
is_ephemeral_ = true;
return *this;
}
inline bool is_ephemeral()
{
return is_ephemeral_;
}
operator_id id = -1;
private:
function_t func;
std::optional name;
bool is_ephemeral_ = false;
};
template
class operation_t : public operation_t
{
public:
using operation_t::operation_t;
};
template
operation_t(Lambda) -> operation_t;
template
operation_t(Return(*)(Args...)) -> operation_t;
template
operation_t(Lambda, std::optional) -> operation_t;
template
operation_t(Return(*)(Args...), std::optional) -> operation_t;
}
#endif //BLT_GP_OPERATIONS_H