From 8a9ecb348447a37dd3025461b786e6e9ece41bb2 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 24 Jun 2024 14:00:08 -0400 Subject: [PATCH] so progress is slow --- CMakeLists.txt | 2 +- examples/gp_test_2.cpp | 2 +- examples/main.cpp | 63 ++++++++++++++++++++++++++++++++++- include/blt/gp/fwdecl.h | 9 +++++ include/blt/gp/operations.h | 66 +++++++++++++++++++++++-------------- include/blt/gp/program.h | 1 + lib/blt | 2 +- 7 files changed, 117 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c0fd5e..14f7883 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.28) +project(blt-gp VERSION 0.0.29) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/examples/gp_test_2.cpp b/examples/gp_test_2.cpp index 894e2ad..4829051 100644 --- a/examples/gp_test_2.cpp +++ b/examples/gp_test_2.cpp @@ -31,7 +31,7 @@ blt::gp::operation_t lit([]() { return dist(program.get_random()); }); -int main() +int main_old() { type_system.register_type(); type_system.register_type(); diff --git a/examples/main.cpp b/examples/main.cpp index 7b67789..a330691 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -17,6 +17,7 @@ */ #include #include +#include #include #include #include @@ -300,9 +301,32 @@ struct super_large unsigned char data[5129]; }; +struct context +{ + float x, y; +}; + +namespace blt::gp::detail +{ + class operator_storage_test + { + public: + explicit operator_storage_test(blt::gp::gp_operations& ops): ops(ops) + {} + + inline blt::gp::detail::callable_t& operator[](blt::size_t index) + { + return ops.operators[index]; + } + + private: + blt::gp::gp_operations& ops; + }; +} + blt::gp::stack_allocator alloc; -int main_old() +int main() { constexpr blt::size_t MAX_ALIGNMENT = 8; test(); @@ -381,6 +405,43 @@ int main_old() std::cout << std::endl; + blt::gp::operation_t silly_op_3([](const context& ctx, float f) { + return ctx.x + ctx.y + f; + }); + + blt::gp::operation_t silly_op_4([](const context& ctx) { + return ctx.x; + }); + + blt::gp::type_system system; + system.register_type(); + blt::gp::gp_operations ops{system}; + + //BLT_TRACE(blt::type_string()); + //BLT_TRACE(typeid(decltype(silly_op_3)::first::type).name()); + //BLT_TRACE(blt::type_string>()); + //BLT_TRACE("Same types? %s", (std::is_same_v>) ? "true" : "false"); + + ops.add_operator(silly_op_3); + ops.add_operator(silly_op_4); + ops.add_operator(silly_op_2); + + blt::gp::detail::operator_storage_test de(ops); + + context hello{5, 10}; + + alloc.push(1.153f); + de[0](static_cast(&hello), alloc); + BLT_TRACE("first value: %f", alloc.pop()); + + de[1](static_cast(&hello), alloc); + BLT_TRACE("second value: %f", alloc.pop()); + + alloc.push(1.0f); + alloc.push(52.213f); + de[2](static_cast(&hello), alloc); + BLT_TRACE("third value: %f", alloc.pop()); + //auto* pointer = static_cast(head->metadata.offset); //return std::align(alignment, bytes, pointer, remaining_bytes); diff --git a/include/blt/gp/fwdecl.h b/include/blt/gp/fwdecl.h index 81b6b33..a1afacb 100644 --- a/include/blt/gp/fwdecl.h +++ b/include/blt/gp/fwdecl.h @@ -24,13 +24,22 @@ namespace blt::gp { class gp_program; + class type; + class type_system; class tree_generator_t; + class grow_generator_t; + class full_generator_t; + namespace detail + { + class operator_storage_test; + } + } #endif //BLT_GP_FWDECL_H diff --git a/include/blt/gp/operations.h b/include/blt/gp/operations.h index 480f3f5..9813213 100644 --- a/include/blt/gp/operations.h +++ b/include/blt/gp/operations.h @@ -30,6 +30,42 @@ namespace blt::gp namespace detail { using callable_t = std::function; + + 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 { }; @@ -66,17 +102,6 @@ namespace blt::gp } }; - template - struct first_arg - { - using type = First; - }; - - template<> - struct first_arg - { - }; - template struct call_without_first : public call_with { @@ -118,7 +143,7 @@ namespace blt::gp { BLT_ABORT("Cannot pass context to function without arguments!"); } - auto& ctx_ref = *static_cast::type*>(context); + auto& ctx_ref = *static_cast::type>*>(context); if constexpr (sizeof...(Args) == 1) { return func(ctx_ref); @@ -132,21 +157,14 @@ namespace blt::gp [[nodiscard]] detail::callable_t make_callable() const { return [this](void* context, stack_allocator& values) { - if constexpr (sizeof...(Args) == 0) + if constexpr (detail::is_same_v::type>>) { - values.push(this->operator()(values)); + // first arg is context + values.push(this->operator()(context, values)); } else { - // annoying hack. - if constexpr (std::is_same_v::type>) - { - // first arg is context - values.push(this->operator()(context, values)); - } else - { - // first arg isn't context - values.push(this->operator()(values)); - } + // first arg isn't context + values.push(this->operator()(values)); } }; } diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index 826c847..80fb5c0 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -50,6 +50,7 @@ namespace blt::gp class gp_operations { friend class gp_program; + friend class blt::gp::detail::operator_storage_test; public: explicit gp_operations(type_system& system): system(system) diff --git a/lib/blt b/lib/blt index ac163a3..2a34be2 160000 --- a/lib/blt +++ b/lib/blt @@ -1 +1 @@ -Subproject commit ac163a34b9d70346df80fcac44054e8dffe9ad43 +Subproject commit 2a34be2e7b452aa623b9043b7decf83c2e367cc7