xo-type: + DArrayType

This commit is contained in:
Roland Conybeare 2026-03-07 00:25:55 +11:00
commit 553e7c0151
17 changed files with 511 additions and 0 deletions

View file

@ -70,6 +70,26 @@ xo_add_genfacetimpl(
# ----------------------------------------------------------------
# note: manual target; generated code committed to git
xo_add_genfacetimpl(
TARGET xo-type-facetimpl-type-arraytype
FACET_PKG xo_type
FACET Type
REPR ArrayType
INPUT idl/IType_DArrayType.json5
)
# note: manual target; generated code committed to git
xo_add_genfacetimpl(
TARGET xo-type-facetimpl-gcobject-arraytype
FACET_PKG xo_alloc2
FACET GCObject
REPR ArrayType
INPUT idl/IGCObject_DArrayType.json5
)
# ----------------------------------------------------------------
xo_add_genfacet_all(xo-type-genfacet-all)
install(DIRECTORY idl/

View file

@ -0,0 +1,16 @@
{
mode: "implementation",
output_cpp_dir: "src/type",
output_hpp_dir: "include/xo/type",
output_impl_subdir: "array",
includes: [
],
local_types: [ ],
namespace1: "xo",
namespace2: "scm",
facet_idl: "idl/GCObject.json5",
brief: "provide AGCObject interface for DArrayType",
using_doxygen: true,
repr: "DArrayType",
doc: [ "implement AGCObject for DArrayType" ],
}

View file

@ -0,0 +1,16 @@
{
mode: "implementation",
output_cpp_dir: "src/type",
output_hpp_dir: "include/xo/type",
output_impl_subdir: "array",
includes: [],
local_types: [],
namespace1: "xo",
namespace2: "scm",
facet_idl: "idl/Type.json5",
brief: "provide AType interface for DArrayType",
using_doxygen: true,
repr: "DArrayType",
doc: [ "implement AType for DArrayType" ],
}

View file

@ -0,0 +1,12 @@
/** @file ArrayType.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "DArrayType.hpp"
#include "array/IType_DArrayType.hpp"
#include "array/IGCObject_DArrayType.hpp"
/* end ArrayType.hpp */

View file

@ -0,0 +1,60 @@
/** @file DArrayType.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "Type.hpp"
#include "Metatype.hpp"
#include <xo/alloc2/Collector.hpp>
#include <xo/alloc2/Allocator.hpp>
namespace xo {
namespace scm {
/** @brief A parameterized array type: array(T)
*
* Represents a fixed-size homogeneous collection.
**/
class DArrayType {
public:
using ACollector = xo::mm::ACollector;
using AAllocator = xo::mm::AAllocator;
public:
/** @defgroup xo-scm-arraytype-ctors **/
///@{
explicit DArrayType(obj<AType> elt);
/** create instance using memory from @p mm with element type @p elt_type **/
static DArrayType * _make(obj<AAllocator> mm, obj<AType> elt_type);
///@}
/** @defgroup xo-scm-arraytype-type-facet **/
///@{
Metatype metatype() const noexcept { return Metatype::array(); }
bool is_equal_to(const obj<AType> & y) const noexcept;
bool is_subtype_of(const obj<AType> & y) const noexcept;
///@}
/** @defgroup xo-scm-arraytype-gcobject-facet **/
///@{
std::size_t shallow_size() const noexcept;
DArrayType * shallow_copy(obj<AAllocator> mm) const noexcept;
std::size_t forward_children(obj<ACollector> gc) noexcept;
///@}
private:
/** @defgroup xo-scm-arraytype-member-vars **/
///@{
/** all array elements satisfy @ref elt_type_ **/
obj<AType> elt_type_;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end DArrayType.hpp */

View file

@ -46,6 +46,7 @@ namespace xo {
static Metatype any() { return Metatype(code::t_any); }
static Metatype list() { return Metatype(code::t_list); }
static Metatype array() { return Metatype(code::t_array); }
/** description string for this type category **/
const char * _descr() const noexcept;

View file

@ -5,6 +5,7 @@
#include "AtomicType.hpp"
#include "ListType.hpp"
#include "ArrayType.hpp"
#include <xo/alloc2/Allocator.hpp>
namespace xo {
@ -23,6 +24,11 @@ namespace xo {
static obj<AFacet,DListType> list_type(obj<AAllocator> mm, obj<AType> elt_type) {
return obj<AFacet,DListType>(DListType::_make(mm, elt_type));
}
template <typename AFacet = AType>
static obj<AFacet,DArrayType> array_type(obj<AAllocator> mm, obj<AType> elt_type) {
return obj<AFacet,DArrayType>(DArrayType::_make(mm, elt_type));
}
};
} /*namespace scm*/

View file

@ -0,0 +1,65 @@
/** @file IGCObject_DArrayType.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IGCObject_DArrayType.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IGCObject_DArrayType.json5]
**/
#pragma once
#include "GCObject.hpp"
#include "DArrayType.hpp"
namespace xo { namespace scm { class IGCObject_DArrayType; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::mm::AGCObject,
xo::scm::DArrayType>
{
using ImplType = xo::mm::IGCObject_Xfer
<xo::scm::DArrayType,
xo::scm::IGCObject_DArrayType>;
};
}
}
namespace xo {
namespace scm {
/** @class IGCObject_DArrayType
**/
class IGCObject_DArrayType {
public:
/** @defgroup scm-gcobject-darraytype-type-traits **/
///@{
using size_type = xo::mm::AGCObject::size_type;
using AAllocator = xo::mm::AGCObject::AAllocator;
using ACollector = xo::mm::AGCObject::ACollector;
using Copaque = xo::mm::AGCObject::Copaque;
using Opaque = xo::mm::AGCObject::Opaque;
///@}
/** @defgroup scm-gcobject-darraytype-methods **/
///@{
// const methods
/** memory consumption for this instance **/
static size_type shallow_size(const DArrayType & self) noexcept;
/** copy instance using allocator **/
static Opaque shallow_copy(const DArrayType & self, obj<AAllocator> mm) noexcept;
// non-const methods
/** during GC: forward immdiate children **/
static size_type forward_children(DArrayType & self, obj<ACollector> gc) noexcept;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -0,0 +1,63 @@
/** @file IType_DArrayType.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IType_DArrayType.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IType_DArrayType.json5]
**/
#pragma once
#include "Type.hpp"
#include "DArrayType.hpp"
namespace xo { namespace scm { class IType_DArrayType; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::scm::AType,
xo::scm::DArrayType>
{
using ImplType = xo::scm::IType_Xfer
<xo::scm::DArrayType,
xo::scm::IType_DArrayType>;
};
}
}
namespace xo {
namespace scm {
/** @class IType_DArrayType
**/
class IType_DArrayType {
public:
/** @defgroup scm-type-darraytype-type-traits **/
///@{
using obj_AType = xo::scm::AType::obj_AType;
using Copaque = xo::scm::AType::Copaque;
using Opaque = xo::scm::AType::Opaque;
///@}
/** @defgroup scm-type-darraytype-methods **/
///@{
// const methods
/** category for this type **/
static Metatype metatype(const DArrayType & self) noexcept;
/** true iff this type is equal to y **/
static bool is_equal_to(const DArrayType & self, const obj_AType & y);
/** true iff this is a subtype of y **/
static bool is_subtype_of(const DArrayType & self, const obj_AType & y);
// non-const methods
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -9,11 +9,14 @@ set(SELF_SRCS
Metatype.cpp
DAtomicType.cpp
DListType.cpp
DArrayType.cpp
IType_Any.cpp
IType_DAtomicType.cpp
IType_DListType.cpp
IType_DArrayType.cpp
IGCObject_DAtomicType.cpp
IGCObject_DListType.cpp
IGCObject_DArrayType.cpp
)
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})

92
src/type/DArrayType.cpp Normal file
View file

@ -0,0 +1,92 @@
/** @file DArrayType.cpp
*
* @author Roland Conybeare, Mar 2026
**/
#include "Type.hpp"
#include "ArrayType.hpp"
#include <xo/alloc2/Collector.hpp>
#include <xo/alloc2/Allocator.hpp>
#include <xo/facet/FacetRegistry.hpp>
namespace xo {
using xo::mm::AGCObject;
using xo::mm::AAllocator;
using xo::facet::FacetRegistry;
namespace scm {
DArrayType::DArrayType(obj<AType> elt) : elt_type_{elt} {}
DArrayType *
DArrayType::_make(obj<AAllocator> mm,
obj<AType> elt_type)
{
void * mem = mm.alloc_for<DArrayType>();
return new (mem) DArrayType(elt_type);
}
// ----- type facet -----
bool
DArrayType::is_equal_to(const obj<AType> & y_arg) const noexcept
{
Metatype y_mtype = y_arg.metatype();
if (y_mtype != Metatype::array())
return false;
auto y = obj<AType,DArrayType>::from(y_arg);
obj<AType> e = elt_type_;
return (e.is_equal_to(y->elt_type_));
}
bool
DArrayType::is_subtype_of(const obj<AType> & y_arg) const noexcept
{
Metatype y_mtype = y_arg.metatype();
if (y_mtype == Metatype::any())
return true;
if (y_mtype != Metatype::array())
return false;
auto y = obj<AType,DArrayType>::from(y_arg);
obj<AType> e = elt_type_;
return (e.is_subtype_of(y->elt_type_));
}
// ----- gcobject facet -----
std::size_t
DArrayType::shallow_size() const noexcept
{
return sizeof(*this);
}
DArrayType *
DArrayType::shallow_copy(obj<AAllocator> mm) const noexcept
{
return mm.std_copy_for(this);
}
std::size_t
DArrayType::forward_children(obj<ACollector> gc) noexcept
{
{
auto e = FacetRegistry::instance().variant<AGCObject,AType>(elt_type_);
gc.forward_inplace(e.iface(), (void **)&(e.data_));
}
return this->shallow_size();
}
}
}
/* end DArrayType.cpp */

View file

@ -0,0 +1,39 @@
/** @file IGCObject_DArrayType.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IGCObject_DArrayType.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IGCObject_DArrayType.json5]
**/
#include "array/IGCObject_DArrayType.hpp"
namespace xo {
namespace scm {
auto
IGCObject_DArrayType::shallow_size(const DArrayType & self) noexcept -> size_type
{
return self.shallow_size();
}
auto
IGCObject_DArrayType::shallow_copy(const DArrayType & self, obj<AAllocator> mm) noexcept -> Opaque
{
return self.shallow_copy(mm);
}
auto
IGCObject_DArrayType::forward_children(DArrayType & self, obj<ACollector> gc) noexcept -> size_type
{
return self.forward_children(gc);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IGCObject_DArrayType.cpp */

View file

@ -0,0 +1,40 @@
/** @file IType_DArrayType.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IType_DArrayType.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IType_DArrayType.json5]
**/
#include "array/IType_DArrayType.hpp"
namespace xo {
namespace scm {
auto
IType_DArrayType::metatype(const DArrayType & self) noexcept -> Metatype
{
return self.metatype();
}
auto
IType_DArrayType::is_equal_to(const DArrayType & self, const obj_AType & y) -> bool
{
return self.is_equal_to(y);
}
auto
IType_DArrayType::is_subtype_of(const DArrayType & self, const obj_AType & y) -> bool
{
return self.is_subtype_of(y);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IType_DArrayType.cpp */

View file

@ -7,6 +7,7 @@
#include <xo/type/AtomicType.hpp>
#include <xo/type/ListType.hpp>
#include <xo/type/ArrayType.hpp>
#include <xo/facet/FacetRegistry.hpp>
#include <xo/indentlog/scope.hpp>
@ -28,8 +29,12 @@ namespace xo {
FacetRegistry::register_impl<AType, DListType>();
FacetRegistry::register_impl<AGCObject, DListType>();
FacetRegistry::register_impl<AType, DArrayType>();
FacetRegistry::register_impl<AGCObject, DArrayType>();
log && log(xtag("DAtomicType.tseq", typeseq::id<DAtomicType>()));
log && log(xtag("DListType.tseq", typeseq::id<DListType>()));
log && log(xtag("DArrayType.tseq", typeseq::id<DArrayType>()));
return true;
}

View file

@ -7,6 +7,7 @@
#include "AtomicType.hpp"
#include "ListType.hpp"
#include "ArrayType.hpp"
#include <xo/alloc2/Collector.hpp>
#include <xo/facet/FacetRegistry.hpp>
#include <xo/indentlog/scope.hpp>
@ -27,6 +28,7 @@ namespace xo {
ok &= gc.install_type(impl_for<AGCObject, DAtomicType>());
ok &= gc.install_type(impl_for<AGCObject, DListType>());
ok &= gc.install_type(impl_for<AGCObject, DArrayType>());
return ok;
}

View file

@ -5,6 +5,7 @@ set(UTEST_SRCS
type_utest_main.cpp
DAtomicType.test.cpp
DListType.test.cpp
DArrayType.test.cpp
# DString.test.cpp
# StringOps.test.cpp
# X1Collector.test.cpp

70
utest/DArrayType.test.cpp Normal file
View file

@ -0,0 +1,70 @@
/** @file DArrayType.test.cpp
*
* @author Roland Conybeare, Mar 2026
**/
#include "init_type.hpp"
#include "TypeOps.hpp"
#include "ArrayType.hpp"
#include "ListType.hpp"
#include "AtomicType.hpp"
#include <xo/alloc2/Allocator.hpp>
#include <xo/alloc2/Arena.hpp>
#include <xo/facet/obj.hpp>
#include <catch2/catch.hpp>
namespace xo {
using xo::scm::TypeOps;
using xo::scm::AType;
using xo::scm::Metatype;
using xo::mm::AAllocator;
using xo::mm::DArena;
using xo::mm::ArenaConfig;
namespace ut {
static InitEvidence s_init = (InitSubsys<S_type_tag>::require());
TEST_CASE("DArrayType-make", "[type][DArrayType]")
{
ArenaConfig cfg { .name_ = "testarena",
.size_ = 4*1024 };
DArena arena = DArena::map(cfg);
auto alloc = obj<AAllocator,DArena>(&arena);
auto i64_type = TypeOps::atomic_type(alloc, Metatype::i64());
auto array_i64_type = TypeOps::array_type(alloc, i64_type);
REQUIRE(array_i64_type);
REQUIRE(array_i64_type.is_equal_to(array_i64_type));
auto bool_type = TypeOps::atomic_type(alloc, Metatype::t_bool());
auto array_bool_type = TypeOps::array_type(alloc, bool_type);
REQUIRE(array_bool_type);
REQUIRE(array_bool_type.is_equal_to(array_bool_type));
auto any_type = TypeOps::atomic_type(alloc, Metatype::any());
auto array_any_type = TypeOps::array_type(alloc, any_type);
REQUIRE(array_any_type);
REQUIRE(array_any_type.is_equal_to(array_any_type));
REQUIRE(array_bool_type.is_subtype_of(array_any_type));
REQUIRE(array_i64_type.is_subtype_of(array_any_type));
REQUIRE(!array_i64_type.is_subtype_of(array_bool_type));
REQUIRE(!array_any_type.is_subtype_of(array_bool_type));
REQUIRE(!array_bool_type.is_subtype_of(array_i64_type));
REQUIRE(!array_any_type.is_subtype_of(array_i64_type));
// array and list are unrelated
auto list_i64_type = TypeOps::list_type(alloc, i64_type);
REQUIRE(!array_i64_type.is_equal_to(list_i64_type));
REQUIRE(!array_i64_type.is_subtype_of(list_i64_type));
REQUIRE(!list_i64_type.is_subtype_of(array_i64_type));
}
}
}
/* end DArrayType.test.cpp */