Loading...
Searching...
No Matches
by::binder Class Reference

Generalized binding class for managing instance lifecycles. More...

#include <binder.hpp>

Inheritance diagram for by::binder:
by::typeProvidable by::tbindable< instance > by::tweak< T, strTactic > by::tweak< by::stela, strTactic > by::tweak< T, TACTIC > by::tstr< by::stela > by::tstr< T, TACTIC >

Public Member Functions

 binder (const type &type, bindTacticable &tactic)
 Constructs a binder with a specific type and binding tactic.
 
 binder (const me &rhs)
 
instanceoperator-> ()
 Dereference operator to access bound instance.
 
const instanceoperator-> () const BY_CONST_FUNC(operator->()) instance &operator*()
 
const instanceoperator* () const BY_CONST_FUNC(operator*()) me &operator
 Assigns another binder's bound instance to this binder.
 
id getItsId () const
 Retrieves the unique ID of the instance currently bound by this binder.
 
nbool isBind () const override
 
void rel () override
 
nbool canBind (const type &it) const override
 Checks if given type can be bound to this binder.
 
nbool bind (const instance &it) override
 Binds instance to this binder with reference counting.
 
instanceget ()
 
const instanceget () const BY_CONST_FUNC(get()) template< typename E > E *get()
 
template<typename E >
const Eget () const BY_CONST_FUNC(get< E >()) const type &getType() const override
 
voidcast (const type &to) override
 
nbool canBind (const T &it) const
 
nbool canBind (const T *it) const BY_SIDE_FUNC(canBind)
 
virtual nbool canBind (const type &it) const=0
 
nbool canBind (const type *it) const BY_SIDE_FUNC(canBind)
 
virtual nbool bind (const T &it)
 
nbool bind (const T *it)
 Bind to instance pointer.
 
template<typename T >
Tcast ()
 
template<typename T >
const Tcast () const BY_CONST_FUNC(cast< T >()) virtual void *cast(const type &to)
 Safe cast to target type using type hierarchy information.
 
const voidcast (const type &to) const BY_CONST_FUNC(cast(to)) void *cast(const type *it) BY_SIDE_FUNC(cast)
 
- Public Member Functions inherited from by::typeProvidable
nbool operator== (const me &rhs) const
 
nbool operator!= (const me &rhs) const
 
virtual const typegetType () const =0
 
nbool isSub (const type &it) const
 
nbool isSub (const type *it) const BY_SIDE_FUNC(isSub)
 
nbool isSub (const me &it) const
 
nbool isSub (const me *it) const BY_SIDE_FUNC(isSub)
 
nbool isSuper (const type &it) const
 
nbool isSuper (const type *it) const BY_SIDE_FUNC(isSuper)
 
nbool isSuper (const me &it) const
 
nbool isSuper (const me *it) const BY_SIDE_FUNC(isSuper)
 
template<typename T >
nint isSub () const
 
template<typename T >
nint isSuper () const
 
template<typename T >
Tcast ()
 
template<typename T >
const Tcast () const BY_CONST_FUNC(cast< T >()) virtual void *cast(const type &to)
 Safe cast to target type using type hierarchy information.
 
const voidcast (const type &to) const BY_CONST_FUNC(cast(to)) void *cast(const type *it) BY_SIDE_FUNC(cast)
 
- Public Member Functions inherited from by::tbindable< instance >
 operator nbool () const
 
nbool bind (const instance *it)
 Bind to instance pointer.
 
nbool canBind (const instance &it) const
 
nbool canBind (const instance *it) const BY_SIDE_FUNC(canBind)
 
nbool canBind (const type *it) const BY_SIDE_FUNC(canBind)
 

Protected Member Functions

nbool _assign (const binder &rhs)
 Assigns binder from another binder.
 
nbool _onSame (const typeProvidable &rhs) const override
 Checks if two typeProvidable objects are same instance.
 
life_getBindTag () const
 Retrieves life object associated with bound instance.
 

Protected Attributes

id _itsId
 
const type_type
 
bindTacticable_tactic
 

Friends

struct ::binderTest
 

Detailed Description

Generalized binding class for managing instance lifecycles.

Can bind any object inheriting from instance. Uses reference counting to properly destroy objects at appropriate times. Similar to std::weak_ptr and std::shared_ptr, with tweak handling weak pointers and tstr handling strong pointers.

Usage

Basic usage with bind(), isBind(), and get():

class A : public instance {}; // Inherits instance, so bindable
A* a = new A();
{
strBinder.bind(a);
// a's life count becomes 1
strBinder.isBind(); // true
a == strBinder.get(); // true
} // strBinder destroys, life count becomes 0, a automatically destroyed
*a; // Error: using destroyed object
Base class for all managed instances in memlite system.
Definition instance.hpp:41
Rich logging support with polymorphic type conversion.
Definition richLog.hpp:34

More concise real-world usage:

class shell : public instance {
public:
int age;
};
tstr<shell> ptr(new shell()); // Create and bind simultaneously
ptr->age = 57; // Supports operator->
tweak<shell> weak = ptr; // Binders can be compatible
callShell(*weak); // Supports operator* too
return ptr; // Returns by value, count maintained
// shell object created with new won't be destroyed
}

