xo-interpreter2: scaffold repl + alloc measurement frameowkr

This commit is contained in:
Roland Conybeare 2026-02-02 21:55:34 -05:00
commit 9a2370e362
9 changed files with 452 additions and 0 deletions

View file

@ -18,6 +18,23 @@ add_definitions(${PROJECT_CXX_FLAGS})
# ----------------------------------------------------------------
# note: manual target; generated code committed to git
xo_add_genfacet(
TARGET xo-alloc2-facet-resourcevisitor
FACET ResourceVisitor
INPUT idl/ResourceVisitor.json5
OUTPUT_HPP_DIR include/xo/alloc2
OUTPUT_IMPL_SUBDIR visitor
OUTPUT_CPP_DIR src/alloc2
)
# ----------------------------------------------------------------
xo_add_genfacet_all(xo-alloc2-genfacet-all)
# ----------------------------------------------------------------
# must complete definition of expression lib before configuring examples
add_subdirectory(src/alloc2)
add_subdirectory(utest)

49
idl/ResourceVisitor.json5 Normal file
View file

@ -0,0 +1,49 @@
{
mode: "facet",
includes: [
"\"Allocator.hpp\""
],
// extra includes in ResourceVisitor.hpp, if any
user_hpp_includes: [],
namespace1: "xo",
namespace2: "mm",
// text after includes, before AResourceVisitor
pretext: [ "// {pretext} here" ],
facet: "ResourceVisitor",
detail_subdir: "visitor",
brief: "visitor to inspect resource consumption",
using_doxygen: true,
doc: [
"Visitor to receive measured resource consumption"
],
types: [
// using size_type = std::size_t
{
name: "size_type",
doc: ["type for length of a sequence"],
definition: "std::size_t",
},
// // using AGCObject = xo::mm::AGCObject
// {
// name: "AGCObject",
// doc: ["facet for types with GC support"],
// definition: "xo::mm::AGCObject",
// }
],
const_methods: [
// bool on_memory(name, allocated, committed, reserved) const noexcept
{
name: "on_allocator",
doc: ["report memory consumption"],
return_type: "void",
args: [
{type: "obj<AAllocator>", name: "mm"},
],
const: true,
noexcept: true,
attributes: [],
},
],
nonconst_methods: [
],
}

View file

@ -0,0 +1,22 @@
/** @file ResourceVisitor.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/ResourceVisitor.json5]
* 2. jinja2 template for facet .hpp file:
* [facet.hpp.j2]
* 3. idl for facet methods
* [idl/ResourceVisitor.json5]
**/
#pragma once
#include "visitor/AResourceVisitor.hpp"
#include "visitor/IResourceVisitor_Any.hpp"
#include "visitor/IResourceVisitor_Xfer.hpp"
#include "visitor/RResourceVisitor.hpp"
/* end ResourceVisitor.hpp */

View file

@ -0,0 +1,74 @@
/** @file AResourceVisitor.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/ResourceVisitor.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [abstract_facet.hpp.j2]
* 3. idl for facet methods
* [idl/ResourceVisitor.json5]
**/
#pragma once
// includes (via {facet_includes})
#include "Allocator.hpp"
#include <xo/facet/obj.hpp>
#include <xo/facet/facet_implementation.hpp>
#include <xo/facet/typeseq.hpp>
// {pretext} here
namespace xo {
namespace mm {
using Copaque = const void *;
using Opaque = void *;
/**
Visitor to receive measured resource consumption
**/
class AResourceVisitor {
public:
/** @defgroup mm-resourcevisitor-type-traits **/
///@{
// types
/** integer identifying a type **/
using typeseq = xo::facet::typeseq;
using Copaque = const void *;
using Opaque = void *;
/** type for length of a sequence **/
using size_type = std::size_t;
///@}
/** @defgroup mm-resourcevisitor-methods **/
///@{
// const methods
/** RTTI: unique id# for actual runtime data representation **/
virtual typeseq _typeseq() const noexcept = 0;
/** report memory consumption **/
virtual void on_allocator(Copaque data, obj<AAllocator> mm) const noexcept = 0;
// nonconst methods
///@}
}; /*AResourceVisitor*/
/** Implementation IResourceVisitor_DRepr of AResourceVisitor for state DRepr
* should provide a specialization:
*
* template <>
* struct xo::facet::FacetImplementation<AResourceVisitor, DRepr> {
* using Impltype = IResourceVisitor_DRepr;
* };
*
* then IResourceVisitor_ImplType<DRepr> --> IResourceVisitor_DRepr
**/
template <typename DRepr>
using IResourceVisitor_ImplType = xo::facet::FacetImplType<AResourceVisitor, DRepr>;
} /*namespace mm*/
} /*namespace xo*/
/* AResourceVisitor.hpp */

View file

@ -0,0 +1,86 @@
/** @file IResourceVisitor_Any.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/ResourceVisitor.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/ResourceVisitor.json5]
**/
#pragma once
#include "AResourceVisitor.hpp"
#include <xo/facet/obj.hpp>
namespace xo { namespace mm { class IResourceVisitor_Any; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::mm::AResourceVisitor,
DVariantPlaceholder>
{
using ImplType = xo::mm::IResourceVisitor_Any;
};
}
}
namespace xo {
namespace mm {
/** @class IResourceVisitor_Any
* @brief AResourceVisitor implementation for empty variant instance
**/
class IResourceVisitor_Any : public AResourceVisitor {
public:
/** @defgroup mm-resourcevisitor-any-type-traits **/
///@{
/** integer identifying a type **/
using typeseq = xo::facet::typeseq;
using size_type = AResourceVisitor::size_type;
///@}
/** @defgroup mm-resourcevisitor-any-methods **/
///@{
const AResourceVisitor * iface() const { return std::launder(this); }
// from AResourceVisitor
// const methods
typeseq _typeseq() const noexcept override { return s_typeseq; }
[[noreturn]] void on_allocator(Copaque, obj<AAllocator>) const noexcept override { _fatal(); }
// nonconst methods
///@}
private:
/** @defgraoup mm-resourcevisitor-any-private-methods **/
///@{
[[noreturn]] static void _fatal();
///@}
public:
/** @defgroup mm-resourcevisitor-any-member-vars **/
///@{
static typeseq s_typeseq;
static bool _valid;
///@}
};
} /*namespace mm */
} /*namespace xo */
/* IResourceVisitor_Any.hpp */

View file

@ -0,0 +1,81 @@
/** @file IResourceVisitor_Xfer.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/ResourceVisitor.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/ResourceVisitor.json5]
**/
#pragma once
#include "Allocator.hpp"
namespace xo {
namespace mm {
/** @class IResourceVisitor_Xfer
**/
template <typename DRepr, typename IResourceVisitor_DRepr>
class IResourceVisitor_Xfer : public AResourceVisitor {
public:
/** @defgroup mm-resourcevisitor-xfer-type-traits **/
///@{
/** actual implementation (not generated; often delegates to DRepr) **/
using Impl = IResourceVisitor_DRepr;
/** integer identifying a type **/
using typeseq = AResourceVisitor::typeseq;
using size_type = AResourceVisitor::size_type;
///@}
/** @defgroup mm-resourcevisitor-xfer-methods **/
///@{
static const DRepr & _dcast(Copaque d) { return *(const DRepr *)d; }
static DRepr & _dcast(Opaque d) { return *(DRepr *)d; }
// from AResourceVisitor
// const methods
typeseq _typeseq() const noexcept override { return s_typeseq; }
void on_allocator(Copaque data, obj<AAllocator> mm) const noexcept override {
return I::on_allocator(_dcast(data), mm);
}
// non-const methods
///@}
private:
using I = Impl;
public:
/** @defgroup mm-resourcevisitor-xfer-member-vars **/
///@{
/** typeseq for template parameter DRepr **/
static typeseq s_typeseq;
/** true iff satisfies facet implementation **/
static bool _valid;
///@}
};
template <typename DRepr, typename IResourceVisitor_DRepr>
xo::facet::typeseq
IResourceVisitor_Xfer<DRepr, IResourceVisitor_DRepr>::s_typeseq
= xo::facet::typeseq::id<DRepr>();
template <typename DRepr, typename IResourceVisitor_DRepr>
bool
IResourceVisitor_Xfer<DRepr, IResourceVisitor_DRepr>::_valid
= xo::facet::valid_facet_implementation<AResourceVisitor,
IResourceVisitor_Xfer>();
} /*namespace mm */
} /*namespace xo*/
/* end IResourceVisitor_Xfer.hpp */

