xo-procedure2: procedure abstraction for Schematika
This commit is contained in:
parent
c7913a81db
commit
e35c85def0
19 changed files with 885 additions and 0 deletions
0
xo-procedure2/include/xo/procedure2/.gitkeep
Normal file
0
xo-procedure2/include/xo/procedure2/.gitkeep
Normal file
94
xo-procedure2/include/xo/procedure2/DPrimitive.hpp
Normal file
94
xo-procedure2/include/xo/procedure2/DPrimitive.hpp
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
/** @file DPrimitive.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Jan 2025
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <xo/object2/DArray.hpp>
|
||||
#include <xo/gc/GCObjectConversion.hpp>
|
||||
#include <xo/gc/GCObject.hpp>
|
||||
#include <xo/alloc2/Allocator.hpp>
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <string_view>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @brief Extract return type and argument types from a function type.
|
||||
*
|
||||
* Primary template (undefined) - specializations handle specific cases
|
||||
**/
|
||||
template <typename Fn>
|
||||
struct FnTraits;
|
||||
|
||||
/** specialization for function pointers **/
|
||||
template <typename R, typename... Args>
|
||||
struct FnTraits<R(*)(Args...)> {
|
||||
/** function return type **/
|
||||
using return_type = R;
|
||||
/** tuple type for function arguments **/
|
||||
using args_tuple = std::tuple<Args...>;
|
||||
/** number of arguments **/
|
||||
static constexpr std::size_t n_args = sizeof...(Args);
|
||||
|
||||
/** arg_type<i> is the type of the i'th argument to Fn **/
|
||||
template <std::size_t I>
|
||||
using arg_type = std::tuple_element_t<I, args_tuple>;
|
||||
};
|
||||
|
||||
/** specialization for function references **/
|
||||
template <typename R, typename... Args>
|
||||
struct FnTraits<R(&)(Args...)> : FnTraits<R(*)(Args...)> {};
|
||||
|
||||
/** specialization for plain function types **/
|
||||
template <typename R, typename... Args>
|
||||
struct FnTraits<R(Args...)> : FnTraits<R(*)(Args...)> {};
|
||||
|
||||
template <typename Fn>
|
||||
class Primitive {
|
||||
public:
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
using DArray = xo::scm::DArray;
|
||||
using Traits = FnTraits<Fn>;
|
||||
|
||||
public:
|
||||
Primitive(std::string_view name, Fn fn) : name_{name}, fn_{fn} {}
|
||||
|
||||
bool is_nary() const noexcept { return false; }
|
||||
static constexpr std::int32_t n_args() noexcept { return Traits::n_args; }
|
||||
|
||||
obj<AGCObject> apply_nocheck(obj<AAllocator> mm, const DArray * args) {
|
||||
return _apply_nocheck(mm, args,
|
||||
std::make_index_sequence<Traits::n_args>{});
|
||||
}
|
||||
|
||||
private:
|
||||
template <std::size_t... Is>
|
||||
obj<AGCObject> _apply_nocheck(obj<AAllocator> mm,
|
||||
const DArray * args,
|
||||
std::index_sequence<Is...>)
|
||||
{
|
||||
using R = typename Traits::return_type;
|
||||
|
||||
R result
|
||||
= fn_(GCObjectConversion<typename Traits::template arg_type<Is>>::from_gco(mm, args->at(Is))...
|
||||
);
|
||||
|
||||
return GCObjectConversion<R>::to_gco(mm, result);
|
||||
}
|
||||
|
||||
private:
|
||||
/** name of this primitive **/
|
||||
std::string_view name_;
|
||||
/** function implementation **/
|
||||
Fn fn_;
|
||||
}; /*Primitive*/
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end DPrimitive.hpp */
|
||||
22
xo-procedure2/include/xo/procedure2/Procedure.hpp
Normal file
22
xo-procedure2/include/xo/procedure2/Procedure.hpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/** @file Procedure.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/Procedure.json5]
|
||||
* 2. jinja2 template for facet .hpp file:
|
||||
* [facet.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/Procedure.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "detail/AProcedure.hpp"
|
||||
#include "detail/IProcedure_Any.hpp"
|
||||
#include "detail/IProcedure_Xfer.hpp"
|
||||
#include "detail/RProcedure.hpp"
|
||||
|
||||
|
||||
/* end Procedure.hpp */
|
||||
77
xo-procedure2/include/xo/procedure2/detail/AProcedure.hpp
Normal file
77
xo-procedure2/include/xo/procedure2/detail/AProcedure.hpp
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
/** @file AProcedure.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/Procedure.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [abstract_facet.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/Procedure.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
// includes (via {facet_includes})
|
||||
#include <xo/facet/obj.hpp>
|
||||
#include <xo/facet/facet_implementation.hpp>
|
||||
#include <xo/facet/typeseq.hpp>
|
||||
|
||||
// {pretex} here
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
using Copaque = const void *;
|
||||
using Opaque = void *;
|
||||
|
||||
/**
|
||||
Abstraction for a schematika procedure
|
||||
**/
|
||||
class AProcedure {
|
||||
public:
|
||||
/** @defgroup scm-procedure-type-traits **/
|
||||
///@{
|
||||
// types
|
||||
/** integer identifying a type **/
|
||||
using typeseq = xo::facet::typeseq;
|
||||
using Copaque = const void *;
|
||||
using Opaque = void *;
|
||||
/** a gc-aware object **/
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
///@}
|
||||
|
||||
/** @defgroup scm-procedure-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** RTTI: unique id# for actual runtime data representation **/
|
||||
virtual typeseq _typeseq() const noexcept = 0;
|
||||
/** true iff procedure takes n arguments **/
|
||||
virtual bool is_nary(Copaque data) const noexcept = 0;
|
||||
/** number of arguments. -1 for n-ary **/
|
||||
virtual std::int32_t n_args(Copaque data) const noexcept = 0;
|
||||
|
||||
// nonconst methods
|
||||
/** invoke procedure; assume arguments satisfy type system **/
|
||||
virtual obj<AGCObject> apply_nocheck(Opaque data, obj<AAllocator> mm, const DArray * args) = 0;
|
||||
///@}
|
||||
}; /*AProcedure*/
|
||||
|
||||
/** Implementation IProcedure_DRepr of AProcedure for state DRepr
|
||||
* should provide a specialization:
|
||||
*
|
||||
* template <>
|
||||
* struct xo::facet::FacetImplementation<AProcedure, DRepr> {
|
||||
* using Impltype = IProcedure_DRepr;
|
||||
* };
|
||||
*
|
||||
* then IProcedure_ImplType<DRepr> --> IProcedure_DRepr
|
||||
**/
|
||||
template <typename DRepr>
|
||||
using IProcedure_ImplType = xo::facet::FacetImplType<AProcedure, DRepr>;
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* AProcedure.hpp */
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
/** @file IProcedure_Any.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/Procedure.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_any.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/Procedure.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "AProcedure.hpp"
|
||||
#include <xo/facet/obj.hpp>
|
||||
|
||||
namespace xo { namespace scm { class IProcedure_Any; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
|
||||
template <>
|
||||
struct FacetImplementation<xo::scm::AProcedure,
|
||||
DVariantPlaceholder>
|
||||
{
|
||||
using ImplType = xo::scm::IProcedure_Any;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
/** @class IProcedure_Any
|
||||
* @brief AProcedure implementation for empty variant instance
|
||||
**/
|
||||
class IProcedure_Any : public AProcedure {
|
||||
public:
|
||||
/** @defgroup scm-procedure-any-type-traits **/
|
||||
///@{
|
||||
|
||||
/** integer identifying a type **/
|
||||
using typeseq = xo::facet::typeseq;
|
||||
using AGCObject = AProcedure::AGCObject;
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-procedure-any-methods **/
|
||||
///@{
|
||||
|
||||
const AProcedure * iface() const { return std::launder(this); }
|
||||
|
||||
// from AProcedure
|
||||
|
||||
// const methods
|
||||
typeseq _typeseq() const noexcept override { return s_typeseq; }
|
||||
[[noreturn]] bool is_nary(Copaque) const noexcept override { _fatal(); }
|
||||
[[noreturn]] std::int32_t n_args(Copaque) const noexcept override { _fatal(); }
|
||||
|
||||
// nonconst methods
|
||||
[[noreturn]] obj<AGCObject> apply_nocheck(Opaque, obj<AAllocator>, const DArray *) override;
|
||||
|
||||
///@}
|
||||
|
||||
private:
|
||||
/** @defgraoup scm-procedure-any-private-methods **/
|
||||
///@{
|
||||
|
||||
[[noreturn]] static void _fatal();
|
||||
|
||||
///@}
|
||||
|
||||
public:
|
||||
/** @defgroup scm-procedure-any-member-vars **/
|
||||
///@{
|
||||
|
||||
static typeseq s_typeseq;
|
||||
static bool _valid;
|
||||
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm */
|
||||
} /*namespace xo */
|
||||
|
||||
/* IProcedure_Any.hpp */
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
/** @file IProcedure_Xfer.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/Procedure.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_any.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/Procedure.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class IProcedure_Xfer
|
||||
**/
|
||||
template <typename DRepr, typename IProcedure_DRepr>
|
||||
class IProcedure_Xfer : public AProcedure {
|
||||
public:
|
||||
/** @defgroup scm-procedure-xfer-type-traits **/
|
||||
///@{
|
||||
/** actual implementation (not generated; often delegates to DRepr) **/
|
||||
using Impl = IProcedure_DRepr;
|
||||
/** integer identifying a type **/
|
||||
using typeseq = AProcedure::typeseq;
|
||||
using AGCObject = AProcedure::AGCObject;
|
||||
///@}
|
||||
|
||||
/** @defgroup scm-procedure-xfer-methods **/
|
||||
///@{
|
||||
|
||||
static const DRepr & _dcast(Copaque d) { return *(const DRepr *)d; }
|
||||
static DRepr & _dcast(Opaque d) { return *(DRepr *)d; }
|
||||
|
||||
// from AProcedure
|
||||
|
||||
// const methods
|
||||
typeseq _typeseq() const noexcept override { return s_typeseq; }
|
||||
bool is_nary(Copaque data) const noexcept override {
|
||||
return I::is_nary(_dcast(data));
|
||||
}
|
||||
std::int32_t n_args(Copaque data) const noexcept override {
|
||||
return I::n_args(_dcast(data));
|
||||
}
|
||||
|
||||
// non-const methods
|
||||
obj<AGCObject> apply_nocheck(Opaque data, obj<AAllocator> mm, const DArray * args) override {
|
||||
return I::apply_nocheck(_dcast(data), mm, args);
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
private:
|
||||
using I = Impl;
|
||||
|
||||
public:
|
||||
/** @defgroup scm-procedure-xfer-member-vars **/
|
||||
///@{
|
||||
|
||||
/** typeseq for template parameter DRepr **/
|
||||
static typeseq s_typeseq;
|
||||
/** true iff satisfies facet implementation **/
|
||||
static bool _valid;
|
||||
|
||||
///@}
|
||||
};
|
||||
|
||||
template <typename DRepr, typename IProcedure_DRepr>
|
||||
xo::facet::typeseq
|
||||
IProcedure_Xfer<DRepr, IProcedure_DRepr>::s_typeseq
|
||||
= xo::facet::typeseq::id<DRepr>();
|
||||
|
||||
template <typename DRepr, typename IProcedure_DRepr>
|
||||
bool
|
||||
IProcedure_Xfer<DRepr, IProcedure_DRepr>::_valid
|
||||
= xo::facet::valid_facet_implementation<AProcedure,
|
||||
IProcedure_Xfer>();
|
||||
|
||||
} /*namespace scm */
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end IProcedure_Xfer.hpp */
|
||||
86
xo-procedure2/include/xo/procedure2/detail/RProcedure.hpp
Normal file
86
xo-procedure2/include/xo/procedure2/detail/RProcedure.hpp
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/** @file RProcedure.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/Procedure.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_any.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/Procedure.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "AProcedure.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
/** @class RProcedure
|
||||
**/
|
||||
template <typename Object>
|
||||
class RProcedure : public Object {
|
||||
private:
|
||||
using O = Object;
|
||||
|
||||
public:
|
||||
/** @defgroup scm-procedure-router-type-traits **/
|
||||
///@{
|
||||
using ObjectType = Object;
|
||||
using DataPtr = Object::DataPtr;
|
||||
using typeseq = xo::reflect::typeseq;
|
||||
using AGCObject = AProcedure::AGCObject;
|
||||
///@}
|
||||
|
||||
/** @defgroup scm-procedure-router-ctors **/
|
||||
///@{
|
||||
RProcedure() {}
|
||||
RProcedure(Object::DataPtr data) : Object{std::move(data)} {}
|
||||
RProcedure(const AProcedure * iface, void * data)
|
||||
requires std::is_same_v<typename Object::DataType, xo::facet::DVariantPlaceholder>
|
||||
: Object(iface, data) {}
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-procedure-router-methods **/
|
||||
///@{
|
||||
|
||||
// const methods
|
||||
typeseq _typeseq() const noexcept { return O::iface()->_typeseq(); }
|
||||
bool is_nary() const noexcept {
|
||||
return O::iface()->is_nary(O::data());
|
||||
}
|
||||
std::int32_t n_args() const noexcept {
|
||||
return O::iface()->n_args(O::data());
|
||||
}
|
||||
|
||||
// non-const methods (still const in router!)
|
||||
obj<AGCObject> apply_nocheck(obj<AAllocator> mm, const DArray * args) {
|
||||
return O::iface()->apply_nocheck(O::data(), mm, args);
|
||||
}
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-procedure-member-vars **/
|
||||
///@{
|
||||
|
||||
static bool _valid;
|
||||
|
||||
///@}
|
||||
};
|
||||
|
||||
template <typename Object>
|
||||
bool
|
||||
RProcedure<Object>::_valid = xo::facet::valid_object_router<Object>();
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
namespace xo { namespace facet {
|
||||
template <typename Object>
|
||||
struct RoutingFor<xo::scm::AProcedure, Object> {
|
||||
using RoutingType = xo::scm::RProcedure<Object>;
|
||||
};
|
||||
} }
|
||||
|
||||
/* end RProcedure.hpp */
|
||||
29
xo-procedure2/include/xo/procedure2/init_primitives.hpp
Normal file
29
xo-procedure2/include/xo/procedure2/init_primitives.hpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/** @file init_primitives.hpp **/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "DPrimitive.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
using Primitive_f64_1_f64 = Primitive<double (*)(double)>;
|
||||
using Primitive_f64_2_f64_f64 = Primitive<double (*)(double, double)>;
|
||||
|
||||
struct Primitives {
|
||||
static Primitive_f64_1_f64 s_neg_f64_pm;
|
||||
|
||||
static Primitive_f64_2_f64_f64 s_add_f64_f64_pm;
|
||||
static Primitive_f64_2_f64_f64 s_sub_f64_f64_pm;
|
||||
static Primitive_f64_2_f64_f64 s_mul_f64_f64_pm;
|
||||
static Primitive_f64_2_f64_f64 s_div_f64_f64_pm;
|
||||
static Primitive_f64_2_f64_f64 s_pow_f64_f64_pm;
|
||||
|
||||
static Primitive_f64_1_f64 s_log_f64_pm;
|
||||
static Primitive_f64_1_f64 s_sin_f64_pm;
|
||||
static Primitive_f64_1_f64 s_cos_f64_pm;
|
||||
static Primitive_f64_1_f64 s_tan_f64_pm;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/* end init_primitives.hpp */
|
||||
21
xo-procedure2/include/xo/procedure2/init_procedure2.hpp
Normal file
21
xo-procedure2/include/xo/procedure2/init_procedure2.hpp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/** @file init_procedure2.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Jan 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <xo/subsys/Subsystem.hpp>
|
||||
|
||||
namespace xo {
|
||||
/* tag to represent the xo-procedure2/ subsystem within ordered iniitalization */
|
||||
enum S_procedure2_tag {};
|
||||
|
||||
template <>
|
||||
struct InitSubsys<S_procedure2_tag> {
|
||||
static void init();
|
||||
static InitEvidence require();
|
||||
};
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end init_procedure2.hpp */
|
||||
13
xo-procedure2/include/xo/procedure2/primitives.hpp
Normal file
13
xo-procedure2/include/xo/procedure2/primitives.hpp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/** @file primitives.hpp **/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
struct Primitives {
|
||||
static void init_primitives();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/* end primitives.hpp */
|
||||
Loading…
Add table
Add a link
Reference in a new issue