xo-object2 xo-gc xo-facet: codegen updates + IGCObject_DString

This commit is contained in:
Roland Conybeare 2026-01-14 14:37:44 -05:00
commit 78e7548e53
9 changed files with 240 additions and 1 deletions

View file

@ -64,6 +64,18 @@ xo_add_genfacetimpl(
OUTPUT_CPP_DIR src/object2
)
# note: manual target; generated code committed to git
xo_add_genfacetimpl(
TARGET xo-object2-facetimpl-gcobject-string
FACET_PKG xo_gc
FACET GCObject
REPR String
INPUT idl/IGCObject_DString.json5
OUTPUT_HPP_DIR include/xo/object2
OUTPUT_IMPL_SUBDIR string
OUTPUT_CPP_DIR src/object2
)
# ----------------------------------------------------------------
# must complete definition of expression lib before configuring examples

View file

@ -0,0 +1,14 @@
{
mode: "implementation",
includes: [ "<xo/gc/GCObject.hpp>",
"<xo/alloc2/Allocator.hpp>"
],
local_types: [ ],
namespace1: "xo",
namespace2: "scm",
facet_idl: "idl/GCObject.json5",
brief: "provide AGCObject interface for DString",
using_doxygen: true,
repr: "DString",
doc: [ "implement AGCObject for DString" ],
}

View file

