You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/_SOURCE/0.8.x/fio-stl.md
+321-2Lines changed: 321 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3219,14 +3219,16 @@ The JSON parsing should stop with an error.
3219
3219
3220
3220
The facil.io library includes a dynamic type system that makes it a easy to handle mixed-type tasks, such as JSON object construction.
3221
3221
3222
-
This dynamic type system is based on the core types and API provided by the core STL library.
3222
+
This soft type system included in the facil.io STL is based on the Core types mentioned above and shares their API (Dynamic Strings, Dynamic Arrays, and Hash Maps).
3223
3223
3224
3224
The `FIOBJ` type API is divided by it's inner types (tested using `FIOBJ_TYPE(obj)` or `FIOBJ_TYPE_IS(obj, type)`).
3225
3225
3226
3226
In addition, some `FIOBJ` functions can be called for any `FIOBJ` object, regardless of their type.
3227
3227
3228
3228
The documentation regarding the `FIOBJ` soft-type system is divided as follows:
3229
3229
3230
+
* [`FIOBJ` General Considerations](#fiobj-general-considerations)
3231
+
3230
3232
* [`FIOBJ` Core Type Identification](#fiobj-core-type-identification)
@@ -3245,14 +3247,39 @@ The documentation regarding the `FIOBJ` soft-type system is divided as follows:
3245
3247
3246
3248
* [Hash Maps](#fiobj-hash-maps)
3247
3249
3248
-
* [JSON](#fiobj-json-helpers)
3250
+
* [JSON Helpers](#fiobj-json-helpers)
3251
+
3252
+
* [How to Extend the `FIOBJ` Type System](#how-to-extend-the-fiobj-type-system)
3249
3253
3250
3254
In the facil.io web application framework, there are extensions to the core `FIOBJ` primitives, including:
3251
3255
3252
3256
* [IO storage](fiobj_io)
3253
3257
3254
3258
* [Mustache](fiobj_mustache)
3255
3259
3260
+
3261
+
### `FIOBJ` General Considerations
3262
+
3263
+
1. To use the `FIOBJ` soft types, define the `FIO_FIOBJ` macro and then include the facil.io STL header.
3264
+
3265
+
2. To include declarations as globally available symbols (allowing the functions to be called from multiple C files), define `FIOBJ_EXTERN` _before_ including the STL header.
3266
+
3267
+
This also requires that _only_ a single C file (translation unit) define `FIOBJ_EXTERN_COMPLETE` _before_ including the header with the `FIOBJ_EXTERN` directive.
3268
+
3269
+
3. The `FIOBJ` types use pointer tagging and require that the memory allocator provide allocations on 64 bit memory alignment boundaries.
3270
+
3271
+
If the system allocator doesn't provide (at least) 64 bit allocation boundaries, use the facil.io memory allocator provided (`fio_malloc`).
3272
+
3273
+
4. The `FIOBJ` soft type system uses an "ownership" memory model.
3274
+
3275
+
This means that Arrays "own" their members and Hash Maps "own" their values (but **not** the keys).
3276
+
3277
+
Freeing an Array will free all the objects within the Array. Freeing a Hash Map will free all the values within the Hash Map (but none of the keys).
3278
+
3279
+
Ownership is only transferred if the object is removed from it's container.
3280
+
3281
+
i.e., `fiobj_array_get` does **not** transfer ownership (it's just a short temporary "loan"). Whereas, `fiobj_array_remove` **does** revoke ownership - either freeing the object or moving the ownership to the pointer provided to hold the `old` value.
3282
+
3256
3283
### `FIOBJ` Core Type Identification
3257
3284
3258
3285
`FIOBJ` objects can contain any number of possible types, including user defined types.
@@ -3883,3 +3910,295 @@ Returns a FIOBJ object matching the JSON valid C string `str`.
3883
3910
If the parsing failed (no complete valid JSON data) `FIOBJ_INVALID` is returned.
3884
3911
3885
3912
`fiobj_json_parse2` is a helper macro, it calls `fiobj_json_parse` with the provided string information.
3913
+
3914
+
### How to Extend the `FIOBJ` Type System
3915
+
3916
+
The `FIOBJ` source code includes two extensions for the `Float` and `Number` types.
3917
+
3918
+
In many cases, numbers and floats can be used without memory allocations. However, when memory allocation is required to store the data, the `FIOBJ_T_NUMBER` and `FIOBJ_T_FLOAT` types are extended using the same techniques described here.
3919
+
3920
+
#### `FIOBJ` Extension Requirements
3921
+
3922
+
To extend the `FIOBJ` soft type system, there are a number of requirements:
3923
+
3924
+
1. A **unique** type ID must be computed.
3925
+
3926
+
Type IDs are `size_t` bits in length. Values under 100 are reserved. Values under 40 are illegal (might break implementation).
3927
+
3928
+
2. A static virtual function table object (`FIOBJ_class_vtable_s`) must be fully populated (`NULL` values may break cause a segmentation fault).
3929
+
3930
+
3. The unique type construct / destructor must be wrapped using the facil.io reference counting wrapper (using `FIO_REF_NAME`).
3931
+
3932
+
The `FIO_REF_METADATA` should be set to a `FIOBJ_class_vtable_s` pointer and initialized for every object.
3933
+
3934
+
4. The unique type wrapper must use pointer tagging as described bellow (`FIO_PTR_TAG`).
3935
+
3936
+
5. A public API should be presented.
3937
+
3938
+
#### `FIOBJ` Pointer Tagging
3939
+
3940
+
The `FIOBJ` types is often identified by th a bit "tag" added to the pointer.
3941
+
3942
+
The facil.io memory allocator (`fio_malloc`), as well as most system allocators, promise a 64 bit allocation alignment.
3943
+
3944
+
The `FIOBJ` types leverage this behavior by utilizing the least significant 3 bits that are always zero.
3945
+
3946
+
The following macros should be defined for tagging an extension `FIOBJ` type, allowing the `FIO_REF_NAME` constructor / destructor to manage pointer tagging.
**Note**: The header assumes that _somewhere_ there's a C implementation file that includes the `FIOBJ` implementation. That C file defines the `FIOBJ_EXTERN_COMPLETE` macro.
0 commit comments