Why not use shared_ptr?

Several advantages over shared_ptr:

  1. Reference counting block attached to instance itself
    • shared_ptr creates a "Control block" on the heap for reference counting. This makes the following dangerous:
      Foo* raw = new Foo();
      shared_ptr<Foo> foo2(raw); // Separate control blocks -> double delete
    • memlite uses life class for reference counting, assigned per instance by watcher. No double deletion problem.
  2. Provides ADT (Abstract Data Type)
    • tstr and tweak share same binder base, enabling generic logic:
      void me::rel(binder& me) { // Works for both tstr and tweak
      WHEN(!me.isBind()) .ret();
      life* l = me._getBindTag();
      if(l) l->_onStrong(-1);
      }
      Generalized binding class for managing instance lifecycles.
      Definition binder.hpp:108
      Binding tag for instance reference tracking and lifecycle management.
      Definition life.hpp:21
  3. Dynamic type checking
    • binder is ADT and not even a class template. bind() parameter is instance type. tstr allows bind(new B()); without compile error. bind() uses meta module for dynamic type checking, binding only when types match.
Remarks
Abstract nature binder is abstract, so cannot create objects. Only meaningful for generic logic using already created tstr or tweak binders.

Custom memory pool

Ultimate goal of memlite is lightweight C++ memory management for byeol runtime environment. Requires GC and additional memory management, implying custom memory pool and instance lifecycle management. All instance allocation starts with instancer.

Performance improvements

shared_ptr's algorithm stores reference counting info on heap. Using faster custom memory pool instead of heap and optimizing binding speed provides performance gains. Binding is one of the hotspots consuming most performance in byeol.

Additional information

shared_ptr creates objects on heap for reference counting. memlite uses watcher to lend empty life objects from pre-allocated memory, using them as reference counting space for instances. If GC features are added later, instances may require additional lifecycle information. Unlike shared_ptr, managing each instance's lifecycle information internally allows appropriate handling of such requirements.

Constructor & Destructor Documentation

◆ binder()

by::binder::binder ( const type & type,
bindTacticable & tactic )

Constructs a binder with a specific type and binding tactic.

Parameters
typeThe type of the instance this binder will manage.
tacticThe binding tactic (e.g., strong or weak) to use for reference counting.

Member Function Documentation

◆ _assign()

nbool by::binder::_assign ( const binder & rhs)
protected

Assigns binder from another binder.

Returns
true on success, false on failure
Note
Handles reference counting via tactic when copying bindings

◆ _getBindTag()

life * by::binder::_getBindTag ( ) const
protected

Retrieves life object associated with bound instance.

Returns
Pointer to life object, or nullptr if not bound
Note
Internal accessor for reference counting operations

◆ _onSame()

nbool by::binder::_onSame ( const typeProvidable & rhs) const
overrideprotected

Checks if two typeProvidable objects are same instance.

Returns
true if same object, false otherwise
Note
Overrides typeProvidable for binder-specific equality

◆ bind() [1/2]

nbool by::binder::bind ( const instance & it)
overridevirtual

Binds instance to this binder with reference counting.

Returns
true on success, false if type mismatch or binding fails
Note
Increases reference count. Type must match via canBind() check.

Reimplemented from by::tbindable< instance >.

◆ bind() [2/2]

nbool by::tbindable< T >::bind ( const T * it)

Bind to instance pointer.

binder will loose previous binding instance whether given parameter it isn't nullptr or not.

◆ canBind() [1/2]

nbool by::binder::canBind ( const type & it) const
overridevirtual

Checks if given type can be bound to this binder.

Returns
true if type matches or is subtype of binder's type, false otherwise
Note
Uses meta module for dynamic type checking

Implements by::tbindable< instance >.

◆ canBind() [2/2]

virtual nbool by::tbindable< T >::canBind ( const type & it) const
virtual

◆ cast()

template<typename T >
const T * by::typeProvidable::cast ( ) const &

Safe cast to target type using type hierarchy information.

Returns
Pointer to object if cast is valid, nullptr otherwise
Note
Uses O(1) tier algorithm instead of slow dynamic_cast

◆ getItsId()

id by::binder::getItsId ( ) const

Retrieves the unique ID of the instance currently bound by this binder.

Returns
The ID of the bound instance. Returns an invalid ID if no instance is bound.

◆ isBind()

nbool by::binder::isBind ( ) const
overridevirtual

◆ operator*()

const instance & by::binder::operator* ( ) const &

Assigns another binder's bound instance to this binder.

This operator handles the transfer of binding, ensuring proper reference counting adjustments for both the source and target binders.

Parameters
rhsThe binder to assign from.
Returns
A reference to this binder.

◆ operator->()

instance * by::binder::operator-> ( )

Dereference operator to access bound instance.

This follows the same policy as tmay and stl. that is, if the binder does not bind any instances and tries to dereference them with get() or operator*(), it will behave as UB. this is likely to crash.

◆ rel()

void by::binder::rel ( )
overridevirtual

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