103 lines
3.4 KiB
C++
103 lines
3.4 KiB
C++
/** @file ObjectHandle.hpp
|
|
*
|
|
* @author Roland Conybeare, Sep 2026
|
|
**/
|
|
|
|
#pragma once
|
|
|
|
#include "AllocFlywheel.hpp"
|
|
#include "xo/facet/handlestore/ObjectSlot.hpp"
|
|
#include <xo/refcnt/Displayable.hpp>
|
|
|
|
namespace xo::facet {
|
|
|
|
/** @brief object handle for python bindings
|
|
*
|
|
* Python bindings will use an ObjectHandle as a proxy for
|
|
* a particular underlying XO object.
|
|
*
|
|
* In practice will inherit from this for each distinct interface.
|
|
*
|
|
* AllocFlywheel stores anonymized obj<ATop> handles.
|
|
* ObjectHandle recovers a specific obj<Iface,DRepr> pair
|
|
**/
|
|
class ObjectHandleBase {
|
|
public:
|
|
using FacetAppcx = xo::FacetAppcx;
|
|
using Indentlog2Appcx = xo::Indentlog2Appcx;
|
|
using impl_handle_type = AllocFlywheel::handle_type;
|
|
using handle_index_type = AllocFlywheel::handle_index_type;
|
|
|
|
public:
|
|
/** @defgroup objecthandle-ctors **/
|
|
///@{
|
|
ObjectHandleBase(rp<AllocFlywheel> mem,
|
|
std::pair<handle_index_type, impl_handle_type *> obj_info);
|
|
|
|
ObjectHandleBase(ObjectHandleBase && other) noexcept;
|
|
/** not copyable **/
|
|
ObjectHandleBase(const ObjectHandleBase &) = delete;
|
|
|
|
~ObjectHandleBase();
|
|
///@}
|
|
|
|
/** not assignable **/
|
|
ObjectHandleBase & operator=(const ObjectHandleBase &) = delete;
|
|
|
|
impl_handle_type * _impl_handle() const { return impl_handle_; }
|
|
|
|
//const AllocFlywheel & flywheel() const { return *memory_.get(); }
|
|
handle_index_type object_ix() const { return object_ix_; }
|
|
|
|
/** @defgroup objecthandle-witness evidence carried by a handle;
|
|
* chain of trust checked at compile time.
|
|
**/
|
|
///@{
|
|
FacetAppcx::CreationEvidence facetappcx_creation_evidence() const;
|
|
Indentlog2Appcx::CreationEvidence indentlog2appcx_creation_evidence() const;
|
|
///@}
|
|
|
|
private:
|
|
rp<AllocFlywheel> memory_;
|
|
/** identity of target object in @ref memory_ **/
|
|
handle_index_type object_ix_;
|
|
/** object handle **/
|
|
impl_handle_type * impl_handle_ = nullptr;;
|
|
};
|
|
|
|
/** Provide a proxy for an @c obj<AFacet,DRepr> instance,
|
|
* where the instance belongs to a particular AllocFlywheel.
|
|
**/
|
|
template <typename AFacet, typename DRepr>
|
|
class DObjectHandle : public ObjectHandleBase {
|
|
public:
|
|
using object_type = obj<AFacet, DRepr>;
|
|
|
|
public:
|
|
DObjectHandle(rp<AllocFlywheel> mem,
|
|
std::pair<handle_index_type, impl_handle_type *> obj_info)
|
|
: ObjectHandleBase(mem, obj_info) {}
|
|
|
|
/** move-only **/
|
|
DObjectHandle(DObjectHandle &&) noexcept = default;
|
|
|
|
/** Attach instance to flywheel @p mem to keep its backing memory alive.
|
|
*
|
|
* Require: @p x was allocated from flywheel @p mem.
|
|
**/
|
|
static DObjectHandle make_strong_ref(bp<AllocFlywheel> mem, obj<AFacet, DRepr> x) {
|
|
auto ref = mem->add_strong_ref(static_cast<const ATop *>(x.iface()),
|
|
x.opaque_data());
|
|
|
|
return DObjectHandle(mem.promote(), ref);
|
|
}
|
|
|
|
public:
|
|
/** recover the typed obj for this handle's target. **/
|
|
object_type _native() const {
|
|
return object_type(static_cast<DRepr *>(this->_impl_handle()->opaque_data()));
|
|
}
|
|
};
|
|
} /*namespace xo::facet*/
|
|
|
|
/* end ObjectHandle.hpp */
|