Debian Codegraph @

sys/oo.h

raw
1 #ifndef _oo_h_
2 #define _oo_h_
3 /* oo.h
4 *
5 * Copyright (C) 1994-2013,2015-2020,2022-2024,2026 Paul Boersma
6 *
7 * This code is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
11 *
12 * This code is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this work. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*** Single types. ***/
22
23 /*
24 The possible storage types give these binary formats:
25 i8: store as signed big-endian integer in 8 bits (-128..+127).
26 i16: store as signed big-endian integer in 16 bits (-32768..+32767).
27 integer: store as signed big-endian integer in 32 bits (-2147483648..+2147483647). This will be changed to allow 64 bits.
28 u8: store as unsigned big-endian integer in 8 bits (0..255).
29 u16: store as unsigned big-endian integer in 16 bits (0..65535).
30 u32: store as unsigned big-endian integer in 32 bits (0..4294967295).
31 i8LE ... u32LE: store as little-endian integers.
32 r32: store as 32-bits IEEE MSB-first floating point format.
33 r64: store as 64-bits IEEE MSB-first floating point format.
34 r80: store as 80-bits IEEE/Apple MSB-first floating point format.
35 c64: store real and imaginary part as r32.
36 c128: store real and imaginary part as r64.
37 For text format, reading imposes the same restrictions for the integer types,
38 and the real numbers are written with a precision of 8, 17 (or 16 or 15), or 20 characters.
39 */
40
41 /* Single types. Declarations like: int x; */
42
43 #define oo_BYTE(x) oo_SIMPLE (signed char, i8, x)
44 #define oo_INT(x) oo_SIMPLE (int, i16, x)
45 #define oo_INTEGER(x) oo_SIMPLE (integer, integer32BE, x)
46 #define oo_INTEGER16(x) oo_SIMPLE (integer, integer16BE, x)
47 #define oo_UBYTE(x) oo_SIMPLE (unsigned char, u8, x)
48 #define oo_UINT(x) oo_SIMPLE (unsigned int, u16, x)
49 #define oo_UINTEGER(x) oo_SIMPLE (uinteger, u32, x)
50 #define oo_INT8(x) oo_SIMPLE (int8, i8, x)
51 #define oo_INT16(x) oo_SIMPLE (int16, i16, x)
52 #define oo_INT32(x) oo_SIMPLE (int32, i32, x)
53 #define oo_INT64(x) oo_SIMPLE (int64, i64, x)
54 #define oo_UINT8(x) oo_SIMPLE (uint8, u8, x)
55 #define oo_UINT16(x) oo_SIMPLE (uint16, u16, x)
56 #define oo_UINT32(x) oo_SIMPLE (uint32, u32, x)
57 #define oo_FLOAT(x) oo_SIMPLE (double, r32, x)
58 #define oo_DOUBLE(x) oo_SIMPLE (double, r64, x)
59 //#define oo_COMPLEX(x) oo_SIMPLE (dcomplex, c128, x)
60
61 /* Sets with compile-time allocation of capacity. Declarations like: int x [1 + setType::MAX]; */
62 /* The first index is always 0, the last index is setType::MAX. */
63
64 //#define oo_BYTE_SET(x,setType) oo_SET (signed char, i8, x, setType)
65 //#define oo_INT_SET(x,setType) oo_SET (int, i16, x, setType)
66 //#define oo_INTEGER_SET(x,setType) oo_SET (integer, integer32BE, x, setType)
67 //#define oo_UBYTE_SET(x,setType) oo_SET (unsigned char, u8, x, setType)
68 //#define oo_UINT_SET(x,setType) oo_SET (unsigned int, u16, x, setType)
69 //#define oo_UINTEGER_SET(x,setType) oo_SET (uinteger, u32, x, setType)
70 //#define oo_FLOAT_SET(x,setType) oo_SET (double, r32, x, setType)
71 #define oo_DOUBLE_SET(x,setType) oo_SET (double, r64, x, setType)
72 //#define oo_COMPLEX_SET(x,setType) oo_SET (dcomplex, c128, x, setType)
73
74 #define oo_VEC(x,size) oo_ANYVEC (double, r64, x, size)
75 #define oo_obsoleteVEC32(x,size) oo_ANYVEC (double, r32, x, size)
76 #define oo_INTVEC(x,size) oo_ANYVEC (integer, integer32BE, x, size)
77 #define oo_INTVEC16(x,size) oo_ANYVEC (integer, integer16BE, x, size)
78 #define oo_BOOLVEC(x,size) oo_ANYVEC (bool, eb, x, size)
79 #define oo_COMPVEC(x,size) oo_ANYVEC (dcomplex, c128, x, size)
80 #define oo_BYTEVEC(x,size) oo_ANYVEC (byte, u8, x, size)
81
82 #define oo_MAT(x,nrow,ncol) oo_ANYMAT (double, r64, x, nrow, ncol)
83 #define oo_obsoleteMAT32(x,nrow,ncol) oo_ANYMAT (double, r32, x, nrow, ncol)
84 #define oo_INTMAT(x,nrow,ncol) oo_ANYMAT (integer, integer32BE, x, nrow, ncol)
85 #define oo_BOOLMAT(x,nrow,ncol) oo_ANYMAT (bool, eb, x, nrow, ncol)
86 #define oo_COMPMAT(x,nrow,ncol) oo_ANYMAT (dcomplex, c128, x, nrow, ncol)
87 #define oo_BYTEMAT(x,nrow,ncol) oo_ANYMAT (byte, u8, x, nrow, ncol)
88
89 #define oo_TEN3(x,ndi1,ndim2,ndim3) oo_ANYTEN3 (double, r64, x, ndim1, ndim2, ndim3)
90
91 /*** Enumerated types. ***/
92
93 /* The possible storage types give these binary formats: */
94 /* e8: store as signed integer in 8 bits. */
95 /* e16: store in signed big-endian integer in 16 bits. */
96 /* eb: store as byte 170 (false) or byte 185 (true). */
97 /* eq: store as byte 150 (no) or byte 165 (yes). */
98 /* ex: store as byte 226 (absent) or byte 241 (present). */
99 /* For text format, the value is written as a string between '<' and '>'. */
100
101 #define oo_ENUM(kType,x) oo_ENUMx (kType, e8, x)
102 //#define oo_LENUM(Type,x) oo_ENUMx (short, e16, Type, x)
103 //#define oo_ENUM_SET(Type,x,setType) oo_ENUMx_SET (signed char, e8, Type, x, setType)
104 //#define oo_LENUM_SET(Type,x,setType) oo_ENUMx_SET (short, e16, Type, x, setType)
105 #define oo_BOOLEAN(x) oo_SIMPLE (bool, eb, x)
106 #define oo_QUESTION(x) oo_SIMPLE (bool, eq, x)
107
108 /*** Strings. ***/
109
110 /* The possible storage types give these binary formats: */
111 /* s16: store as sequence of bytes, preceded with 16 bits (u16) to denote length. */
112 /* w16: store as sequence of characters (u16), preceded with 16 bits (u16) to denote length. */
113 /* s32: store as sequence of bytes, preceded with 32 bits (u32) to denote length. */
114 /* w32: store as sequence of characters (u16), preceded with 32 bits (u32) to denote length. */
115
116 #define oo_STRING(x) oo_STRINGx (w16, x)
117 #define oo_LSTRING(x) oo_STRINGx (w32, x)
118
119 //#define oo_STRING_SET(x,setType) oo_STRINGx_SET (w16, x, setType)
120 //#define oo_LSTRING_SET(x,setType) oo_STRINGx_SET (w32, x, setType)
121
122 #define oo_STRING_VECTOR(x,n) oo_STRINGx_VECTOR (w16, x, n)
123 //#define oo_LSTRING_VECTOR(x,n) oo_STRINGx_VECTOR (w32, x, n)
124
125 /********** Definitions for header files only. **********/
126 /* These are undef'ed and redefined in the header files that implement methods, */
127 /* such as oo_DESTROY.h, oo_COPY.h, oo_EQUAL.h, oo_WRITE_TEXT.h, etc. */
128
129 /* Types. */
130
131 #define oo_SIMPLE(type,storage,x) type x;
132 #define oo_SET(type,storage,x,setType) type x [1 + (int) setType::MAX];
133 #define oo_ANYVEC(type,storage,x,sizeExpression) autovector <type> x;
134 #define oo_ANYMAT(type,storage,x,nrowExpression,ncolExpression) automatrix <type> x;
135 #define oo_ANYTEN3(type,storage,x,ndim1Expression,ndim2Expression,ndim3Expression) autotensor3 <type> x;
136
137 #define oo_ENUMx(kType,storage,x) kType x;
138 //#define oo_ENUMx_SET(kType,storage,x,setType) kType x [1 + (int) setType::MAX];
139
140 #define oo_STRINGx(storage,x) autostring32 x;
141 #define oo_STRINGx_SET(storage,x,setType) autostring32 x [1 + setType::MAX];
142 #define oo_STRINGx_VECTOR(storage,x,size) autoSTRVEC x;
143
144 #define oo_STRUCT(Type,x) struct struct##Type x;
145 #define oo_STRUCT_SET(Type,x,setType) struct struct##Type x [1 + (int) setType::MAX];
146 #define oo_STRUCTVEC(Type,x,sizeExpression) autovector <struct##Type> x;
147 #define oo_STRUCTMAT(Type,x,nrowExpression,ncolExpression) automatrix <struct##Type> x;
148
149 #define oo_OBJECT(Class,version,x) auto##Class x;
150 #define oo_COLLECTION_OF(Class,x,ItemClass,version) Class<struct##ItemClass> x;
151 #define oo_COLLECTION(Class,x,ItemClass,version) auto##Class x;
152 #define oo_TRANSIENT_FILE(x) structMelderFile x;
153 #define oo_TRANSIENT_FOLDER(x) structMelderFolder x;
154 #define oo_UNSAFE_BORROWED_TRANSIENT_CONST_OBJECT_REFERENCE(Class,x) const##Class x;
155 #define oo_UNSAFE_BORROWED_TRANSIENT_MUTABLE_OBJECT_REFERENCE(Class,x) mutable##Class x;
156
157 /* Struct and class definitions. */
158
159 #define oo_DEFINE_STRUCT(T) \
160 typedef struct struct##T *T; \
161 typedef const struct struct##T *const##T; \
162 struct struct##T {
163 #define oo_END_STRUCT(T) \
164 void destroy (); \
165 void copy (T data_to) const; \
166 bool equal (T otherData); \
167 Data_Description description (); \
168 static Data_Description s_description; \
169 bool canWriteAsEncoding (int outputEncoding); \
170 void writeText (MelderFile openFile); \
171 void readText (MelderReadText text, int formatVersion); \
172 void writeBinary (FILE *f); \
173 void readBinary (FILE *f, int formatVersion); \
174 };
175
176 #define oo_DEFINE_CLASS(klas,parent) \
177 typedef struct struct##klas *klas; \
178 typedef struct struct##klas *mutable##klas; \
179 typedef const struct struct##klas *const##klas; \
180 typedef autoSomeThing <struct##klas> auto##klas; \
181 typedef struct##parent klas##_Parent; \
182 extern struct structClassInfo theClassInfo_##klas; \
183 extern ClassInfo class##klas; \
184 struct struct##klas : public struct##parent {
185
186 #define oo_END_CLASS(Class) \
187 void v9_destroy () noexcept override; \
188 void v1_copy (Daata data_to) const override; \
189 bool v1_equal (Daata otherData) override; \
190 static Data_Description s_description; \
191 Data_Description v_description () override { return s_description; } \
192 bool v1_canWriteAsEncoding (int outputEncoding) override; \
193 void v1_writeText (MelderFile openFile) override; \
194 void v1_readText (MelderReadText text, int formatVersion) override; \
195 void v1_writeBinary (FILE *f) override; \
196 void v1_readBinary (FILE *f, int formatVersion) override; \
197 };
198
199 /*** Miscellaneous. ***/
200
201 /* For fields that should not be read in older versions: */
202 #define oo_FROM(from)
203 #define oo_ENDFROM
204
205 #define oo_VERSION_UNTIL(version) \
206 if (_formatVersion_ < version) {
207 #define oo_VERSION_ELSE_UNTIL(version) \
208 } else if (_formatVersion_ < version) {
209 #define oo_VERSION_ELSE \
210 } else {
211 #define oo_VERSION_END \
212 }
213
214 /* Examples of the usage of the following macros:
215 For code that should only appear in reading methods (consistency checks etc.),
216 use: #if oo_READING
217 For code that should only appear in writing methods (e.g., debugging statements),
218 use: #if oo_WRITING
219 For fields that are not destroyed, copied, or compared, either (i.e., only declarations),
220 use: #if oo_DECLARING
221 For fields that do not take part in I/O (e.g. redundant or temporary fields),
222 use: #if ! oo_READING && ! oo_WRITING && ! oo_COMPARING
223 For fields that should not be read (e.g., when expanding a database file),
224 use: #if ! oo_READING
225 For fields that should not be written (e.g., when shrinking a database file),
226 use: #if ! oo_WRITING
227 The last two should be temporary situations,
228 because the output files do not match the input files!
229 */
230 #define oo_DECLARING 1
231 #define oo_DESTROYING 0
232 #define oo_COPYING 0
233 #define oo_COMPARING 0
234 #define oo_VALIDATING_ASCII 0
235 #define oo_READING 0
236 #define oo_READING_TEXT 0
237 #define oo_READING_BINARY 0
238 #define oo_WRITING 0
239 #define oo_WRITING_TEXT 0
240 #define oo_WRITING_BINARY 0
241 #define oo_DESCRIBING 0
242
243 /* End of file oo.h */
244 #endif