Skip to content

Commit

Permalink
Added support for metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Nilsendahl committed Oct 17, 2022
1 parent 2150862 commit b5617d6
Show file tree
Hide file tree
Showing 11 changed files with 735 additions and 139 deletions.
198 changes: 105 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ Type-libs in text-format follows this format.

"comment": "my_enum is an externally declared enumeration and have 7 values",,

// "metadata" has a list of dl instances which are attached to the enumeration
"metadata": [ { "my_type" : { "member": 1 } } ]

// "values" are the actual values of the enum-type as a dict.
"values" : {
// ... a value can be specified as a simple integer ...
Expand All @@ -163,6 +166,9 @@ Type-libs in text-format follows this format.
"aliases" : ["apa", "kossa"],

"comment": "This enumerator has aliases",

// "metadata" has a list of dl instances which are attached to the enumerator
"metadata": [ { "my_type" : { "member": 1 } } ]
}
}
}
Expand Down Expand Up @@ -194,6 +200,9 @@ Type-libs in text-format follows this format.
// If not set, no comment will be written to .h
"comment" : "this type is a nice type!",

// "metadata" has a list of dl instances which are attached to the type
"metadata": [ { "my_type" : { "member": 1 } } ],

// "members" is a list of all members of this type.
"members" : [
// one {} per member and the members will be ordered as they are in this list.
Expand All @@ -220,6 +229,9 @@ Type-libs in text-format follows this format.

// "const" adds the const type modifier to this member in the generated header file
"const" : true,

// "metadata" has a list of dl instances which are attached to the member
"metadata": [ { "my_type" : { "member": 1 } } ]
}
]
}
Expand Down Expand Up @@ -333,16 +345,16 @@ structs are defined in a typelibrary like this:

```json
{
"types" : {
"data_t" : {
"members" : [
{ "name" : "id", "type" : "string" },
{ "name" : "x", "type" : "fp32" },
{ "name" : "y", "type" : "fp32" },
{ "name" : "array", "type" : "uint32[]" }
]
}
}
"types" : {
"data_t" : {
"members" : [
{ "name" : "id", "type" : "string" },
{ "name" : "x", "type" : "fp32" },
{ "name" : "y", "type" : "fp32" },
{ "name" : "array", "type" : "uint32[]" }
]
}
}
}
```

Expand All @@ -359,22 +371,22 @@ a `tld` file called example_tld.bin. `dltlc` must be run twice, like so.

