CppInterOp
C++ Language Interoperability Layer
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
Cpp::Box Class Reference

#include "include/CppInterOp/Box.h"

Classes

struct  ObjectOps
 Operations vtable for a K_PtrOrObj payload. More...
 

Public Types

enum  Kind : int {
  K_Bool , K_Char_S , K_SChar , K_UChar ,
  K_Short , K_UShort , K_Int , K_UInt ,
  K_Long , K_ULong , K_LongLong , K_ULongLong ,
  K_Float , K_Double , K_LongDouble , K_Char_U ,
  K_Void , K_PtrOrObj , K_Unspecified
}
 

Public Member Functions

 Box ()=default
 
 Box (const Box &o) noexcept
 
Boxoperator= (const Box &o) noexcept
 
 Box (Box &&o) noexcept
 
Boxoperator= (Box &&o) noexcept
 
 ~Box () noexcept
 
Kind getKind () const noexcept
 
void * getType () const noexcept
 
template<class T >
unbox () const noexcept
 AOT-typed extraction.
 
void * getObjectPtr () const noexcept
 Object payload accessor; only valid for K_PtrOrObj.
 
template<class T >
convertTo () const noexcept
 Runtime convert across Kinds: dispatches on the actual Kind and returns the value reinterpreted (via static_cast) as T.
 
template<class V >
auto visit (V &&vis) const -> decltype(vis(int{}))
 Runtime-typed dispatch via visitor.
 

Static Public Member Functions

template<class T >
static Box Create (T x, void *type=nullptr) noexcept
 AOT-typed construction for fundamentals.
 
static Box AdoptObject (void *obj, const ObjectOps *ops, void *type) noexcept
 Object-payload construction.
 

Detailed Description

Definition at line 96 of file Box.h.

Member Enumeration Documentation

◆ Kind

enum Cpp::Box::Kind : int
Enumerator
K_Bool 
K_Char_S 
K_SChar 
K_UChar 
K_Short 
K_UShort 
K_Int 
K_UInt 
K_Long 
K_ULong 
K_LongLong 
K_ULongLong 
K_Float 
K_Double 
K_LongDouble 
K_Char_U 
K_Void 
K_PtrOrObj 
K_Unspecified 

Definition at line 99 of file Box.h.

Constructor & Destructor Documentation

◆ Box() [1/3]

Cpp::Box::Box ( )
default

◆ Box() [2/3]

Cpp::Box::Box ( const Box o)
inlinenoexcept

Definition at line 149 of file Box.h.

References K_PtrOrObj.

◆ Box() [3/3]

Cpp::Box::Box ( Box &&  o)
inlinenoexcept

Definition at line 167 of file Box.h.

References K_Unspecified.

◆ ~Box()

Cpp::Box::~Box ( )
inlinenoexcept

Definition at line 184 of file Box.h.

References K_PtrOrObj.

Referenced by operator=(), and operator=().

Member Function Documentation

◆ AdoptObject()

static Box Cpp::Box::AdoptObject ( void *  obj,
const ObjectOps ops,
void *  type 
)
inlinestaticnoexcept

Object-payload construction.

obj enters with refcount 1 (the producer's invariant); ~Box calls ops->release(obj) which on the last drop runs the payload's destructor. The ops table is const-static, defined in the TU that knows the concrete payload type.

Definition at line 225 of file Box.h.

References K_PtrOrObj.

Referenced by compat::MakeValueBox().

◆ convertTo()

template<class T >
T Cpp::Box::convertTo ( ) const
inlinenoexcept

Runtime convert across Kinds: dispatches on the actual Kind and returns the value reinterpreted (via static_cast) as T.

Mirrors clang::Value::convertTo<T>. Only valid for fundamental Kinds (K_Bool ... K_LongDouble); K_PtrOrObj / K_Void / K_Unspecified are caller-must-check-Kind cases.

Definition at line 247 of file Box.h.

References visit().

◆ Create()

template<class T >
static Box Cpp::Box::Create ( x,
void *  type = nullptr 
)
inlinestaticnoexcept

AOT-typed construction for fundamentals.

Sets Kind = KindOf<T>(), stores x. Optional QualType preserves typedef sugar (int8_t vs signed char) the Kind enum collapses.

Definition at line 212 of file Box.h.

◆ getKind()

Kind Cpp::Box::getKind ( ) const
inlinenoexcept

Definition at line 189 of file Box.h.

◆ getObjectPtr()

void * Cpp::Box::getObjectPtr ( ) const
inlinenoexcept

Object payload accessor; only valid for K_PtrOrObj.

Returns the raw pointer set at AdoptObject time. Layout is producer-defined – the producer that installed the ObjectOps knows how to extract the underlying object.

Definition at line 238 of file Box.h.

References K_PtrOrObj.

◆ getType()

void * Cpp::Box::getType ( ) const
inlinenoexcept

Definition at line 190 of file Box.h.

◆ operator=() [1/2]

Box & Cpp::Box::operator= ( Box &&  o)
inlinenoexcept

Definition at line 172 of file Box.h.

References K_Unspecified, and ~Box().

◆ operator=() [2/2]

Box & Cpp::Box::operator= ( const Box o)
inlinenoexcept

Definition at line 156 of file Box.h.

References K_PtrOrObj, and ~Box().

◆ unbox()

template<class T >
T Cpp::Box::unbox ( ) const
inlinenoexcept

AOT-typed extraction.

T must match the runtime Kind exactly (UB to read an inactive union member otherwise – e.g. calling unbox<int>() on a K_Long Box). For unknown-kind extraction use visit() or check getKind() against KindOf<T>() first. The assert is a no-op under NDEBUG so the AOT-fold path stays zero-overhead.

Definition at line 198 of file Box.h.

◆ visit()

template<class V >
auto Cpp::Box::visit ( V &&  vis) const -> decltype(vis(int{}))
inline

Runtime-typed dispatch via visitor.

Switch over Kind, call visitor with the typed T extracted from storage. K_PtrOrObj / K_Void / K_* non-fundamental kinds are not dispatched – caller checks Kind first.

Definition at line 257 of file Box.h.

Referenced by convertTo().


The documentation for this class was generated from the following file: