CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
serwriter.h
Go to the documentation of this file.
1
3
4#pragma once
5
8
9CX_C_BEGIN
10
14
15typedef struct SerWriter SerWriter;
16
21typedef struct SerWriterOps {
22 bool (*writeNull)(_Inout_ SerWriter* w);
23 bool (*writeBool)(_Inout_ SerWriter* w, bool v);
24 bool (*writeInt)(_Inout_ SerWriter* w, int64 v, stype declared);
25 bool (*writeUint)(_Inout_ SerWriter* w, uint64 v, stype declared);
26 bool (*writeReal)(_Inout_ SerWriter* w, float64 v, stype declared);
27 bool (*writeStr)(_Inout_ SerWriter* w, _In_opt_ strref v);
28 bool (*writeBytes)(_Inout_ SerWriter* w, _In_reads_bytes_(n) const void* p, size_t n);
29
31 bool (*arrBegin)(_Inout_ SerWriter* w, int32 count);
32 bool (*arrEnd)(_Inout_ SerWriter* w);
34 bool (*mapBegin)(_Inout_ SerWriter* w, int32 count);
35 bool (*mapKey)(_Inout_ SerWriter* w, _In_opt_ strref key);
38 bool (*mapKeyTyped)(_Inout_ SerWriter* w, _In_ const STypeInfoExt* kt, stgeneric key);
39 bool (*mapEnd)(_Inout_ SerWriter* w);
40
41 bool (*typeTag)(_Inout_ SerWriter* w, _In_ const STypeInfoExt* st);
42
49 bool (*refDef)(_Inout_ SerWriter* w, uint32 id);
51 bool (*refUse)(_Inout_ SerWriter* w, uint32 id);
52
53 bool (*finish)(_Inout_ SerWriter* w);
54 void (*destroy)(_Inout_ _Post_invalid_ SerWriter* w);
56
60typedef struct SerWriter {
61 const SerWriterOps* ops;
62 flags_t caps;
63 flags_t flags;
64 bool finished; // serWriterFinish has run
65 SerError err;
66 SerTraverseState traverse; // path stack and error list; managed internally
67
68 sa_ptr inprogress; // objects currently being written; cycle guard, unused with SER_Refs
69
70 hashtable refids; // object -> assigned reference id, once SER_Refs is in use
71 uint32 nextrefid; // next reference id to hand out
72 // backend private data follows
73} SerWriter;
74
85_Ret_notnull_ SerWriter* _serWriterAlloc(size_t size, _In_ const SerWriterOps* ops, flags_t caps,
86 flags_t flags);
87
96bool serWriterFinish(_Inout_ SerWriter* w);
97
104void serWriterDestroy(_Inout_ SerWriter** w);
105
109#define serWriterCaps(w) ((w)->caps)
110
123#define serWriterCan(w, capname) (((w)->caps & SER_Cap_##capname) != 0)
124
125// Thin wrappers around the vtable, used by the traverser and by custom serialize hooks.
126_meta_inline bool serWriteNull(_Inout_ SerWriter* w) { return w->ops->writeNull(w); }
127_meta_inline bool serWriteBool(_Inout_ SerWriter* w, bool v) { return w->ops->writeBool(w, v); }
128_meta_inline bool serWriteInt(_Inout_ SerWriter* w, int64 v, stype declared)
129{
130 return w->ops->writeInt(w, v, declared);
131}
132_meta_inline bool serWriteUint(_Inout_ SerWriter* w, uint64 v, stype declared)
133{
134 return w->ops->writeUint(w, v, declared);
135}
136_meta_inline bool serWriteReal(_Inout_ SerWriter* w, float64 v, stype declared)
137{
138 return w->ops->writeReal(w, v, declared);
139}
140_meta_inline bool serWriteStr(_Inout_ SerWriter* w, _In_opt_ strref v)
141{
142 return w->ops->writeStr(w, v);
143}
144_meta_inline bool serWriteBytes(_Inout_ SerWriter* w, _In_reads_bytes_(n) const void* p, size_t n)
145{
146 return w->ops->writeBytes(w, p, n);
147}
148_meta_inline bool serArrBegin(_Inout_ SerWriter* w, int32 count)
149{
150 return w->ops->arrBegin(w, count);
151}
152_meta_inline bool serArrEnd(_Inout_ SerWriter* w) { return w->ops->arrEnd(w); }
153_meta_inline bool serMapBegin(_Inout_ SerWriter* w, int32 count)
154{
155 return w->ops->mapBegin(w, count);
156}
157_meta_inline bool serMapKey(_Inout_ SerWriter* w, _In_opt_ strref key)
158{
159 return w->ops->mapKey(w, key);
160}
161_meta_inline bool serMapEnd(_Inout_ SerWriter* w) { return w->ops->mapEnd(w); }
162
163// Prefer the serWrite() macro. This takes the schema as a descriptor rather than a type name,
164// for the cases the macro cannot spell: a generated member descriptor (`memberdesc->schema`), a
165// hand-built container descriptor, or a slot whose schema and value type come from different
166// names -- a `structp` in a `structset` slot, for instance. The schema is never NULL; a dynamic
167// slot uses a leaf descriptor such as `stExt(stvar)`. The value's runtime type is `schema->type`.
168bool _serWrite(_Inout_ SerWriter* w, _In_ const STypeInfoExt* schema, stgeneric val);
169
188#define serWrite(w, type, val) _serWrite(w, stExt(type), stArg(type, val))
189
199bool serWriterFail(_Inout_ SerWriter* w, int32 code, _In_opt_ strref msg);
200
202
203CX_C_END
bool serWriterFinish(SerWriter *w)
SerWriter * _serWriterAlloc(size_t size, const SerWriterOps *ops, flags_t caps, flags_t flags)
bool serWriterFail(SerWriter *w, int32 code, strref msg)
void serWriterDestroy(SerWriter **w)
Hash table container with type-safe generic key-value storage.
The abstract data model, type resolution, and serialization errors.
bool(* mapBegin)(SerWriter *w, int32 count)
Definition serwriter.h:34
void(* destroy)(SerWriter *w)
frees private state and the struct
Definition serwriter.h:54
bool(* arrBegin)(SerWriter *w, int32 count)
Definition serwriter.h:31
bool(* refDef)(SerWriter *w, uint32 id)
Definition serwriter.h:49
bool(* finish)(SerWriter *w)
last chance to fail; optional
Definition serwriter.h:53
bool(* refUse)(SerWriter *w, uint32 id)
Stands in for the whole value, which was already written under id.
Definition serwriter.h:51
bool(* mapKeyTyped)(SerWriter *w, const STypeInfoExt *kt, stgeneric key)
Definition serwriter.h:38
flags_t caps
SerCapabilitiesEnum – what this backend can represent.
Definition serwriter.h:62
flags_t flags
SerFlagsEnum – per-document options.
Definition serwriter.h:63