serial: convert b_serial_ctx to a b_object interface

This commit is contained in:
2025-10-28 15:20:57 +00:00
parent e63095c458
commit 2d29713f57
9 changed files with 225 additions and 145 deletions

View File

@@ -1,3 +1,46 @@
#include "serial.h" #include <blue/serial/bitcode.h>
#include <blue/serial/ctx.h>
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)

57
serial/ctx.c Normal file
View File

@@ -0,0 +1,57 @@
#include <blue/serial.h>
#include <stdlib.h>
#include <string.h>
/*** 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);
}

View File

@@ -1,34 +1,8 @@
#ifndef BLUE_SERIAL_H_ #ifndef BLUE_SERIAL_H_
#define BLUE_SERIAL_H_ #define BLUE_SERIAL_H_
#include <blue/core/misc.h> #include <blue/serial/bitcode.h>
#include <blue/core/object.h> #include <blue/serial/ctx.h>
#include <blue/core/status.h> #include <blue/serial/toml.h>
#include <blue/core/stream.h>
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);
#endif #endif

View File

@@ -0,0 +1,21 @@
#ifndef BLUE_SERIAL_BITCODE_H_
#define BLUE_SERIAL_BITCODE_H_
#include <blue/core/macros.h>
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

View File

@@ -0,0 +1,42 @@
#ifndef BLUE_SERIAL_CTX_H_
#define BLUE_SERIAL_CTX_H_
#include <blue/core/macros.h>
#include <blue/core/misc.h>
#include <blue/core/object.h>
#include <blue/core/status.h>
#include <blue/core/stream.h>
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

View File

@@ -0,0 +1,21 @@
#ifndef BLUE_SERIAL_TOML_H_
#define BLUE_SERIAL_TOML_H_
#include <blue/core/macros.h>
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

View File

@@ -1,83 +0,0 @@
#include "serial.h"
#include <blue/serial.h>
#include <stdlib.h>
#include <string.h>
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);
}

View File

@@ -1,21 +0,0 @@
#ifndef _SERIAL_SERIAL_H_
#define _SERIAL_SERIAL_H_
#include <blue/core/object.h>
#include <blue/core/stream.h>
#include <blue/serial.h>
#include <stdint.h>
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

View File

@@ -1,13 +1,13 @@
#include "blue/core/stringstream.h"
#include "serial.h"
#include <blue/core/status.h> #include <blue/core/status.h>
#include <blue/core/stringstream.h>
#include <blue/ds/array.h> #include <blue/ds/array.h>
#include <blue/ds/datetime.h> #include <blue/ds/datetime.h>
#include <blue/ds/dict.h> #include <blue/ds/dict.h>
#include <blue/ds/hashmap.h> #include <blue/ds/hashmap.h>
#include <blue/ds/number.h> #include <blue/ds/number.h>
#include <blue/ds/string.h> #include <blue/ds/string.h>
#include <blue/serial/ctx.h>
#include <blue/serial/toml.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -1543,10 +1543,10 @@ static enum b_status ctx_init(struct ctx *ctx)
} }
static enum b_status toml_serialise( 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) enum b_serial_flags flags)
{ {
return B_SUCCESS; return B_ERR_NOT_SUPPORTED;
} }
static void print_token(struct token *tok) 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( 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) enum b_serial_flags flags)
{ {
struct ctx ctx = {0}; struct ctx ctx = {0};
@@ -2266,7 +2266,33 @@ static enum b_status toml_deserialise(
return B_SUCCESS; return B_SUCCESS;
} }
const struct b_serial_format_ops z__b_toml_format_ops = { /*** VIRTUAL FUNCTIONS ********************************************************/
.fmt_serialise = toml_serialise,
.fmt_deserialise = toml_deserialise, 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)