3
* Luabins internal constants and helper macros
4
* See copyright notice in luabins.h
7
#ifndef LUABINS_SAVELOAD_H_INCLUDED_
8
#define LUABINS_SAVELOAD_H_INCLUDED_
10
/* Find minimum of two values */
11
#define luabins_min(a, b) \
12
( ((a) < (b)) ? (a) : (b) )
14
/* Find maximum of two values */
15
#define luabins_max(a, b) \
16
( ((a) > (b)) ? (a) : (b) )
18
/* Find minimum of three values */
19
#define luabins_min3(a, b, c) \
20
( ((a) < (b)) ? luabins_min((a), (c)) : luabins_min((b), (c)) )
22
/* Preprocessor concatenation */
23
#define LUABINS_CAT(a, b) a##b
25
/* Static assert helper macro */
26
#define luabins_static_assert_line(pred, line) \
27
typedef char LUABINS_CAT( \
28
static_assertion_failed_at_line_, \
32
/* Static (compile-time) assert */
33
#define luabins_static_assert(pred) \
34
luabins_static_assert_line(pred, __LINE__)
36
/* Internal error codes */
37
#define LUABINS_ESUCCESS (0)
38
#define LUABINS_EFAILURE (1)
39
#define LUABINS_EBADTYPE (2)
40
#define LUABINS_ETOODEEP (3)
41
#define LUABINS_ENOSTACK (4)
42
#define LUABINS_EBADDATA (5)
43
#define LUABINS_ETAILEFT (6)
44
#define LUABINS_EBADSIZE (7)
45
#define LUABINS_ETOOLONG (8)
48
#define LUABINS_CNIL '-' /* 0x2D (45) */
49
#define LUABINS_CFALSE '0' /* 0x30 (48) */
50
#define LUABINS_CTRUE '1' /* 0x31 (49) */
51
#define LUABINS_CNUMBER 'N' /* 0x4E (78) */
52
#define LUABINS_CSTRING 'S' /* 0x53 (83) */
53
#define LUABINS_CTABLE 'T' /* 0x54 (84) */
56
* PORTABILITY WARNING!
57
* You have to ensure manually that length constants below are the same
58
* for both code that does the save and code that does the load.
59
* Also these constants must be actual or code below would break.
60
* Beware of endianness and lua_Number actual type as well.
61
* Note also that luabins does not check for overflow on save,
62
* if your integer does not fit, it would be truncated.
64
#define LUABINS_LINT (4)
65
#define LUABINS_LSIZET (4)
66
#define LUABINS_LNUMBER (8)
71
* WARNING: Change these if format is changed!
75
#define LUABINS_LTYPEBYTE (1)
77
/* Minimal table: type, array size, hash size, no data */
78
#define LUABINS_LMINTABLE (LUABINS_LTYPEBYTE + LUABINS_LINT + LUABINS_LINT)
80
/* Minimal number: type, number value */
81
#define LUABINS_LMINNUMBER (LUABINS_LTYPEBYTE + LUABINS_LNUMBER)
83
/* Minimal string: type, length, no data */
84
#define LUABINS_LMINSTRING (LUABINS_LTYPEBYTE + LUABINS_LSIZET)
86
/* Minimum large (non-boolean non-nil) value length */
87
#define LUABINS_LMINLARGEVALUE \
88
( luabins_min3(LUABINS_LMINTABLE, LUABINS_LMINSTRING, LUABINS_LMINSTRING) )
91
* Lower limit on total table data size is determined as follows:
92
* -- All entries are always key and value.
93
* -- Minimum value size is one byte for nil and boolean,
94
* but that is two keys maximum (nil can'be the key).
95
* -- All the rest of key types are mimimum of LUABINS_MINLARGEVALUELEN
96
* bytes (type byte plus data bytes).
97
* -- All values in the table may be booleans.
99
* WARNING: Change this if format is changed!
101
* Note this formula does NOT take in account
102
* table header (type byte and array/hash sizes).
104
#define luabins_min_table_data_size(total_size) \
108
2 * (LUABINS_LTYPEBYTE + LUABINS_LTYPEBYTE) \
109
+ (total_size - 2) * (LUABINS_LMINLARGEVALUE + LUABINS_LTYPEBYTE) \
111
: (total_size * (LUABINS_LTYPEBYTE + LUABINS_LTYPEBYTE)) \
114
#endif /* LUABINS_SAVELOAD_H_INCLUDED_ */