You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.5 KiB
114 lines
2.5 KiB
/**
|
|
* cclass.c: basic "class-like" handling of code and data structures
|
|
* Copyright (C) 2011 Georg Hopp
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <json/json.h>
|
|
|
|
#include "token/cclass.h"
|
|
|
|
#undef __construct
|
|
#undef __clear
|
|
#undef __destruct
|
|
#undef __jsonConst
|
|
#undef __toJson
|
|
|
|
void *
|
|
_new(const _CCLASS _class, ...)
|
|
{
|
|
const _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, ¶ms);
|
|
va_end(params);
|
|
}
|
|
|
|
return object;
|
|
}
|
|
|
|
void *
|
|
_newFromJson(const _CCLASS _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
|
|
clear(void * _object)
|
|
{
|
|
const struct CCLASS ** class = _object;
|
|
|
|
if (_object && *class && (*class)->__clear) {
|
|
(*class)->__clear(_object);
|
|
}
|
|
}
|
|
|
|
void
|
|
delete(void * _object)
|
|
{
|
|
const struct CCLASS ** class = *(void**)_object;
|
|
|
|
if (*(void**)_object && *class && (*class)->__destruct) {
|
|
(*class)->__destruct(*(void**)_object);
|
|
}
|
|
|
|
free(*(void**)_object);
|
|
*(void**)_object = NULL;
|
|
}
|
|
|
|
void
|
|
toJson(void * _object, struct json_object ** json)
|
|
{
|
|
const struct CCLASS ** class = _object;
|
|
|
|
if (_object && *class && (*class)->__toJson) {
|
|
(*class)->__toJson(_object, json);
|
|
}
|
|
}
|
|
|
|
int
|
|
isObject(void * _object)
|
|
{
|
|
const struct CCLASS ** class = _object;
|
|
|
|
return (_object && (*class) && CCLASS_MAGIC == (*class)->magic);
|
|
}
|
|
|
|
int
|
|
_instanceOf(const _CCLASS _class, void * _object)
|
|
{
|
|
const struct CCLASS ** class = _object;
|
|
|
|
return (class && _class == *class);
|
|
}
|
|
|
|
// vim: set et ts=4 sw=4:
|