cclass.c 1.22 KB
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <json/json.h>

#include "token/cclass.h"


void *
new(const void * _class, ...)
{
    const struct CCLASS * class = _class;
    void * object               = calloc(1, class->size);

    * (const struct CCLASS **) object = class;

    if (class->__construct) {
        va_list params;

        va_start(params, _class);
        class->__construct(object, &params);
        va_end(params);
    }

    return object;
}

void *
newFromJson(const void * _class, struct json_object * json)
{
    const struct CCLASS * class = _class;
    void * object               = calloc(1, class->size);

    * (const struct CCLASS **) object = class;

    if (class->__jsonConst && json) {
        class->__jsonConst(object, json);
    }

    return object;
}

void
delete(void * _object)
{
    const struct CCLASS ** class = _object;

    if (_object && *class && (*class)->__destruct) {
        (*class)->__destruct(_object);
    }

    free(_object);
}

struct json_object *
toJson(void * _object)
{
    const struct CCLASS ** class = _object;

    if (_object && *class && (*class)->__toJson) {
        return (*class)->__toJson(_object);
    }

    return NULL;
}

// vim: set et ts=4 sw=4: