Import source to librtti
Summary: - Import initial source Test Plan: NA
This commit is contained in:
commit
c8c1749347
23 changed files with 969 additions and 0 deletions
81
src/create.c
Normal file
81
src/create.c
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "rtti.h"
|
||||
|
||||
static void* objAlloc(typeinfo_t* type, size_t sz)
|
||||
{
|
||||
size_t privateSize = SIZE_ROUNDUP(type->privateSize);
|
||||
size_t headerSize = OBJ_HEADER_SIZE;
|
||||
char* p = NULL;
|
||||
meta_obj_t* obj = NULL;
|
||||
size_t total = privateSize + headerSize + sz;
|
||||
|
||||
p = (char*)calloc(1, total);
|
||||
if (p == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
obj = (meta_obj_t*)(p + privateSize);
|
||||
obj->type = type;
|
||||
|
||||
return (void*)(p + privateSize + headerSize);
|
||||
}
|
||||
|
||||
var makeInstance(typeinfo_t* type, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_list ap1;
|
||||
var v = NULL;
|
||||
int rc = 0;
|
||||
|
||||
if ((type == NULL) || (type->init == NULL) || (type->dataSize == NULL)) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
va_start(ap, type);
|
||||
va_copy(ap1, ap);
|
||||
v = objAlloc(type, type->dataSize(ap1));
|
||||
va_end(ap1);
|
||||
|
||||
if (v != NULL) {
|
||||
rc = type->init(v, ap);
|
||||
}
|
||||
|
||||
va_end(ap);
|
||||
|
||||
if (rc < 0) {
|
||||
destroy(v);
|
||||
v = NULL;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
void destroy(var v)
|
||||
{
|
||||
typeinfo_t* type = NULL;
|
||||
meta_obj_t* obj = NULL;
|
||||
void* p = NULL;
|
||||
|
||||
if (v == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
obj = DATA_TO_OBJ(v);
|
||||
|
||||
type = obj->type;
|
||||
if (type == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (type->destroy != NULL) {
|
||||
type->destroy(v);
|
||||
}
|
||||
|
||||
p = OBJ_TO_PRIV(obj);
|
||||
free(p);
|
||||
|
||||
return;
|
||||
}
|
||||
74
src/iter.c
Normal file
74
src/iter.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "rtti.h"
|
||||
|
||||
int iter_make(iterator_t* it, var v)
|
||||
{
|
||||
iter_make_fn_t method = NULL;
|
||||
|
||||
if (it == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
RTTI_GET_METHOD(method, v, iter, make);
|
||||
if (method == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
memset(it, 0x0, sizeof(*it));
|
||||
it->v = v;
|
||||
|
||||
return method(it, v);
|
||||
}
|
||||
|
||||
int iter_next(iterator_t* it, void** data)
|
||||
{
|
||||
iter_next_fn_t method = NULL;
|
||||
|
||||
if ((it == NULL) || (data == NULL) || (it->v == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
RTTI_GET_METHOD(method, it->v, iter, next);
|
||||
if (method == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return method(it, data);
|
||||
}
|
||||
|
||||
int iter_reset(iterator_t* it)
|
||||
{
|
||||
iter_reset_fn_t method = NULL;
|
||||
|
||||
if ((it == NULL) || (it->v == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
RTTI_GET_METHOD(method, it->v, iter, reset);
|
||||
if (method == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return method(it);
|
||||
}
|
||||
|
||||
void iter_destroy(iterator_t* it)
|
||||
{
|
||||
iter_destroy_fn_t method = NULL;
|
||||
|
||||
if ((it == NULL) || (it->v == NULL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
RTTI_GET_METHOD(method, it->v, iter, destroy);
|
||||
if (method == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
method(it);
|
||||
|
||||
return;
|
||||
}
|
||||
30
src/lock.c
Normal file
30
src/lock.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "rtti.h"
|
||||
|
||||
int lock(var v, uint64_t option)
|
||||
{
|
||||
lock_fn_t method = NULL;
|
||||
|
||||
RTTI_GET_METHOD(method, v, lock, lock);
|
||||
if (method == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return method(v, option);
|
||||
}
|
||||
|
||||
void unlock(var v)
|
||||
{
|
||||
unlock_fn_t method = NULL;
|
||||
|
||||
RTTI_GET_METHOD(method, v, lock, unlock);
|
||||
if (method == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
method(v);
|
||||
|
||||
return;
|
||||
}
|
||||
42
src/meson.build
Normal file
42
src/meson.build
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
pkg = import('pkgconfig')
|
||||
|
||||
librtti_c_srcs=[
|
||||
'create.c',
|
||||
'lock.c',
|
||||
'iter.c',
|
||||
'ref.c',
|
||||
'str.c',
|
||||
]
|
||||
|
||||
|
||||
librtti_srcs= librtti_c_srcs
|
||||
|
||||
incdir = include_directories('../include', '.')
|
||||
|
||||
rtti_top_headers = [
|
||||
'../include/rtti.h',
|
||||
]
|
||||
|
||||
rtti_headers = [
|
||||
'../include/rtti/typeinfo.h',
|
||||
'../include/rtti/utils.h',
|
||||
]
|
||||
|
||||
librtti_deps = [
|
||||
]
|
||||
|
||||
librtti = static_library('librtti',
|
||||
librtti_srcs,
|
||||
include_directories: incdir,
|
||||
dependencies: librtti_deps,
|
||||
install: true)
|
||||
|
||||
install_headers(rtti_top_headers, install_dir: 'include')
|
||||
install_headers(rtti_headers, install_dir: 'include/rtti')
|
||||
|
||||
dep_librtti = declare_dependency(link_with: librtti)
|
||||
|
||||
pkg.generate(librtti,
|
||||
name: 'librtti',
|
||||
description: 'runtime type information library for c',
|
||||
version: '0.0.1')
|
||||
29
src/ref.c
Normal file
29
src/ref.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "rtti.h"
|
||||
|
||||
int32_t reference(var v)
|
||||
{
|
||||
reference_fn_t method = NULL;
|
||||
|
||||
RTTI_GET_METHOD(method, v, reference, reference);
|
||||
if (method == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return method(v);
|
||||
}
|
||||
|
||||
void dereference(var v)
|
||||
{
|
||||
dereference_fn_t method = NULL;
|
||||
|
||||
RTTI_GET_METHOD(method,v, reference, dereference);
|
||||
if (method != NULL) {
|
||||
method(v);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
23
src/str.c
Normal file
23
src/str.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "rtti.h"
|
||||
|
||||
char* toString(var v, ...)
|
||||
{
|
||||
to_string_fn_t method = NULL;
|
||||
va_list ap;
|
||||
char* s = NULL;
|
||||
|
||||
RTTI_GET_METHOD(method, v, toString, toString);
|
||||
if (method == NULL) {
|
||||
errno = -EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
va_start(ap, v);
|
||||
s = method(v, ap);
|
||||
va_end(ap);
|
||||
|
||||
return s;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue