Predefined data types
Here is the list of predefined types provided to bindings since version 4.
type name | C macro name | description |
---|---|---|
#opaque | AFB_PREDEFINED_TYPE_OPAQUE | The opaque type is anything represented by its address |
#stringz | AFB_PREDEFINED_TYPE_STRINGZ | Type of zero terminated string |
#json | AFB_PREDEFINED_TYPE_JSON | Type of json string terminated by a zero |
#json_c | AFB_PREDEFINED_TYPE_JSON_C | Type of json object as handled by libjson-c |
#bytearray | AFB_PREDEFINED_TYPE_BYTEARRAY | Type for arrays of bytes |
#bool | AFB_PREDEFINED_TYPE_BOOL | Type of boolean values |
#i32 | AFB_PREDEFINED_TYPE_I32 | Type of signed 32 bit integers |
#u32 | AFB_PREDEFINED_TYPE_U32 | Type of unsigned 32 bit integers |
#i64 | AFB_PREDEFINED_TYPE_I64 | Type of signed 64 bit integers |
#u64 | AFB_PREDEFINED_TYPE_U64 | Type of unsigned 64 bit integers |
#double | AFB_PREDEFINED_TYPE_DOUBLE | Type of doubles |
Below section describes the predefined types. For each of them, the following items are explained:
- length: explain the meaning of the length
- family: what is the family of the type
- converters: implemented converters
- updaters: implemented updaters
- examples: some exemples of use
For all of them, data is represented by the address to its first byte.
AFB_PREDEFINED_TYPE_OPAQUE
The opaque type is anything represented by its address.
length
The length is not used
flags
- Afb_Type_Flags_Opaque
family
No family
converters
The type AFB_PREDEFINED_TYPE_OPAQUE converts to any of the below types:
- AFB_PREDEFINED_TYPE_STRINGZ
- AFB_PREDEFINED_TYPE_JSON
- AFB_PREDEFINED_TYPE_JSON_C
The conversion implies the creation of a unique anchor for the given data and producing the string representation of that anchor. For example, if the created anchor for a data is the number 0x3f7c, the string representation of the opacified data is “#@3f7c”.
updaters
No updater
examples
In this example, an internal instance of context
is wrapped to a data.
/**
* Get the data referencing the context, create it if needed.
* The returned data has its reference count incremented and the
* caller MUST call afb_data_unref.
* @param context the context to reference
* @param data address to return the data representing the context
* @return 0 on success, a negative code otherwise
*/
int data_of_context(struct context *context, afb_data_t *data)
{
int code = 0;
if (context->data == NULL)
code = afb_create_data_raw(&context->data, AFB_PREDEFINED_TYPE_OPAQUE, context, 0, 0, 0);
*data = afb_data_addref(context->data); /* neutral if context->data is NULL */
return code;
}
AFB_PREDEFINED_TYPE_BYTEARRAY
Type for arrays of bytes
length
The count of byte in the array
flags
- Afb_Type_Flags_Shareable
- Afb_Type_Flags_Streamable
family
No family
converters
No converter
updaters
No updater
examples
AFB_PREDEFINED_TYPE_STRINGZ
Type of zero terminated string UTF8, ASCII or any encoding compatible with zero terminated byte string.
length
The length in bytes including the tailing zero. A length of zero is compatible with an address of NULL value.
flags
- Afb_Type_Flags_Streamable
family
No family
converters
-
to AFB_PREDEFINED_TYPE_BYTEARRAY: produce the string without the trailing zero
-
to AFB_PREDEFINED_TYPE_OPAQUE: if the string is a valid opaque representation of an opacified data, return that opacified data.
-
to AFB_PREDEFINED_TYPE_JSON: translate the string to its JSON representation. Example: the string
HELLO
produces"HELLO"
-
to AFB_PREDEFINED_TYPE_JSON_C: translate the string to its json-c representation.
updaters
No updater
examples
In this first example, the created data
is a static const C string:
status = afb_create_data_raw(&data, AFB_PREDEFINED_TYPE_STRINGZ, "hello", 1+strlen("hello"), 0, 0);
In this second example, the created data
is a copy of the other string value
:
status = afb_create_data_copy(&data, AFB_PREDEFINED_TYPE_STRINGZ, value, 1+strlen(value));
In this third example, the created data
is a reference to field of value
:
status = afb_create_data_raw(&data, AFB_PREDEFINED_TYPE_STRINGZ, value->name, 1+strlen(value->name), unref_value, value);
Here the function unref_value
is called when the created data
is no more used,
meaning that the value value->name
isn’t needed anymore for the created data.
AFB_PREDEFINED_TYPE_JSON
Type of json string terminated by a zero, UTF8 or ASCII encoded.
length
The length in bytes including the tailing zero. A length of zero is compatible with an address of NULL value.
flags
- Afb_Type_Flags_Streamable
family
In the family of AFB_PREDEFINED_TYPE_STRINGZ.
It means that any data of type AFB_PREDEFINED_TYPE_JSON is also naturally of type AFB_PREDEFINED_TYPE_STRINGZ.
converters
-
to AFB_PREDEFINED_TYPE_OPAQUE: if the JSON string is a valid opaque representation of an opacified data, return that opacified data.
-
to AFB_PREDEFINED_TYPE_JSON_C: decode the JSON string to a json-c object and return a data holding it.
updaters
No updater
examples
AFB_PREDEFINED_TYPE_JSON_C
Type of json object as handled by libjson-c
length
The length is not used
flags
No flags
family
No family
converters
-
to AFB_PREDEFINED_TYPE_OPAQUE: if the JSON string is a valid opaque representation of an opacified data, return that opacified data.
-
to AFB_PREDEFINED_TYPE_JSON: encode the json-c object to its JSON string representation and return a data holding it.
updaters
No updater
examples
AFB_PREDEFINED_TYPE_BOOL
Type of boolean values
length
Must be 1
flags
- Afb_Type_Flags_Shareable
family
No family
converters
- to and from AFB_PREDEFINED_TYPE_JSON
- to and from AFB_PREDEFINED_TYPE_JSON_C
- to AFB_PREDEFINED_TYPE_I32
- to AFB_PREDEFINED_TYPE_U32
- to AFB_PREDEFINED_TYPE_I64
- to AFB_PREDEFINED_TYPE_U64
- to AFB_PREDEFINED_TYPE_DOUBLE
updaters
- from AFB_PREDEFINED_TYPE_JSON
- from AFB_PREDEFINED_TYPE_JSON_C
- to AFB_PREDEFINED_TYPE_I32
- to AFB_PREDEFINED_TYPE_U32
- to AFB_PREDEFINED_TYPE_I64
- to AFB_PREDEFINED_TYPE_U64
- to AFB_PREDEFINED_TYPE_DOUBLE
examples
AFB_PREDEFINED_TYPE_I32
Type of signed 32 bit integers
length
Must be 4
flags
- Afb_Type_Flags_Shareable
family
No family
converters
- to and from AFB_PREDEFINED_TYPE_JSON
- to and from AFB_PREDEFINED_TYPE_JSON_C
- to AFB_PREDEFINED_TYPE_I64
- to AFB_PREDEFINED_TYPE_DOUBLE
updaters
- from AFB_PREDEFINED_TYPE_JSON
- from AFB_PREDEFINED_TYPE_JSON_C
- to AFB_PREDEFINED_TYPE_I64
- to AFB_PREDEFINED_TYPE_DOUBLE
examples
AFB_PREDEFINED_TYPE_U32
Type of unsigned 32 bit integers
length
Must be 4
flags
- Afb_Type_Flags_Shareable
family
No family
converters
- to and from AFB_PREDEFINED_TYPE_JSON
- to and from AFB_PREDEFINED_TYPE_JSON_C
- to AFB_PREDEFINED_TYPE_I64
- to AFB_PREDEFINED_TYPE_U64
- to AFB_PREDEFINED_TYPE_DOUBLE
updaters
- from AFB_PREDEFINED_TYPE_JSON
- from AFB_PREDEFINED_TYPE_JSON_C
- to AFB_PREDEFINED_TYPE_I64
- to AFB_PREDEFINED_TYPE_U64
- to AFB_PREDEFINED_TYPE_DOUBLE
examples
AFB_PREDEFINED_TYPE_I64
Type of signed 64 bit integers
length
Must be 8
flags
- Afb_Type_Flags_Shareable
family
No family
converters
- to and from AFB_PREDEFINED_TYPE_JSON
- to and from AFB_PREDEFINED_TYPE_JSON_C
- to AFB_PREDEFINED_TYPE_DOUBLE
updaters
- from AFB_PREDEFINED_TYPE_JSON
- from AFB_PREDEFINED_TYPE_JSON_C
- to AFB_PREDEFINED_TYPE_DOUBLE
examples
AFB_PREDEFINED_TYPE_U64
Type of unsigned 64 bit integers
length
Must be 8
flags
- Afb_Type_Flags_Shareable
family
No family
converters
- to and from AFB_PREDEFINED_TYPE_JSON
- to AFB_PREDEFINED_TYPE_DOUBLE
updaters
- from AFB_PREDEFINED_TYPE_JSON
- to AFB_PREDEFINED_TYPE_DOUBLE
examples
AFB_PREDEFINED_TYPE_DOUBLE
Type of doubles
length
Must be 8
flags
- Afb_Type_Flags_Shareable
family
No family
converters
- to and from AFB_PREDEFINED_TYPE_JSON
- to and from AFB_PREDEFINED_TYPE_JSON_C
updaters
- from AFB_PREDEFINED_TYPE_JSON
- from AFB_PREDEFINED_TYPE_JSON_C