oo.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #ifndef _oo_h_
  2. #define _oo_h_
  3. /* oo.h
  4. *
  5. * Copyright (C) 1994-2013,2015-2018 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 2 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. /*** Single types. ***/
  21. /*
  22. The possible storage types give these binary formats:
  23. i8: store as signed big-endian integer in 8 bits (-128..+127).
  24. i16: store as signed big-endian integer in 16 bits (-32768..+32767).
  25. integer: store as signed big-endian integer in 32 bits (-2147483648..+2147483647). This will be changed to allow 64 bits.
  26. u8: store as unsigned big-endian integer in 8 bits (0..255).
  27. u16: store as unsigned big-endian integer in 16 bits (0..65535).
  28. u32: store as unsigned big-endian integer in 32 bits (0..4294967295).
  29. i8LE ... u32LE: store as little-endian integers.
  30. r32: store as 32-bits IEEE MSB-first floating point format.
  31. r64: store as 64-bits IEEE MSB-first floating point format.
  32. r80: store as 80-bits IEEE/Apple MSB-first floating point format.
  33. c64: store real and imaginary part as r32.
  34. c128: store real and imaginary part as r64.
  35. For text format, reading imposes the same restrictions for the integer types,
  36. and the real numbers are written with a precision of 8, 17 (or 16 or 15), or 20 characters.
  37. */
  38. /* Single types. Declarations like: int x; */
  39. #define oo_BYTE(x) oo_SIMPLE (signed char, i8, x)
  40. #define oo_INT(x) oo_SIMPLE (int, i16, x)
  41. #define oo_INTEGER(x) oo_SIMPLE (integer, integer32BE, x)
  42. #define oo_UBYTE(x) oo_SIMPLE (unsigned char, u8, x)
  43. #define oo_UINT(x) oo_SIMPLE (unsigned int, u16, x)
  44. #define oo_UINTEGER(x) oo_SIMPLE (uinteger, u32, x)
  45. #define oo_INT8(x) oo_SIMPLE (int8, i8, x)
  46. #define oo_INT16(x) oo_SIMPLE (int16, i16, x)
  47. #define oo_INT32(x) oo_SIMPLE (int32, i32, x)
  48. #define oo_INT64(x) oo_SIMPLE (int64, i64, x)
  49. #define oo_UINT8(x) oo_SIMPLE (uint8, u8, x)
  50. #define oo_UINT16(x) oo_SIMPLE (uint16, u16, x)
  51. #define oo_UINT32(x) oo_SIMPLE (uint32, u32, x)
  52. #define oo_FLOAT(x) oo_SIMPLE (double, r32, x)
  53. #define oo_DOUBLE(x) oo_SIMPLE (double, r64, x)
  54. //#define oo_COMPLEX(x) oo_SIMPLE (dcomplex, c128, x)
  55. /* Sets with compile-time allocation of capacity. Declarations like: int x [1 + setType::MAX]; */
  56. /* The first index is always 0, the last index is setType::MAX. */
  57. //#define oo_BYTE_SET(x,setType) oo_SET (signed char, i8, x, setType)
  58. //#define oo_INT_SET(x,setType) oo_SET (int, i16, x, setType)
  59. //#define oo_INTEGER_SET(x,setType) oo_SET (integer, integer32BE, x, setType)
  60. //#define oo_UBYTE_SET(x,setType) oo_SET (unsigned char, u8, x, setType)
  61. //#define oo_UINT_SET(x,setType) oo_SET (unsigned int, u16, x, setType)
  62. //#define oo_UINTEGER_SET(x,setType) oo_SET (uinteger, u32, x, setType)
  63. //#define oo_FLOAT_SET(x,setType) oo_SET (double, r32, x, setType)
  64. #define oo_DOUBLE_SET(x,setType) oo_SET (double, r64, x, setType)
  65. //#define oo_COMPLEX_SET(x,setType) oo_SET (dcomplex, c128, x, setType)
  66. /* Arrays with run-time allocation of size. Declarations like: int *x; */
  67. /* The first index is 'min', the last index is 'max'. */
  68. /* While the structure exists, 'min' and 'max' may change in any direction if the pointer changes; */
  69. /* if the pointer does not change, 'min' cannot change, but 'max' may become lower than the original value. */
  70. //#define oo_BYTE_VECTOR_FROM(x,min,max) oo_VECTOR (signed char, i8, x, min, max)
  71. #define oo_INT_VECTOR_FROM(x,min,max) oo_VECTOR (int, i16, x, min, max)
  72. #define oo_INTEGER_VECTOR_FROM(x,min,max) oo_VECTOR (integer, integer32BE, x, min, max)
  73. #define oo_UBYTE_VECTOR_FROM(x,min,max) oo_VECTOR (unsigned char, u8, x, min, max)
  74. //#define oo_UINT_VECTOR_FROM(x,min,max) oo_VECTOR (unsigned int, u16, x, min, max)
  75. //#define oo_UINTEGER_VECTOR_FROM(x,min,max) oo_VECTOR (uinteger, u32, x, min, max)
  76. #define oo_FLOAT_VECTOR_FROM(x,min,max) oo_VECTOR (double, r32, x, min, max)
  77. #define oo_DOUBLE_VECTOR_FROM(x,min,max) oo_VECTOR (double, r64, x, min, max)
  78. #define oo_COMPLEX_VECTOR_FROM(x,min,max) oo_VECTOR (dcomplex, c128, x, min, max)
  79. //#define oo_BYTE_MATRIX_FROM(x,row1,row2,col1,col2) oo_MATRIX (signed char, i8, x, row1, row2, col1, col2)
  80. //#define oo_INT_MATRIX_FROM(x,row1,row2,col1,col2) oo_MATRIX (int, i16, x, row1, row2, col1, col2)
  81. //#define oo_INTEGER_MATRIX_FROM(x,row1,row2,col1,col2) oo_MATRIX (integer, integer32BE, x, row1, row2, col1, col2)
  82. //#define oo_UBYTE_MATRIX_FROM(x,row1,row2,col1,col2) oo_MATRIX (unsigned char, u8, x, row1, row2, col1, col2)
  83. //#define oo_UINT_MATRIX_FROM(x,row1,row2,col1,col2) oo_MATRIX (unsigned int, u16, x, row1, row2, col1, col2)
  84. //#define oo_UINTEGER_MATRIX_FROM(x,row1,row2,col1,col2) oo_MATRIX (uinteger, u32, x, row1, row2, col1, col2)
  85. //#define oo_FLOAT_MATRIX_FROM(x,row1,row2,col1,col2) oo_MATRIX (double, r32, x, row1, row2, col1, col2)
  86. #define oo_DOUBLE_MATRIX_FROM(x,row1,row2,col1,col2) oo_MATRIX (double, r64, x, row1, row2, col1, col2)
  87. //#define oo_COMPLEX_MATRIX_FROM(x,row1,row2,col1,col2) oo_MATRIX (dcomplex, c128, x, row1, row2, col1, col2)
  88. /* The same arrays, with the first index fixed at 1. */
  89. //#define oo_BYTE_VECTOR(x,n) oo_VECTOR (signed char, i8, x, 1, n)
  90. #define oo_INT_VECTOR(x,n) oo_VECTOR (int, i16, x, 1, n)
  91. #define oo_INTEGER_VECTOR(x,n) oo_VECTOR (integer, integer32BE, x, 1, n)
  92. //#define oo_UBYTE_VECTOR(x,n) oo_VECTOR (unsigned char, u8, x, 1, n)
  93. //#define oo_UINT_VECTOR(x,n) oo_VECTOR (unsigned int, u16, x, 1, n)
  94. //#define oo_UINTEGER_VECTOR(x,n) oo_VECTOR (uinteger, u32, x, 1, n)
  95. #define oo_FLOAT_VECTOR(x,n) oo_VECTOR (double, r32, x, 1, n)
  96. #define oo_DOUBLE_VECTOR(x,n) oo_VECTOR (double, r64, x, 1, n)
  97. //#define oo_COMPLEX_VECTOR(x,n) oo_VECTOR (dcomplex, c128, x, 1, n)
  98. #define oo_VEC(x,size) oo_ANYVEC (double, r64, x, size)
  99. #define oo_INTVEC(x,size) oo_ANYVEC (integer, integer32BE, x, size)
  100. #define oo_INTVEC16(x,size) oo_ANYVEC (integer, integer16BE, x, size)
  101. #define oo_obsoleteVEC32(x,size) oo_ANYVEC (double, r32, x, size)
  102. //#define oo_BYTE_MATRIX(x,nrow,ncol) oo_MATRIX (signed char, i8, x, 1, nrow, 1, ncol)
  103. //#define oo_INT_MATRIX(x,nrow,ncol) oo_MATRIX (int, i16, x, 1, nrow, 1, ncol)
  104. #define oo_INTEGER_MATRIX(x,nrow,ncol) oo_MATRIX (integer, integer32BE, x, 1, nrow, 1, ncol)
  105. #define oo_UBYTE_MATRIX(x,nrow,ncol) oo_MATRIX (unsigned char, u8, x, 1, nrow, 1, ncol)
  106. //#define oo_UINT_MATRIX(x,nrow,ncol) oo_MATRIX (unsigned int, u16, x, 1, nrow, 1, ncol)
  107. //#define oo_UINTEGER_MATRIX(x,nrow,ncol) oo_MATRIX (uinteger, u32, x, 1, nrow, 1, ncol)
  108. #define oo_FLOAT_MATRIX(x,nrow,ncol) oo_MATRIX (double, r32, x, 1, nrow, 1, ncol)
  109. #define oo_DOUBLE_MATRIX(x,nrow,ncol) oo_MATRIX (double, r64, x, 1, nrow, 1, ncol)
  110. //#define oo_COMPLEX_MATRIX(x,nrow,ncol) oo_MATRIX (dcomplex, c128, x, 1, nrow, 1, ncol)
  111. #define oo_MAT(x,nrow,ncol) oo_ANYMAT (double, r64, x, nrow, ncol)
  112. #define oo_INTMAT(x,nrow,ncol) oo_ANYMAT (integer, integer32BE, x, nrow, ncol)
  113. #define oo_obsoleteMAT32(x,nrow,ncol) oo_ANYMAT (double, r32, x, nrow, ncol)
  114. /*** Enumerated types. ***/
  115. /* The possible storage types give these binary formats: */
  116. /* e8: store as signed integer in 8 bits. */
  117. /* e16: store in signed big-endian integer in 16 bits. */
  118. /* eb: store as byte 170 (false) or byte 185 (true). */
  119. /* eq: store as byte 150 (no) or byte 165 (yes). */
  120. /* ex: store as byte 226 (absent) or byte 241 (present). */
  121. /* For text format, the value is written as a string between '<' and '>'. */
  122. #define oo_ENUM(kType,x) oo_ENUMx (kType, e8, x)
  123. //#define oo_LENUM(Type,x) oo_ENUMx (short, e16, Type, x)
  124. //#define oo_ENUM_SET(Type,x,setType) oo_ENUMx_SET (signed char, e8, Type, x, setType)
  125. //#define oo_LENUM_SET(Type,x,setType) oo_ENUMx_SET (short, e16, Type, x, setType)
  126. //#define oo_ENUM_VECTOR_FROM(Type,x,min,max) oo_ENUMx_VECTOR (signed char, e8, Type, x, min, max)
  127. //#define oo_LENUM_VECTOR_FROM(Type,x,min,max) oo_ENUMx_VECTOR (short, e16, Type, x, min, max)
  128. //#define oo_ENUM_VECTOR(Type,x,n) oo_ENUMx_VECTOR (signed char, e8, Type, x, 1, n)
  129. //#define oo_LENUM_VECTOR(Type,x,n) oo_ENUMx_VECTOR (short, e16, Type, x, 1, n)
  130. #define oo_BOOLEAN(x) oo_SIMPLE (bool, eb, x)
  131. //#define oo_BOOLEAN_VECTOR_FROM(x,min,max) oo_VECTOR (bool, eb, x, min, max)
  132. #define oo_BOOLEAN_VECTOR(x,n) oo_VECTOR (bool, eb, x, 1, n)
  133. #define oo_QUESTION(x) oo_SIMPLE (bool, eq, x)
  134. //#define oo_QUESTION_VECTOR_FROM(x,min,max) oo_VECTOR (bool, eq, x, min, max)
  135. //#define oo_QUESTION_VECTOR(x,n) oo_VECTOR (bool, eq, x, 1, n)
  136. /*** Strings. ***/
  137. /* The possible storage types give these binary formats: */
  138. /* s16: store as sequence of bytes, preceded with 16 bits (u16) to denote length. */
  139. /* w16: store as sequence of characters (u16), preceded with 16 bits (u16) to denote length. */
  140. /* s32: store as sequence of bytes, preceded with 32 bits (u32) to denote length. */
  141. /* w32: store as sequence of characters (u16), preceded with 32 bits (u32) to denote length. */
  142. #define oo_STRING(x) oo_STRINGx (w16, x)
  143. #define oo_LSTRING(x) oo_STRINGx (w32, x)
  144. //#define oo_STRING_SET(x,setType) oo_STRINGx_SET (w16, x, setType)
  145. //#define oo_LSTRING_SET(x,setType) oo_STRINGx_SET (w32, x, setType)
  146. #define oo_STRING_VECTOR(x,n) oo_STRINGx_VECTOR (w16, x, n)
  147. //#define oo_LSTRING_VECTOR(x,n) oo_STRINGx_VECTOR (w32, x, n)
  148. /*** Structs. ***/
  149. #define oo_STRUCT_VECTOR(Type,x,n) oo_STRUCT_VECTOR_FROM (Type, x, 1, n)
  150. #define oo_STRUCT_MATRIX(Type,x,nrow,ncol) oo_STRUCT_MATRIX_FROM (Type, x, 1, nrow, 1, ncol)
  151. /********** Definitions for header files only. **********/
  152. /* These are undef'ed and redefined in the header files that implement methods, */
  153. /* such as oo_DESTROY.h, oo_COPY.h, oo_EQUAL.h, oo_WRITE_TEXT.h, etc. */
  154. /* Types. */
  155. #define oo_SIMPLE(type,storage,x) type x;
  156. #define oo_SET(type,storage,x,setType) type x [1 + (int) setType::MAX];
  157. #define oo_VECTOR(type,storage,x,min,max) type *x;
  158. #define oo_ANYVEC(type,storage,x,size) autovector <type> x;
  159. #define oo_MATRIX(type,storage,x,row1,row2,col1,col2) type **x;
  160. #define oo_ANYMAT(type,storage,x,nrow,ncol) automatrix <type> x;
  161. #define oo_ENUMx(kType,storage,x) kType x;
  162. //#define oo_ENUMx_SET(kType,storage,x,setType) kType x [1 + (int) setType::MAX];
  163. //#define oo_ENUMx_VECTOR(kType,storage,x,min,max) kType *x;
  164. #define oo_STRINGx(storage,x) autostring32 x;
  165. #define oo_STRINGx_SET(storage,x,setType) autostring32 x [1 + setType::MAX];
  166. #define oo_STRINGx_VECTOR(storage,x,size) autostring32vector x;
  167. #define oo_STRUCT(Type,x) struct struct##Type x;
  168. #define oo_STRUCT_SET(Type,x,setType) struct struct##Type x [1 + (int) setType::MAX];
  169. #define oo_STRUCT_VECTOR_FROM(Type,x,min,max) Type x;
  170. #define oo_STRUCT_MATRIX_FROM(Type,x,row1,row2,col1,col2) struct struct##Type **x;
  171. #define oo_OBJECT(Class,version,x) auto##Class x;
  172. #define oo_COLLECTION_OF(Class,x,ItemClass,version) Class<struct##ItemClass> x;
  173. #define oo_COLLECTION(Class,x,ItemClass,version) auto##Class x;
  174. #define oo_FILE(x) structMelderFile x;
  175. #define oo_DIR(x) structMelderDir x;
  176. /* Struct and class definitions. */
  177. #define oo_DEFINE_STRUCT(T) \
  178. typedef struct struct##T *T; \
  179. struct struct##T {
  180. #define oo_END_STRUCT(T) \
  181. void destroy (); \
  182. void copy (T data_to); \
  183. bool equal (T otherData); \
  184. Data_Description description (); \
  185. static Data_Description s_description; \
  186. bool canWriteAsEncoding (int outputEncoding); \
  187. void writeText (MelderFile openFile); \
  188. void readText (MelderReadText text, int formatVersion); \
  189. void writeBinary (FILE *f); \
  190. void readBinary (FILE *f, int formatVersion); \
  191. };
  192. #define oo_DEFINE_CLASS(klas,parent) \
  193. typedef struct struct##klas *klas; \
  194. typedef autoSomeThing <struct##klas> auto##klas; \
  195. typedef struct##parent klas##_Parent; \
  196. extern struct structClassInfo theClassInfo_##klas; \
  197. extern ClassInfo class##klas; \
  198. struct struct##klas : public struct##parent {
  199. #define oo_END_CLASS(Class) \
  200. void v_destroy () noexcept override; \
  201. void v_copy (Daata data_to) override; \
  202. bool v_equal (Daata otherData) override; \
  203. static Data_Description s_description; \
  204. Data_Description v_description () override { return s_description; } \
  205. bool v_canWriteAsEncoding (int outputEncoding) override; \
  206. void v_writeText (MelderFile openFile) override; \
  207. void v_readText (MelderReadText text, int formatVersion) override; \
  208. void v_writeBinary (FILE *f) override; \
  209. void v_readBinary (FILE *f, int formatVersion) override; \
  210. };
  211. /*** Miscellaneous. ***/
  212. /* For fields that should not be read in older versions: */
  213. #define oo_FROM(from)
  214. #define oo_ENDFROM
  215. #define oo_VERSION_UNTIL(version) \
  216. if (_formatVersion_ < version) {
  217. #define oo_VERSION_ELSE_UNTIL(version) \
  218. } else if (_formatVersion_ < version) {
  219. #define oo_VERSION_ELSE \
  220. } else {
  221. #define oo_VERSION_END \
  222. }
  223. /* Examples of the usage of the following macros:
  224. For code that should only appear in reading methods (consistency checks etc.),
  225. use: #if oo_READING
  226. For code that should only appear in writing methods (e.g., debugging statements),
  227. use: #if oo_WRITING
  228. For fields that are not destroyed, copied, or compared, either (i.e., only declarations),
  229. use: #if oo_DECLARING
  230. For fields that do not take part in I/O (e.g. redundant or temporary fields),
  231. use: #if ! oo_READING && ! oo_WRITING
  232. For fields that should not be read (e.g., when expanding a database file),
  233. use: #if ! oo_READING
  234. For fields that should not be written (e.g., when shrinking a database file),
  235. use: #if ! oo_WRITING
  236. The last two should be temporary situations,
  237. because the output files do not match the input files!
  238. */
  239. #define oo_DECLARING 1
  240. #define oo_DESTROYING 0
  241. #define oo_COPYING 0
  242. #define oo_COMPARING 0
  243. #define oo_VALIDATING_ASCII 0
  244. #define oo_READING 0
  245. #define oo_READING_TEXT 0
  246. #define oo_READING_BINARY 0
  247. #define oo_WRITING 0
  248. #define oo_WRITING_TEXT 0
  249. #define oo_WRITING_BINARY 0
  250. #define oo_DESCRIBING 0
  251. /* End of file oo.h */
  252. #endif