From aa55e20e1279800980bba44e736c2a4af0cc560a Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Fri, 14 Jun 2024 10:55:08 -0400 Subject: [PATCH] xo-reflect: + infra for reflecting functions --- include/xo/reflect/Metatype.hpp | 57 +++++++++++---------- include/xo/reflect/function/FunctionTdx.hpp | 49 ++++++++++++++++++ src/reflect/function/FunctionTdx.cpp | 37 +++++++++++++ 3 files changed, 116 insertions(+), 27 deletions(-) create mode 100644 include/xo/reflect/function/FunctionTdx.hpp create mode 100644 src/reflect/function/FunctionTdx.cpp diff --git a/include/xo/reflect/Metatype.hpp b/include/xo/reflect/Metatype.hpp index 23f3fd10..549d098e 100644 --- a/include/xo/reflect/Metatype.hpp +++ b/include/xo/reflect/Metatype.hpp @@ -5,34 +5,37 @@ #include namespace xo { - namespace reflect { - enum class Metatype { mt_invalid, mt_atomic, mt_pointer, mt_vector, mt_struct }; + namespace reflect { + enum class Metatype { mt_invalid, mt_atomic, mt_pointer, mt_vector, mt_struct, mt_function }; - inline std::ostream & operator<<(std::ostream & os, - Metatype x) { - switch(x) { - case Metatype::mt_invalid: - os << "invalid!"; - break; - case Metatype::mt_atomic: - os << "atomic"; - break; - case Metatype::mt_pointer: - os << "pointer"; - break; - case Metatype::mt_vector: - os << "vector"; - break; - case Metatype::mt_struct: - os << "struct"; - break; - default: - os << "???"; - } - return os; - } /*operator<<*/ - - } /*namespace reflect*/ + inline std::ostream & operator<<(std::ostream & os, + Metatype x) { + switch(x) { + case Metatype::mt_invalid: + os << "invalid!"; + break; + case Metatype::mt_atomic: + os << "atomic"; + break; + case Metatype::mt_pointer: + os << "pointer"; + break; + case Metatype::mt_vector: + os << "vector"; + break; + case Metatype::mt_struct: + os << "struct"; + break; + case Metatype::mt_function: + os << "function"; + break; + default: + os << "???"; + } + return os; + } /*operator<<*/ + + } /*namespace reflect*/ } /*namespace xo*/ /* end Metatype.hpp */ diff --git a/include/xo/reflect/function/FunctionTdx.hpp b/include/xo/reflect/function/FunctionTdx.hpp new file mode 100644 index 00000000..1558328d --- /dev/null +++ b/include/xo/reflect/function/FunctionTdx.hpp @@ -0,0 +1,49 @@ +/** @file FunctionTdx.hpp + * + * Author: Roland Conybeare + **/ + +#pragma once + +#include "xo/reflect/TypeDescrExtra.hpp" +#include "xo/reflect/EstablishTypeDescr.hpp" + +namespace xo { + namespace reflect { + /** Additional type-associated information for a function/procedure **/ + class FunctionTdx : public TypeDescrExtra { + public: + virtual ~FunctionTdx() = default; + + /** create instance. Will be invoked exactly once for each reflected function type **/ + static std::unique_ptr make_function(TypeDescr retval_td, + std::vector arg_td_v); + + + + // ----- Inherited from TypeDescrExtra ----- + + virtual Metatype metatype() const override { return Metatype::mt_function; } + virtual uint32_t n_child(void * /*object*/) const override { return 0; } + virtual TaggedPtr child_tp(uint32_t i, void * object) const override; + const std::string & struct_member_name(uint32_t i) const override; + + virtual uint32_t n_fn_arg() const override { return arg_td_v_.size(); } + virtual TypeDescr fn_retval() const override { return retval_td_; } + virtual TypeDescr fn_arg(uint32_t i) const override { return arg_td_v_[i]; } + + private: + FunctionTdx(TypeDescr retval_td, + std::vector arg_td_v); + + private: + /** function return value **/ + TypeDescr retval_td_; + /** function arguments, in positional order **/ + std::vector arg_td_v_; + }; /*FunctionTdx*/ + } /*namespace reflect*/ +} /*namespace xo*/ + + +/** end FunctionTdx.hpp **/ diff --git a/src/reflect/function/FunctionTdx.cpp b/src/reflect/function/FunctionTdx.cpp new file mode 100644 index 00000000..1b715fcd --- /dev/null +++ b/src/reflect/function/FunctionTdx.cpp @@ -0,0 +1,37 @@ +/* @file FunctionTdx.cpp */ + +#include "function/FunctionTdx.hpp" +#include "TaggedPtr.hpp" + +namespace xo { + namespace reflect { + /** create instance. Will be invoked exactly once for each reflected function type **/ + std::unique_ptr + FunctionTdx::make_function(TypeDescr retval_td, + std::vector arg_td_v) + { + return std::unique_ptr(new FunctionTdx(retval_td, std::move(arg_td_v))); + } + + FunctionTdx::FunctionTdx(TypeDescr retval_td, + std::vector arg_td_v) + : retval_td_{retval_td}, + arg_td_v_{std::move(arg_td_v)} + {} + + TaggedPtr + FunctionTdx::child_tp(uint32_t /*i*/, void * /*object*/) const + { + return TaggedPtr::universal_null(); + } + + const std::string & + FunctionTdx::struct_member_name(uint32_t i) const + { + return TypeDescrExtra::struct_member_name(i); + } + } /*namespace reflect*/ +} /*namespace xo*/ + + +/* end FunctionTdx.cpp */