View file

@ -0,0 +1,80 @@
/** @file RResourceVisitor.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/ResourceVisitor.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/ResourceVisitor.json5]
**/
#pragma once
#include "AResourceVisitor.hpp"
namespace xo {
namespace mm {
/** @class RResourceVisitor
**/
template <typename Object>
class RResourceVisitor : public Object {
private:
using O = Object;
public:
/** @defgroup mm-resourcevisitor-router-type-traits **/
///@{
using ObjectType = Object;
using DataPtr = Object::DataPtr;
using typeseq = xo::reflect::typeseq;
using size_type = AResourceVisitor::size_type;
///@}
/** @defgroup mm-resourcevisitor-router-ctors **/
///@{
RResourceVisitor() {}
RResourceVisitor(Object::DataPtr data) : Object{std::move(data)} {}
RResourceVisitor(const AResourceVisitor * iface, void * data)
requires std::is_same_v<typename Object::DataType, xo::facet::DVariantPlaceholder>
: Object(iface, data) {}
///@}
/** @defgroup mm-resourcevisitor-router-methods **/
///@{
// const methods
typeseq _typeseq() const noexcept { return O::iface()->_typeseq(); }
void on_allocator(obj<AAllocator> mm) const noexcept {
return O::iface()->on_allocator(O::data(), mm);
}
// non-const methods (still const in router!)
///@}
/** @defgroup mm-resourcevisitor-member-vars **/
///@{
static bool _valid;
///@}
};
template <typename Object>
bool
RResourceVisitor<Object>::_valid = xo::facet::valid_object_router<Object>();
} /*namespace mm*/
} /*namespace xo*/
namespace xo { namespace facet {
template <typename Object>
struct RoutingFor<xo::mm::AResourceVisitor, Object> {
using RoutingType = xo::mm::RResourceVisitor<Object>;
};
} }
/* end RResourceVisitor.hpp */

View file

@ -17,6 +17,8 @@ set(SELF_SRCS
# DArenaIterator.cpp
IAllocIterator_DArenaIterator.cpp
IResourceVisitor_Any.cpp
)
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})

View file

@ -0,0 +1,41 @@
/** @file IResourceVisitor_Any.cpp
*
**/
#include "visitor/IResourceVisitor_Any.hpp"
#include <iostream>
namespace xo {
namespace mm {
using xo::facet::DVariantPlaceholder;
using xo::facet::typeseq;
using xo::facet::valid_facet_implementation;
void
IResourceVisitor_Any::_fatal()
{
/* control here on uninitialized IAllocator_Any.
* Initialized instance will have specific implementation type
*/
std::cerr << "fatal"
<< ": attempt to call uninitialized"
<< " IResourceVisitor_Any method"
<< std::endl;
std::terminate();
}
typeseq
IResourceVisitor_Any::s_typeseq = typeseq::id<DVariantPlaceholder>();
bool
IResourceVisitor_Any::_valid
= valid_facet_implementation<AResourceVisitor, IResourceVisitor_Any>();
// nonconst methods
} /*namespace mm*/
} /*namespace xo*/
/* end IResourceVisitor_Any.cpp */