dl_ctx_t create_dl_ctx()
{
// create and load context
dl_ctx_t dl_ctx;
dl_create_params_t create_params;
DL_CREATE_PARAMS_SET_DEFAULT( create_params );
// create and load context
dl_ctx_t dl_ctx;
dl_create_params_t create_params;
DL_CREATE_PARAMS_SET_DEFAULT( create_params );

dl_context_create( &dl_ctx, &create_params );
dl_context_create( &dl_ctx, &create_params );

// load typelibrary
unsigned char* lib_data = 0x0;
unsigned int lib_data_size = 0;
// load typelibrary
unsigned char* lib_data = 0x0;
unsigned int lib_data_size = 0;

read_from_file( "example_tld.bin", &lib_data, &lib_data_size );
read_from_file( "example_tld.bin", &lib_data, &lib_data_size );

dl_context_load_type_library( dl_ctx, lib_data, lib_data_size );
dl_context_load_type_library( dl_ctx, lib_data, lib_data_size );

return dl_ctx;
return dl_ctx;
}
```

Expand All @@ -386,21 +398,21 @@ dl_ctx_t create_dl_ctx()

void store_me( dl_ctx_t dl_ctx )
{
uint16 arr[] = { 1, 2, 3, 4, 5, 6 };

data_t instance_data;
instance_data.id = "Data Identifier.";
instance_data.x = 10.0f;
instance_data.y = 15.0f;
instance_data.array.data = arr;
instance_data.array.count = 6;

dl_util_store_to_file( dl_ctx,
data_t::TYPE_ID, // type identifier for example-type
"path/to/store/to.file", // file to store to
DL_UTIL_FILE_TYPE_BINARY, // store as binary file
DL_ENDIAN_HOST, // store with endian of this system
sizeof(void*), // store with pointer-size of this system
uint16 arr[] = { 1, 2, 3, 4, 5, 6 };

data_t instance_data;
instance_data.id = "Data Identifier.";
instance_data.x = 10.0f;
instance_data.y = 15.0f;
instance_data.array.data = arr;
instance_data.array.count = 6;

dl_util_store_to_file( dl_ctx,
data_t::TYPE_ID, // type identifier for example-type
"path/to/store/to.file", // file to store to
DL_UTIL_FILE_TYPE_BINARY, // store as binary file
DL_ENDIAN_HOST, // store with endian of this system
sizeof(void*), // store with pointer-size of this system
&instance_data ); // instance to store
}
```
Expand All @@ -413,33 +425,33 @@ void store_me( dl_ctx_t dl_ctx )
void load_me( dl_ctx_t dl_ctx )
{
data_t* instance_data;
dl_util_load_from_file( dl_ctx,
data_t::TYPE_ID, // type identifier for example-type
"path/to/read/from.file", // file to read
DL_UTIL_FILE_TYPE_AUTO, // autodetect if file is binary or text
(void**)&instance_data, // instance will be returned here
0x0 );
printf( "instance_data->id = %s\n", instance_data->id );
printf( "instance_data->x = %f\n", instance_data->x );
printf( "instance_data->x = %f\n", instance_data->y );
for( unsigned int i = 0; i < instance_data->array.count; ++i )
printf( "instance_data->array[%u] = %u\n", i, instance_data->array.data[i] );
// Prints:
// instance_data->id = Data Identifier
// instance_data->x = 10.000000
// instance_data->y = 15.000000
// instance_data->array[%u] = 1
// instance_data->array[%u] = 2
// instance_data->array[%u] = 3
// instance_data->array[%u] = 4
// instance_data->array[%u] = 5
// instance_data->array[%u] = 6
free( instance_data ); // by default memory for instance_data will be allocated by malloc
data_t* instance_data;
dl_util_load_from_file( dl_ctx,
data_t::TYPE_ID, // type identifier for example-type
"path/to/read/from.file", // file to read
DL_UTIL_FILE_TYPE_AUTO, // autodetect if file is binary or text
(void**)&instance_data, // instance will be returned here
0x0 );
printf( "instance_data->id = %s\n", instance_data->id );
printf( "instance_data->x = %f\n", instance_data->x );
printf( "instance_data->x = %f\n", instance_data->y );
for( unsigned int i = 0; i < instance_data->array.count; ++i )
printf( "instance_data->array[%u] = %u\n", i, instance_data->array.data[i] );
// Prints:
// instance_data->id = Data Identifier
// instance_data->x = 10.000000
// instance_data->y = 15.000000
// instance_data->array[%u] = 1
// instance_data->array[%u] = 2
// instance_data->array[%u] = 3
// instance_data->array[%u] = 4
// instance_data->array[%u] = 5
// instance_data->array[%u] = 6
free( instance_data ); // by default memory for instance_data will be allocated by malloc
}
```

Expand Down Expand Up @@ -500,9 +512,9 @@ namespace mmh3 {

static inline my_murmur(void* buffer, unsigned int len)
{
uint32_t res;
mmh3::MurmurHash3_x86_32( buffer, len, 1234, &res );
return res;
uint32_t res;
mmh3::MurmurHash3_x86_32( buffer, len, 1234, &res );
return res;
}

#define DL_HASH_BUFFER(buf, len) my_murmur(buf, len)
Expand All @@ -517,41 +529,41 @@ static inline my_murmur(void* buffer, unsigned int len)
Bam is officially supported by the original author of Data Library. If you want to build the library with bam, as is done during development and on autobuilders, these are the steps needed.
Run script/bootstrap.sh or script/bootstrap.bat depending on your platform, this will only have to be done once.
- these scripts will expect there to be a compiler and git in your PATH ( 'gcc' or 'clang' on 'unix', 'msvc' on windows )
- after this step there should be a local/bam/bam(.exe)
- these scripts will expect there to be a compiler and git in your PATH ( 'gcc' or 'clang' on 'unix', 'msvc' on windows )
- after this step there should be a local/bam/bam(.exe)
Run `local/bam/bam platform=<platform> config=<config> compiler=<compiler> -r sc`
- this will build all target for the specified platform and configuration into `local/<platform>/<compiler>/<config>`
- platforms:
* linux_x86 - 32bit linux x86 build.
* linux_x86_64 - 64bit linux x86 build.
* win32 - 32bit windows build.
* winx64 - 64bit windows build.
- configs
* debug - non-optimized build
* release - optimized build
* sanitizer - sanitizer build
* coverage - special config used for code-coverage on autobuilders
- compilers:
* gcc - build with gcc ( linux only as of now )
* clang - build with gcc ( linux only as of now )
* msvs14 - build with msvc14 ( windows only )
* msvs10 - build with msvc10 ( windows only )
* msvs8 - build with msvc8 ( windows only )
- this will build all target for the specified platform and configuration into `local/<platform>/<compiler>/<config>`
- platforms:
* linux_x86 - 32bit linux x86 build.
* linux_x86_64 - 64bit linux x86 build.
* win32 - 32bit windows build.
* winx64 - 64bit windows build.
- configs
* debug - non-optimized build
* release - optimized build
* sanitizer - sanitizer build
* coverage - special config used for code-coverage on autobuilders
- compilers:
* gcc - build with gcc ( linux only as of now )
* clang - build with gcc ( linux only as of now )
* msvs14 - build with msvc14 ( windows only )
* msvs10 - build with msvc10 ( windows only )
* msvs8 - build with msvc8 ( windows only )
> Note:
> msvc14/msvc10/msvc8 compiler will be replaced with only msvc later and use the cl.exe/link.exe found in PATH
You can also build specific 'targets' with `local/bam/bam platform=<platform> config=<config> compiler=<compiler> -r sc <target>`
- valid targets
* 'test' - build unittests and all its dependencies + run tests
* 'test_valgrind' - build unittests and all its dependencies + run tests under valgrind
* 'test_gdb' - build unittests and all its dependencies + run tests under gdb
* 'benchmark' - build benchmark and all its dependencies + run benchmarks
- valid targets
* 'test' - build unittests and all its dependencies + run tests
* 'test_valgrind' - build unittests and all its dependencies + run tests under valgrind
* 'test_gdb' - build unittests and all its dependencies + run tests under gdb
* 'benchmark' - build benchmark and all its dependencies + run benchmarks
> when running any of the 'test*' targets you can also specify 'test_filter=***' to pass a filter on what tests to run to the executable.
Expand Down
64 changes: 64 additions & 0 deletions include/dl/dl.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,70 @@ typedef struct dl_instance_info
*/
dl_error_t DL_DLL_EXPORT dl_instance_get_info( const unsigned char* packed_instance, size_t packed_instance_size, dl_instance_info_t* out_info );

/*
Function: dl_get_metadata_cnt
Fetch the number of meta data instances for the specified type, enum or union
Parameters:
dl_ctx - Context to load type-library into.
type - Type id for type to fetch the meta data count of.
count - The number of meta data instances available for the give type
Return:
DL_ERROR_OK on success.
*/
dl_error_t DL_DLL_EXPORT dl_get_metadata_cnt( dl_ctx_t dl_ctx, dl_typeid_t type, unsigned int* count );

/*
Function: dl_get_member_metadata_cnt
Fetch the number of meta data instances for the specified type member, enumerator or union member
Parameters:
dl_ctx - Context to load type-library into.
type - Type id for type to fetch the meta data count of.
item_index - The index of the member, enumerator or union member
count - The number of meta data instances available for the give type
Return:
DL_ERROR_OK on success.
*/
dl_error_t DL_DLL_EXPORT dl_get_member_metadata_cnt( dl_ctx_t dl_ctx, dl_typeid_t type, unsigned int item_index, unsigned int* count );

/*
Function: dl_get_metadata
Fetch the number of meta data instances for the specified type
Parameters:
dl_ctx - Context to load type-library into.
type - Type id for type to fetch the meta data instance of.
index - The index of the meta data to fetch
out_type_id - The type id of the returned meta data instance
out_instance_size - The size of the returned instance
out_instance - A pointer to the instance
Return:
DL_ERROR_OK on success.
*/
dl_error_t DL_DLL_EXPORT dl_get_metadata( dl_ctx_t dl_ctx, dl_typeid_t type, unsigned int index, dl_typeid_t* out_type_id, unsigned int* out_instance_size, void** out_instance );

/*
Function: dl_get_member_metadata
Fetch the number of meta data instances for the specified type
Parameters:
dl_ctx - Context to load type-library into.
type - Type id for type to fetch the meta data instance of.
index - The index of the meta data to fetch
item_index - The index of the member, enumerator or union member
out_type_id - The type id of the returned meta data instance
out_instance_size - The size of the returned instance
out_instance - A pointer to the instance
Return:
DL_ERROR_OK on success.
*/
dl_error_t DL_DLL_EXPORT dl_get_member_metadata( dl_ctx_t dl_ctx, dl_typeid_t type, unsigned int index, unsigned int item_index, dl_typeid_t* out_type_id, unsigned int* out_instance_size, void** out_instance );

#ifdef __cplusplus
}
#endif // __cplusplus
Expand Down
Loading

0 comments on commit b5617d6

Please sign in to comment.