@ -6,7 +6,9 @@
#pragma once
#include <xo/alloc2/Allocator.hpp>
#include <xo/gc/Collector.hpp>
#include <xo/facet/obj.hpp>
#include <xo/indentlog/print/ppindentinfo.hpp>
#include <string_view>
#include <cstdint>
@ -38,11 +40,22 @@ namespace xo {
using const_iterator = const char *;
/** xo allocator **/
using AAllocator = xo::mm::AAllocator;
/** garbage collector **/
using ACollector = xo::mm::ACollector;
/** ppindentinfo for APrintable **/
using ppindentinfo = xo::print::ppindentinfo;
///@}
/** @defgroup dstring-ctors constructors **/
///@{
/** default ctor **/
DString() = default;
/** not simply copyable, because of flexible array.
* Need allocator
**/
DString(const DString &) = delete;
/** create empty string with space for @cap chars
* (including null terminator).
* Use memory from allocator @p mm
@ -56,6 +69,17 @@ namespace xo {
static DString * from_cstr(obj<AAllocator> mm,
const char * cstr);
/** clone existing string **/
static DString * clone(obj<AAllocator> mm,
const DString * src);
#ifdef NOT_YET
/** **/
static DString * concat(obj<AAllocator> mm,
DString * s1,
DString * s2);
#endif
///@}
/** @defgroup dstring-access access methods **/
///@{
@ -141,6 +165,24 @@ namespace xo {
operator const char * () const noexcept { return &(chars_[0]); }
///@}
/** @defgroup dstring-printable-methods printable facet methods **/
///@{
bool pretty(const ppindentinfo & ppii) const;
///@}
/** @defgroup dstring-gcobject-methods gcobject facet methods **/
///@{
size_type shallow_size() const noexcept;
/** clone string, using memory from allocator @p mm **/
DString * shallow_copy(obj<AAllocator> mm) const noexcept;
/** fixup child pointers (trivial for DString, no children) **/
size_type forward_children(obj<ACollector> gc) noexcept;
///@}
private:
/** @defgroup dstring-instance-variables instance variables **/

View file

@ -0,0 +1,66 @@
/** @file IGCObject_DString.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IGCObject_DString.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IGCObject_DString.json5]
**/
#pragma once
#include <xo/gc/GCObject.hpp>
#include <xo/alloc2/Allocator.hpp>
#include "DString.hpp"
namespace xo { namespace scm { class IGCObject_DString; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::mm::AGCObject,
xo::scm::DString>
{
using ImplType = xo::mm::IGCObject_Xfer
<xo::scm::DString,
xo::scm::IGCObject_DString>;
};
}
}
namespace xo {
namespace scm {
/** @class IGCObject_DString
**/
class IGCObject_DString {
public:
/** @defgroup scm-gcobject-dstring-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-dstring-methods **/
///@{
// const methods
/** memory consumption for this instance **/
static size_type shallow_size(const DString & self) noexcept;
/** copy instance using allocator **/
static Opaque shallow_copy(const DString & self, obj<AAllocator> mm) noexcept;
// non-const methods
/** during GC: forward immdiate children **/
static size_type forward_children(DString & self, obj<ACollector> gc) noexcept;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -5,6 +5,7 @@ set(SELF_SRCS
IGCObject_DFloat.cpp
IGCObject_DInteger.cpp
IGCObject_DList.cpp
IGCObject_DString.cpp
ISequence_Any.cpp
ISequence_DList.cpp
IPrintable_DList.cpp

View file

@ -52,6 +52,22 @@ namespace xo {
return result;
}
DString *
DString::clone(obj<AAllocator> mm, const DString * src)
{
size_type cap = src->capacity_;
void * mem = mm.alloc(typeseq::id<DString>(),
sizeof(DString) + cap);
DString * result = new (mem) DString();
result->capacity_ = cap;
result->size_ = src->size_;
std::memcpy(result->chars_, src->chars_, cap);
return result;
}
DString &
DString::assign(const DString & other)
{
@ -71,6 +87,32 @@ namespace xo {
return this->size_;
}
auto
DString::shallow_size() const noexcept -> size_type
{
return sizeof(DString) + capacity_;
}
DString *
DString::shallow_copy(obj<AAllocator> mm) const noexcept
{
DString * copy = (DString *)mm.alloc_copy((std::byte *)this);
if (copy) {
copy->capacity_ = capacity_;
copy->size_ = size_;
::memcpy(copy->chars_, chars_, capacity_);
}
return copy;
}
auto
DString::forward_children(obj<ACollector>) noexcept -> size_type
{
return shallow_size();
}
} /*namespace scm*/
} /*namespace xo*/

View file

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

View file

@ -4,9 +4,11 @@
**/
#include "object2_register_facets.hpp"
#include <xo/object2/IGCObject_DList.hpp>
#include <xo/object2/IGCObject_DFloat.hpp>
#include <xo/object2/IGCObject_DInteger.hpp>
#include <xo/object2/IGCObject_DString.hpp>
#include <xo/object2/IPrintable_DList.hpp>
#include <xo/object2/IPrintable_DFloat.hpp>
@ -23,6 +25,7 @@ namespace xo {
using xo::mm::AGCObject;
using xo::scm::DList;
using xo::scm::DFloat;
using xo::scm::DString;
using xo::facet::FacetRegistry;
using xo::facet::typeseq;
@ -41,9 +44,12 @@ namespace xo {
FacetRegistry::register_impl<AGCObject, DInteger>();
FacetRegistry::register_impl<APrintable, DInteger>();
FacetRegistry::register_impl<AGCObject, DString>();
log && log(xtag("DList.tseq", typeseq::id<DList>()));
log && log(xtag("DFloat.tseq", typeseq::id<DFloat>()));
log && log(xtag("DInteger.tseq", typeseq::id<DInteger>()));
log && log(xtag("DString.tseq", typeseq::id<DString>()));
log && log(xtag("AAllocator.tseq", typeseq::id<AAllocator>()));
log && log(xtag("APrintable.tseq", typeseq::id<APrintable>()));

View file

@ -243,6 +243,23 @@ namespace xo {
REQUIRE(std::strcmp(s->chars(), "HELLO") == 0);
}
TEST_CASE("DString-clone", "[object2][DString]")
{
ArenaConfig cfg { .name_ = "testarena",
.size_ = 4*1024 };
DArena arena = DArena::map(cfg);
auto alloc = with_facet<AAllocator>::mkobj(&arena);
DString * src = DString::from_cstr(alloc, "hello world");
DString * copy = DString::clone(alloc, src);
REQUIRE(copy != nullptr);
REQUIRE(copy != src);
REQUIRE(copy->size() == src->size());
REQUIRE(copy->capacity() == src->capacity());
REQUIRE(std::strcmp(copy->chars(), src->chars()) == 0);
}
} /*namespace ut*/
} /*namespace xo*/