Newer
Older
Tardis / lang / object.h
// SPDX-License-Identifier: MIT
// Copyright (c) 2023 John Watts and the LuminaSensum contributors

#ifndef OBJECT_H
#define OBJECT_H

#include <stdatomic.h>

// Forward declare the class as we need it in object
struct object_class;

// 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;
};

// Dispatchable object call
struct object_call {
	const char *name;
	void (*handler)(
		struct object *obj, int arg_count, struct object **args);
};

// Object class shared between objects
struct object_class {
	void (*cleanup)(struct object *obj);
	struct object_call
		*calls; // Points to array terminated by call with NULL name
};

// Adds a reference to an object
void object_hold(struct object *obj);

// Drops a reference to an object, possibly cleaning up the object
// Sets objptr to NULL
void object_drop(struct object **objptr);

// Calls a method on an object
void dispatch_call(struct object *obj, const char *name, int arg_count,
	struct object **args);

#endif