/** * \file * 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 . */ #include #include #include #include #include "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 + sizeof(CCLASS)); * (const struct _CCLASS **)object = class; object += sizeof(CCLASS); 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 CCLASS class = _class; void * object = calloc(1, class->size + sizeof(CCLASS)); * (const struct _CCLASS **) object = class; object += sizeof(CCLASS); if (class->__jsonConst && json) { class->__jsonConst(object, json); } return object; } int _instanceOf(const CCLASS _class, void * _object) { const CCLASS * class = _object - sizeof(CCLASS); return (class && _class == *class); } void clear(void * _object) { const CCLASS * class = _object - sizeof(CCLASS); if (_object && *class && (*class)->__clear) { (*class)->__clear(_object); } } void delete(void * _object) { const CCLASS * class = (*(void**)_object) - sizeof(CCLASS); if (*(void**)_object && *class && (*class)->__destruct) { (*class)->__destruct(*(void**)_object); } free((void *)class); *(void**)_object = NULL; } void toJson(void * _object, struct json_object ** json) { const CCLASS * class = _object - sizeof(CCLASS); if (_object && *class && (*class)->__toJson) { (*class)->__toJson(_object, json); } } int isObject(void * _object) { const CCLASS * class = _object - sizeof(CCLASS); return (_object && (*class) && CCLASS_MAGIC == (*class)->magic); } // vim: set et ts=4 sw=4: