Add a function to create a value reference wrapper.

This commit is contained in:
Danila Fedorin 2018-04-24 15:22:17 -07:00
parent ee76d39f38
commit 735e8715a8
2 changed files with 34 additions and 0 deletions

View File

@ -72,5 +72,13 @@ libab_result libab_instantiate_basetype(libab_basetype* to_instantiate,
* @return the result of the instantiation. * @return the result of the instantiation.
*/ */
libab_result libab_create_table(libab_ref* into, libab_ref* parent); libab_result libab_create_table(libab_ref* into, libab_ref* parent);
/**
* Allocates a new reference counted value with the given type and data.
* @param into the reference to store the allocated data into.
* @param data the type-specific data this value holds.
* @param type the type to give the value.
* @return the result of necessary allocations.
*/
libab_result libab_create_value(libab_ref* into, void* data, libab_ref* type);
#endif #endif

View File

@ -1,4 +1,5 @@
#include "util.h" #include "util.h"
#include "value.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
@ -148,3 +149,28 @@ libab_result libab_create_table(libab_ref* into, libab_ref* parent) {
} }
return result; return result;
} }
void _free_value(void* value) {
libab_value_free(value);
free(value);
}
libab_result libab_create_value(libab_ref* into, void* data, libab_ref* type) {
libab_value* value;
libab_result result = LIBAB_SUCCESS;
if((value = malloc(sizeof(*value)))) {
libab_value_init(value, data, type);
result = libab_ref_new(into, value, _free_value);
if(result != LIBAB_SUCCESS) {
_free_value(value);
}
} else {
result = LIBAB_MALLOC;
}
if(result != LIBAB_SUCCESS) {
libab_ref_null(into);
}
return result;
}