diff --git a/serial/bitcode.c b/serial/bitcode.c index 683667c..3ddc872 100644 --- a/serial/bitcode.c +++ b/serial/bitcode.c @@ -1,3 +1,46 @@ -#include "serial.h" +#include +#include -const struct b_serial_format_ops z__b_bitcode_format_ops = {}; +/*** VIRTUAL FUNCTIONS ********************************************************/ + +static enum b_status bitcode_serialise( + b_serial_ctx *serial, b_object *src, b_stream *dest, + enum b_serial_flags flags) +{ + return B_ERR_NOT_SUPPORTED; +} + +static enum b_status bitcode_deserialise( + b_serial_ctx *serial, b_stream *src, b_object **dest, + enum b_serial_flags flags) +{ + return B_ERR_NOT_SUPPORTED; +} + +static void bitcode_serial_ctx_init(b_object *obj, void *priv) +{ +} + +static void bitcode_serial_ctx_fini(b_object *obj, void *priv) +{ +} + +/*** CLASS DEFINITION *********************************************************/ + +B_TYPE_CLASS_DEFINITION_BEGIN(b_bitcode_serial_ctx) + B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) + B_INTERFACE_ENTRY(to_string) = NULL; + B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) + + B_TYPE_CLASS_INTERFACE_BEGIN(b_serial_ctx, B_TYPE_SERIAL_CTX) + B_INTERFACE_ENTRY(s_serialise) = bitcode_serialise; + B_INTERFACE_ENTRY(s_deserialise) = bitcode_deserialise; + B_TYPE_CLASS_INTERFACE_END(b_serial_ctx, B_TYPE_SERIAL_CTX) +B_TYPE_CLASS_DEFINITION_END(b_bitcode_serial_ctx) + +B_TYPE_DEFINITION_BEGIN(b_bitcode_serial_ctx) + B_TYPE_ID(0xcdc8c462, 0xf2b3, 0x4193, 0x8cae, 0xc1e5ad9afcb8); + B_TYPE_CLASS(b_bitcode_serial_ctx_class); + B_TYPE_INSTANCE_INIT(bitcode_serial_ctx_init); + B_TYPE_INSTANCE_FINI(bitcode_serial_ctx_fini); +B_TYPE_DEFINITION_END(b_bitcode_serial_ctx) diff --git a/serial/ctx.c b/serial/ctx.c new file mode 100644 index 0000000..64918b4 --- /dev/null +++ b/serial/ctx.c @@ -0,0 +1,57 @@ +#include +#include +#include + +/*** PRIVATE DATA *************************************************************/ +/*** PRIVATE FUNCTIONS ********************************************************/ +/*** PUBLIC FUNCTIONS *********************************************************/ +/*** PUBLIC ALIAS FUNCTIONS ***************************************************/ +/*** VIRTUAL FUNCTIONS ********************************************************/ + +static void serial_ctx_init(b_object *obj, void *priv) +{ + b_serial_ctx_data *data = b_object_get_protected(obj, B_TYPE_SERIAL_CTX); + + data->ctx_streambuf = b_stream_buffer_create_dynamic(2048); +} + +static void serial_ctx_fini(b_object *obj, void *priv) +{ + b_serial_ctx_data *data = b_object_get_protected(obj, B_TYPE_SERIAL_CTX); + + b_stream_buffer_unref(data->ctx_streambuf); +} + +/*** CLASS DEFINITION *********************************************************/ + +B_TYPE_CLASS_DEFINITION_BEGIN(b_serial_ctx) + B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) + B_INTERFACE_ENTRY(to_string) = NULL; + B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +B_TYPE_CLASS_DEFINITION_END(b_serial_ctx) + +B_TYPE_DEFINITION_BEGIN(b_serial_ctx) + B_TYPE_ID(0xc7c1039a, 0xf397, 0x4fda, 0xb473, 0x4d86fec85384); + B_TYPE_CLASS(b_serial_ctx_class); + B_TYPE_INSTANCE_PROTECTED(b_serial_ctx_data); + B_TYPE_INSTANCE_INIT(serial_ctx_init); + B_TYPE_INSTANCE_FINI(serial_ctx_fini); +B_TYPE_DEFINITION_END(b_serial_ctx) + +/*** ITERATOR FUNCTIONS *******************************************************/ + +enum b_status b_serial_ctx_serialise( + b_serial_ctx *ctx, b_object *src, b_stream *dest, enum b_serial_flags flags) +{ + B_CLASS_DISPATCH_VIRTUAL( + b_serial_ctx, B_TYPE_SERIAL_CTX, B_ERR_NOT_SUPPORTED, + s_serialise, ctx, src, dest, flags); +} + +enum b_status b_serial_ctx_deserialise( + b_serial_ctx *ctx, b_stream *src, b_object **dest, enum b_serial_flags flags) +{ + B_CLASS_DISPATCH_VIRTUAL( + b_serial_ctx, B_TYPE_SERIAL_CTX, B_ERR_NOT_SUPPORTED, + s_deserialise, ctx, src, dest, flags); +} diff --git a/serial/include/blue/serial.h b/serial/include/blue/serial.h index 0d11ce0..bddca05 100644 --- a/serial/include/blue/serial.h +++ b/serial/include/blue/serial.h @@ -1,34 +1,8 @@ #ifndef BLUE_SERIAL_H_ #define BLUE_SERIAL_H_ -#include -#include -#include -#include - -typedef enum b_serial_format { - B_SERIAL_FORMAT_NONE = 0, - B_SERIAL_FORMAT_BITCODE, - B_SERIAL_FORMAT_JSON, - B_SERIAL_FORMAT_TOML, -} b_serial_format; - -typedef enum b_serial_flags { - B_SERIAL_F_NONE = 0, - B_SERIAL_F_PRETTY = 0x01u, -} b_serial_flags; - -typedef struct b_serial_ctx b_serial_ctx; - -BLUE_API b_status b_serial_ctx_create(b_serial_ctx **out); -BLUE_API b_status b_serial_ctx_destroy(b_serial_ctx *ctx); - -BLUE_API b_status b_serial_ctx_serialise( - b_serial_ctx *ctx, b_serial_format fmt, b_object *src, b_stream *dest, - b_serial_flags flags); - -BLUE_API b_status b_serial_ctx_deserialise( - b_serial_ctx *ctx, b_serial_format fmt, b_stream *src, b_object **dest, - b_serial_flags flags); +#include +#include +#include #endif diff --git a/serial/include/blue/serial/bitcode.h b/serial/include/blue/serial/bitcode.h new file mode 100644 index 0000000..81d4349 --- /dev/null +++ b/serial/include/blue/serial/bitcode.h @@ -0,0 +1,21 @@ +#ifndef BLUE_SERIAL_BITCODE_H_ +#define BLUE_SERIAL_BITCODE_H_ + +#include + +B_DECLS_BEGIN; + +#define B_TYPE_BITCODE_SERIAL_CTX (b_bitcode_serial_ctx_get_type()) + +B_DECLARE_TYPE(b_bitcode_serial_ctx); + +B_TYPE_CLASS_DECLARATION_BEGIN(b_bitcode_serial_ctx) +B_TYPE_CLASS_DECLARATION_END(b_bitcode_serial_ctx) + +BLUE_API b_type b_bitcode_serial_ctx_get_type(void); + +B_TYPE_DEFAULT_CONSTRUCTOR(b_bitcode_serial_ctx, B_TYPE_BITCODE_SERIAL_CTX); + +B_DECLS_END; + +#endif diff --git a/serial/include/blue/serial/ctx.h b/serial/include/blue/serial/ctx.h new file mode 100644 index 0000000..604dbb9 --- /dev/null +++ b/serial/include/blue/serial/ctx.h @@ -0,0 +1,42 @@ +#ifndef BLUE_SERIAL_CTX_H_ +#define BLUE_SERIAL_CTX_H_ + +#include +#include +#include +#include +#include + +B_DECLS_BEGIN; + +#define B_TYPE_SERIAL_CTX (b_serial_ctx_get_type()) + +typedef enum b_serial_flags { + B_SERIAL_F_NONE = 0, + B_SERIAL_F_PRETTY = 0x01u, +} b_serial_flags; + +B_DECLARE_TYPE(b_serial_ctx); + +B_TYPE_CLASS_DECLARATION_BEGIN(b_serial_ctx) + b_status (*s_serialise)( + b_serial_ctx *, b_object *, b_stream *, b_serial_flags); + b_status (*s_deserialise)( + b_serial_ctx *, b_stream *, b_object **, b_serial_flags); +B_TYPE_CLASS_DECLARATION_END(b_serial_ctx) + +typedef struct b_serial_ctx_data { + b_stream_buffer *ctx_streambuf; +} b_serial_ctx_data; + +BLUE_API b_type b_serial_ctx_get_type(void); + +BLUE_API b_status b_serial_ctx_serialise( + b_serial_ctx *ctx, b_object *src, b_stream *dest, b_serial_flags flags); + +BLUE_API b_status b_serial_ctx_deserialise( + b_serial_ctx *ctx, b_stream *src, b_object **dest, b_serial_flags flags); + +B_DECLS_END; + +#endif diff --git a/serial/include/blue/serial/toml.h b/serial/include/blue/serial/toml.h new file mode 100644 index 0000000..e66c676 --- /dev/null +++ b/serial/include/blue/serial/toml.h @@ -0,0 +1,21 @@ +#ifndef BLUE_SERIAL_TOML_H_ +#define BLUE_SERIAL_TOML_H_ + +#include + +B_DECLS_BEGIN; + +#define B_TYPE_TOML_SERIAL_CTX (b_toml_serial_ctx_get_type()) + +B_DECLARE_TYPE(b_toml_serial_ctx); + +B_TYPE_CLASS_DECLARATION_BEGIN(b_toml_serial_ctx) +B_TYPE_CLASS_DECLARATION_END(b_toml_serial_ctx) + +BLUE_API b_type b_toml_serial_ctx_get_type(void); + +B_TYPE_DEFAULT_CONSTRUCTOR(b_toml_serial_ctx, B_TYPE_TOML_SERIAL_CTX); + +B_DECLS_END; + +#endif diff --git a/serial/serial.c b/serial/serial.c deleted file mode 100644 index 7b13583..0000000 --- a/serial/serial.c +++ /dev/null @@ -1,83 +0,0 @@ -#include "serial.h" - -#include -#include -#include - -extern const struct b_serial_format_ops z__b_bitcode_format_ops; -extern const struct b_serial_format_ops z__b_toml_format_ops; - -static const struct b_serial_format_ops *format_ops[] = { - [B_SERIAL_FORMAT_NONE] = NULL, - [B_SERIAL_FORMAT_BITCODE] = &z__b_bitcode_format_ops, - [B_SERIAL_FORMAT_JSON] = NULL, - [B_SERIAL_FORMAT_TOML] = &z__b_toml_format_ops, -}; -static const size_t nr_format_ops = sizeof format_ops / sizeof format_ops[0]; - -enum b_status b_serial_ctx_create(struct b_serial_ctx **out) -{ - struct b_serial_ctx *ctx = malloc(sizeof *ctx); - if (!ctx) { - return B_ERR_NO_MEMORY; - } - - memset(ctx, 0x0, sizeof *ctx); - - ctx->ctx_streambuf = b_stream_buffer_create_dynamic(2048); - if (!ctx->ctx_streambuf) { - free(ctx); - return B_ERR_NO_MEMORY; - } - - *out = ctx; - return B_SUCCESS; -} - -enum b_status b_serial_ctx_destroy(struct b_serial_ctx *ctx) -{ - b_stream_buffer_unref(ctx->ctx_streambuf); - free(ctx); - return B_SUCCESS; -} - -static const struct b_serial_format_ops *get_formatter(enum b_serial_format fmt) -{ - if (fmt < 0 || fmt >= nr_format_ops) { - return NULL; - } - - return format_ops[fmt]; -} - -enum b_status b_serial_ctx_serialise( - struct b_serial_ctx *ctx, enum b_serial_format fmt, b_object *src, - b_stream *dest, enum b_serial_flags flags) -{ - const struct b_serial_format_ops *ops = get_formatter(fmt); - if (!ops) { - return B_ERR_INVALID_ARGUMENT; - } - - if (!ops->fmt_serialise) { - return B_ERR_NOT_SUPPORTED; - } - - return ops->fmt_serialise(ctx, src, dest, flags); -} - -enum b_status b_serial_ctx_deserialise( - struct b_serial_ctx *ctx, enum b_serial_format fmt, b_stream *src, - b_object **dest, enum b_serial_flags flags) -{ - const struct b_serial_format_ops *ops = get_formatter(fmt); - if (!ops) { - return B_ERR_INVALID_ARGUMENT; - } - - if (!ops->fmt_deserialise) { - return B_ERR_NOT_SUPPORTED; - } - - return ops->fmt_deserialise(ctx, src, dest, flags); -} diff --git a/serial/serial.h b/serial/serial.h deleted file mode 100644 index 377de71..0000000 --- a/serial/serial.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef _SERIAL_SERIAL_H_ -#define _SERIAL_SERIAL_H_ - -#include -#include -#include -#include - -struct b_serial_ctx { - b_stream_buffer *ctx_streambuf; -}; - -struct b_serial_format_ops { - enum b_status (*fmt_serialise)( - struct b_serial_ctx *, b_object *, b_stream *, enum b_serial_flags); - enum b_status (*fmt_deserialise)( - struct b_serial_ctx *, b_stream *, b_object **, - enum b_serial_flags); -}; - -#endif diff --git a/serial/toml.c b/serial/toml.c index ee5a894..e415e28 100644 --- a/serial/toml.c +++ b/serial/toml.c @@ -1,13 +1,13 @@ -#include "blue/core/stringstream.h" -#include "serial.h" - #include +#include #include #include #include #include #include #include +#include +#include #include #include #include @@ -1543,10 +1543,10 @@ static enum b_status ctx_init(struct ctx *ctx) } static enum b_status toml_serialise( - struct b_serial_ctx *serial, b_object *src, b_stream *dest, + b_serial_ctx *serial, b_object *src, b_stream *dest, enum b_serial_flags flags) { - return B_SUCCESS; + return B_ERR_NOT_SUPPORTED; } static void print_token(struct token *tok) @@ -2221,7 +2221,7 @@ static enum b_status parse_root(struct ctx *ctx, b_dict **result) } static enum b_status toml_deserialise( - struct b_serial_ctx *serial, b_stream *src, b_object **dest, + b_serial_ctx *serial, b_stream *src, b_object **dest, enum b_serial_flags flags) { struct ctx ctx = {0}; @@ -2266,7 +2266,33 @@ static enum b_status toml_deserialise( return B_SUCCESS; } -const struct b_serial_format_ops z__b_toml_format_ops = { - .fmt_serialise = toml_serialise, - .fmt_deserialise = toml_deserialise, -}; +/*** VIRTUAL FUNCTIONS ********************************************************/ + +static void toml_serial_ctx_init(b_object *obj, void *priv) +{ +} + +static void toml_serial_ctx_fini(b_object *obj, void *priv) +{ +} + +/*** CLASS DEFINITION *********************************************************/ + +B_TYPE_CLASS_DEFINITION_BEGIN(b_toml_serial_ctx) + B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) + B_INTERFACE_ENTRY(to_string) = NULL; + B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) + + B_TYPE_CLASS_INTERFACE_BEGIN(b_serial_ctx, B_TYPE_SERIAL_CTX) + B_INTERFACE_ENTRY(s_serialise) = toml_serialise; + B_INTERFACE_ENTRY(s_deserialise) = toml_deserialise; + B_TYPE_CLASS_INTERFACE_END(b_serial_ctx, B_TYPE_SERIAL_CTX) +B_TYPE_CLASS_DEFINITION_END(b_toml_serial_ctx) + +B_TYPE_DEFINITION_BEGIN(b_toml_serial_ctx) + B_TYPE_ID(0xaec8dca0, 0x131a, 0x4217, 0x916b, 0xaed15756601c); + B_TYPE_CLASS(b_toml_serial_ctx_class); + B_TYPE_EXTENDS(B_TYPE_SERIAL_CTX); + B_TYPE_INSTANCE_INIT(toml_serial_ctx_init); + B_TYPE_INSTANCE_FINI(toml_serial_ctx_fini); +B_TYPE_DEFINITION_END(b_toml_serial_ctx)