CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
struct.h
1#pragma once
2
5#include <cx/container/stype_hashtable.h>
6#include <cx/container/stype_sarray.h>
7#include <cx/string.h>
8#include <cx/stype/stype.h>
9#include "stype_struct.h"
10
11CX_C_BEGIN
12
15
25
26typedef struct StructSet StructSet;
27
28typedef struct StructMemberDesc {
29 strref name; // Name on the wire; empty for a [noserialize] member
30 size_t offset; // Offset within the struct
31
32 const STypeInfoExt* schema; // Declared type, including nested/nominal type info
33
34 uint32 flags; // Member flags (StructMemberFlagsEnum)
35 uint32 cflags; // Creation flags (e.g. for arrays, hashtables, etc.)
36 uint32 arrsize; // Fixed-size C arrays: element count (0 = scalar)
37} StructMemberDesc;
38
39typedef struct StructInfo {
40 strref name; // Name of the struct
41 size_t structsize; // Size in bytes of the struct
42 int nmembers; // Number of struct members
43 const void* defaults; // Default struct to copy (if any)
44 stype type; // Runtime type descriptor for this struct
45
46 void (*init)(void* _struct); // Optional: called after zero-fill to set defaults
47 void (*destroy)(void* _struct); // Optional: called before members are destroyed
48
49 const StructMemberDesc members[]; // Array of struct member descriptors
50} StructInfo;
51
52// A named list of struct types, used for a member that can hold any one of them.
53typedef struct StructSet {
54 int nentries;
55 const StructInfo* entries[]; // sorted by name, for binary search
56} StructSet;
57
63_Ret_maybenull_ const StructInfo* structSetFind(_In_ const StructSet* ss, _In_opt_ strref name);
64
65typedef struct StructBase {
66 union {
67 const StructInfo* structinfo;
68 void* _is_struct; // compile-time marker; not for direct use
69 };
70
71 // Struct-specific data members follow
72} StructBase;
73
74#define structInfoName(sname) sname##_structinfo
75#define structDefaultsName(sname) sname##_structdefaults
76
77#define STRUCTBASE(s) (unused_noeval(&((s)->_is_struct)), (StructBase*)(s))
78#define STRUCTHANDLE(sp) (unused_noeval(&((*sp)->_is_struct)), (StructBase**)(sp))
79
80void _structInitMany(_Out_ StructBase* base, _In_ const StructInfo* info, int number);
81
99#define structInitMany(structname, s, n) \
100 _structInitMany(STRUCTBASE(s), &structInfoName(structname), n)
101
118#define structInit(structname, s) _structInitMany(STRUCTBASE(s), &structInfoName(structname), 1)
119
120_Ret_notnull_ StructBase* _structAlloc(_In_ const StructInfo* info);
121
139#define structCreate(structname) ((structname*)_structAlloc(&structInfoName(structname)))
140
141// Destroy one member of one struct instance, including every element if it is a fixed
142// C array. Exposed for the struct traverser; ordinary code goes through the macros below.
143void _structDestroyMember(_In_ const StructMemberDesc* member, _Inout_ StructBase* s);
144
145void _structDestroyMembersMany(_Pre_notnull_ _Post_invalid_ StructBase* base, int number);
146
165#define structDestroyMembersMany(s, n) _structDestroyMembersMany(STRUCTBASE(s), n)
166
185#define structDestroyMembers(s) _structDestroyMembersMany(STRUCTBASE(s), 1)
186
187_At_(*pbase, _Pre_maybenull_ _Post_null_) void _structDestroy(StructBase** pbase);
188
205#define structDestroy(ps) _structDestroy(STRUCTHANDLE(ps))
206
208
209CX_C_END
const StructInfo * structSetFind(const StructSet *ss, strref name)
StructMemberFlagsEnum
Definition struct.h:16
@ STRUCT_NoCopy
Member should be skipped during copy operations.
Definition struct.h:18
@ STRUCT_NoSerialize
Member should not be serialized (e.g. by JSON, etc.)
Definition struct.h:19
@ STRUCT_Ignore
Definition struct.h:23
@ STRUCT_NoDestroy
Member should not be automatically destroyed.
Definition struct.h:17
Hash table container with type-safe generic key-value storage.
Dynamic arrays with type-safe generic programming.
Copy-on-write strings with automatic memory management and rope optimization.
Runtime type system and type descriptor infrastructure.