ds: memove legacy object system
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
#ifndef BLUELIB_DSREF_H_
|
||||
#define BLUELIB_DSREF_H_
|
||||
|
||||
#include <blue/ds/type.h>
|
||||
|
||||
#define B_DSREF(p) ((b_dsref *)(p))
|
||||
|
||||
#define B_TYPEOF(object) ((struct b_dsref *)(object)->ob_type)
|
||||
#define B_TYPEID(object) (b_typeid(B_DSREF(object)))
|
||||
|
||||
#define B_RV(p) (b_make_rvalue(B_DSREF(p)))
|
||||
#define B_RVT(t, p) ((t *)(b_make_rvalue(B_DSREF(p))))
|
||||
|
||||
#define B_DSREF_IS(object, type) (B_TYPEID(object) == B_DSREF_TYPE_##type)
|
||||
|
||||
struct b_string;
|
||||
struct b_stream;
|
||||
|
||||
typedef enum b_comparison_result {
|
||||
B_LESS = -1,
|
||||
B_EQUAL = 0,
|
||||
B_GREATER = 1,
|
||||
} b_comparison_result_t;
|
||||
|
||||
typedef struct b_dsref {
|
||||
unsigned int ob_ref;
|
||||
const struct b_dsref_type *ob_type;
|
||||
} b_dsref;
|
||||
|
||||
BLUE_API b_dsref *b_make_rvalue(b_dsref *obj);
|
||||
|
||||
BLUE_API b_dsref *b_retain(b_dsref *obj);
|
||||
BLUE_API void b_release(b_dsref *obj);
|
||||
|
||||
BLUE_API void b_to_string(const b_dsref *obj, struct b_stream *out);
|
||||
BLUE_API b_dsref_type_id b_typeid(const b_dsref *obj);
|
||||
|
||||
BLUE_API b_comparison_result_t b_compare(const b_dsref *a, const b_dsref *b);
|
||||
|
||||
#endif
|
||||
@@ -1,54 +0,0 @@
|
||||
#ifndef BLUELIB_TYPE_H_
|
||||
#define BLUELIB_TYPE_H_
|
||||
|
||||
#include <blue/core/queue.h>
|
||||
#include <blue/core/status.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define B_NPOS ((size_t)INTPTR_MAX)
|
||||
|
||||
struct b_dsref;
|
||||
struct b_stream;
|
||||
|
||||
typedef uintptr_t b_dsref_type_id;
|
||||
|
||||
typedef enum b_fundamental_type_id {
|
||||
B_DSREF_TYPE_NONE = 0,
|
||||
B_DSREF_TYPE_ANY,
|
||||
B_DSREF_TYPE_LIST,
|
||||
B_DSREF_TYPE_ARRAY,
|
||||
B_DSREF_TYPE_BUFFER,
|
||||
B_DSREF_TYPE_DICT,
|
||||
B_DSREF_TYPE_ERROR,
|
||||
B_DSREF_TYPE_HASHMAP,
|
||||
B_DSREF_TYPE_NUMBER,
|
||||
B_DSREF_TYPE_STRING,
|
||||
B_DSREF_TYPE_TREE,
|
||||
B_DSREF_TYPE_UUID,
|
||||
B_DSREF_TYPE_PATH,
|
||||
B_DSREF_TYPE_FILE,
|
||||
B_DSREF_TYPE_DIRECTORY,
|
||||
B_DSREF_TYPE_DATETIME,
|
||||
} b_fundamental_type_id;
|
||||
|
||||
typedef enum b_dsref_type_flags {
|
||||
B_DSREF_FUNDAMENTAL = 0x01u,
|
||||
} b_dsref_type_flags;
|
||||
|
||||
typedef struct b_dsref_type {
|
||||
b_dsref_type_flags t_flags;
|
||||
char t_name[64];
|
||||
size_t t_instance_size;
|
||||
b_dsref_type_id t_id;
|
||||
b_queue_entry t_entry;
|
||||
void (*t_init)(struct b_dsref *);
|
||||
void (*t_release)(struct b_dsref *);
|
||||
void (*t_to_string)(const struct b_dsref *, struct b_stream *);
|
||||
} b_dsref_type;
|
||||
|
||||
BLUE_API b_status b_dsref_type_register(b_dsref_type *type);
|
||||
BLUE_API struct b_dsref *b_dsref_type_instantiate(const b_dsref_type *type);
|
||||
|
||||
#endif
|
||||
66
ds/object.c
66
ds/object.c
@@ -1,66 +0,0 @@
|
||||
#include "object.h"
|
||||
|
||||
#include <blue/core/stream.h>
|
||||
#include <blue/ds/object.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <blue/ds/type.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void b_dsref_init(struct b_dsref *obj, struct b_dsref_type *type)
|
||||
{
|
||||
obj->ob_ref = 1;
|
||||
obj->ob_type = type;
|
||||
}
|
||||
|
||||
struct b_dsref *b_make_rvalue(struct b_dsref *obj)
|
||||
{
|
||||
obj->ob_ref = 0;
|
||||
return obj;
|
||||
}
|
||||
|
||||
struct b_dsref *b_retain(struct b_dsref *obj)
|
||||
{
|
||||
obj->ob_ref++;
|
||||
return obj;
|
||||
}
|
||||
|
||||
void b_release(struct b_dsref *obj)
|
||||
{
|
||||
if (obj->ob_ref > 1) {
|
||||
obj->ob_ref--;
|
||||
return;
|
||||
}
|
||||
|
||||
obj->ob_ref = 0;
|
||||
|
||||
if (obj->ob_type && obj->ob_type->t_release) {
|
||||
obj->ob_type->t_release(obj);
|
||||
}
|
||||
|
||||
free(obj);
|
||||
}
|
||||
|
||||
void b_to_string(const struct b_dsref *obj, struct b_stream *out)
|
||||
{
|
||||
if (obj->ob_type->t_to_string) {
|
||||
obj->ob_type->t_to_string(obj, out);
|
||||
return;
|
||||
}
|
||||
|
||||
const char *name = "corelib::object";
|
||||
if (obj->ob_type) {
|
||||
name = obj->ob_type->t_name;
|
||||
}
|
||||
|
||||
b_stream_write_fmt(out, NULL, "<%s@%p>", name, obj);
|
||||
}
|
||||
|
||||
b_dsref_type_id b_typeid(const struct b_dsref *obj)
|
||||
{
|
||||
if (obj->ob_type->t_flags & B_DSREF_FUNDAMENTAL) {
|
||||
return obj->ob_type->t_id;
|
||||
}
|
||||
|
||||
return (b_dsref_type_id)obj->ob_type;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#ifndef _BLUELIB_DSREF_H_
|
||||
#define _BLUELIB_DSREF_H_
|
||||
|
||||
#include <blue/ds/object.h>
|
||||
|
||||
#endif
|
||||
29
ds/type.c
29
ds/type.c
@@ -1,29 +0,0 @@
|
||||
#include "object.h"
|
||||
|
||||
#include <blue/core/queue.h>
|
||||
#include <blue/ds/type.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct b_dsref *b_dsref_type_instantiate(const b_dsref_type *type)
|
||||
{
|
||||
if (!type || type->t_instance_size < sizeof(struct b_dsref)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct b_dsref *out = malloc(type->t_instance_size);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, type->t_instance_size);
|
||||
|
||||
out->ob_ref = 1;
|
||||
out->ob_type = type;
|
||||
|
||||
if (type->t_init) {
|
||||
type->t_init(out);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
Reference in New Issue
Block a user