blt-gp/include/blt/gp/program.h

150 lines
4.5 KiB
C
Raw Normal View History

2024-06-02 21:27:00 -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_PROGRAM_H
#define BLT_GP_PROGRAM_H
2024-06-06 02:25:42 -04:00
#include <blt/gp/stack.h>
2024-06-04 21:52:43 -04:00
#include <cstddef>
2024-06-02 21:27:00 -04:00
#include <blt/gp/fwdecl.h>
#include <functional>
#include <blt/std/ranges.h>
2024-06-03 02:29:51 -04:00
#include <blt/std/types.h>
#include <blt/std/utility.h>
2024-06-02 21:27:00 -04:00
#include <type_traits>
2024-06-03 02:29:51 -04:00
#include <string_view>
#include <string>
#include <utility>
#include <iostream>
2024-06-02 21:27:00 -04:00
namespace blt::gp
{
class identifier
{
};
2024-06-03 02:29:51 -04:00
class type
{
public:
template<typename T>
static type make_type(blt::size_t id)
{
return type(sizeof(T), id, blt::type_string<T>());
}
[[nodiscard]] blt::size_t size() const
{
return size_;
}
[[nodiscard]] blt::size_t id() const
{
return id_;
}
[[nodiscard]] std::string_view name() const
{
return name_;
}
private:
type(size_t size, size_t id, std::string_view name): size_(size), id_(id), name_(name)
{}
blt::size_t size_;
blt::size_t id_;
std::string name_;
};
class type_system
{
public:
type_system() = default;
template<typename T>
inline type register_type()
{
types.push_back(type::make_type<T>(types.size()));
return types.back();
}
private:
std::vector<type> types;
};
template<typename Return, typename... Args>
class operation
{
public:
using function_t = std::function<Return(Args...)>;
operation(const operation& copy) = default;
operation(operation&& move) = default;
2024-06-06 02:25:42 -04:00
explicit operation(const function_t& functor): func(functor)
{}
explicit operation(function_t&& functor): func(std::move(functor))
{}
2024-06-06 02:25:42 -04:00
template<blt::u64 index>
2024-06-06 02:27:06 -04:00
[[nodiscard]] inline constexpr blt::size_t getByteOffset() const
{
blt::size_t offset = 0;
blt::size_t current_index = 0;
2024-06-06 02:25:42 -04:00
((offset += (current_index++ > index ? stack_allocator::aligned_size<Args>() : 0)), ...);
return offset;
}
2024-06-06 02:25:42 -04:00
template<blt::u64... indices>
inline Return sequence_to_indices(stack_allocator& allocator, std::integer_sequence<blt::u64, indices...>) const
{
2024-06-06 02:44:28 -04:00
// expands Args and indices, providing each argument with its index calculating the current argument byte offset
return func(allocator.from<Args>(getByteOffset<indices>())...);
}
2024-06-06 02:25:42 -04:00
[[nodiscard]] inline Return operator()(stack_allocator& allocator) const
{
auto seq = std::make_integer_sequence<blt::u64, sizeof...(Args)>();
2024-06-06 02:32:19 -04:00
Return ret = sequence_to_indices(allocator, seq);
2024-06-06 02:44:28 -04:00
allocator.pop_bytes((stack_allocator::aligned_size<Args>() + ...));
2024-06-06 02:32:19 -04:00
return ret;
2024-06-04 21:52:43 -04:00
}
2024-06-03 02:29:51 -04:00
private:
2024-06-06 02:25:42 -04:00
// template<typename T, blt::size_t index>
// static inline T& access_pack_index(blt::span<void*> args)
// {
// return *reinterpret_cast<T*>(args[index]);
// }
//
// template<typename T, T... indexes>
// Return function_evaluator(blt::span<void*> args, std::integer_sequence<T, indexes...>)
// {
// return func(access_pack_index<Args, indexes>(args)...);
// }
2024-06-04 14:04:49 -04:00
2024-06-06 02:25:42 -04:00
function_t func;
2024-06-03 02:29:51 -04:00
};
2024-06-02 21:27:00 -04:00
}
#endif //BLT_GP_PROGRAM_H