// SPDX-License-Identifier: MIT // Copyright (c) 2023 John Watts and the LuminaSensum contributors #ifndef OBJECT_H #define OBJECT_H #include "vm.h" #include <stdatomic.h> // Dispatchable object call struct object_call { const char *name; void (*handler)(VmState state, Object obj); }; // Object class shared between objects struct object_class { void (*cleanup)(Object obj); struct object_call *calls; // Points to array terminated by call with NULL name }; // Object structure used by all classes // Place this at the start of your private structure and cast accordingly // Make sure to compile with -fno-strict-aliasing struct object { struct object_class *class_data; atomic_int ref_count; }; // Adds a reference to an object void object_hold(Object obj); // Drops a reference to an object, possibly cleaning up the object // Sets objptr to NULL void object_drop(Object *objptr); // Calls a method on an object void dispatch_call(VmState state, Object obj, const char *name); #endif