forked from zeromq/zproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzproject_class.gsl
559 lines (504 loc) · 16.6 KB
/
zproject_class.gsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# Generate project API
#
# This is a code generator built using the iMatix GSL code generation
# language. See https://github.com/imatix/gsl for details.
#
# Copyright (c) the Contributors as noted in the AUTHORS file.
# This file is part of zproject.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
.macro generate_project_api
.#
.# Build the project header file
.#
.directory.create ("include")
.if !file.exists ("include/$(project.header:)") & count (class, class.name = project.name) = 0
. echo "Generating skeleton for include/$(project.header)"
. output "include/$(project.header)"
/* =========================================================================
$(project.name) - $(project.description?'':)
. for project.license
$(string.trim (license.):block )
. endfor
=========================================================================
*/
#ifndef $(PROJECT.HEADER:c)_H_INCLUDED
#define $(PROJECT.HEADER:c)_H_INCLUDED
// Include the project library file
#include "$(project.prefix)_library.h"
// Add your own public definitions here, if you need them
#endif
.endif
.output "include/$(project.prefix)_library.h"
/* =========================================================================
$(project.name) - generated layer of public API
. for project.license
$(string.trim (license.):block )
. endfor
$(project.GENERATED_WARNING_HEADER:)
=========================================================================
*/
#ifndef $(PROJECT.PREFIX)_LIBRARY_H_INCLUDED
#define $(PROJECT.PREFIX)_LIBRARY_H_INCLUDED
// Set up environment for the application
.if file.exists ("include/$(project.prelude)")
#include "$(project.prelude)"
.endif
// External dependencies
.for use where !defined (implied)
. if (use.optional = 1)
#if defined (HAVE_$(USE.LIBNAME))
#include <$(use.header)>
#endif
. else
#include <$(use.header)>
. endif
.endfor
// $(PROJECT.PREFIX) version macros for compile-time API detection
#define $(PROJECT.PREFIX)_VERSION_MAJOR $(project->version.major)
#define $(PROJECT.PREFIX)_VERSION_MINOR $(project->version.minor)
#define $(PROJECT.PREFIX)_VERSION_PATCH $(project->version.patch)
#define $(PROJECT.PREFIX)_MAKE_VERSION(major, minor, patch) \\
((major) * 10000 + (minor) * 100 + (patch))
#define $(PROJECT.PREFIX)_VERSION \\
$(PROJECT.PREFIX)_MAKE_VERSION($(PROJECT.PREFIX)_VERSION_MAJOR, $(PROJECT.PREFIX)_VERSION_MINOR, $(PROJECT.PREFIX)_VERSION_PATCH)
#if defined (__WINDOWS__)
# if defined $(PROJECT.PREFIX)_STATIC
# define $(PROJECT.PREFIX)_EXPORT
# elif defined $(PROJECT.PREFIX)_INTERNAL_BUILD
# if defined DLL_EXPORT
# define $(PROJECT.PREFIX)_EXPORT __declspec(dllexport)
# else
# define $(PROJECT.PREFIX)_EXPORT
# endif
# elif defined $(PROJECT.PREFIX)_EXPORTS
# define $(PROJECT.PREFIX)_EXPORT __declspec(dllexport)
# else
# define $(PROJECT.PREFIX)_EXPORT __declspec(dllimport)
# endif
# define $(PROJECT.PREFIX)_PRIVATE
#else
# define $(PROJECT.PREFIX)_EXPORT
# if (defined __GNUC__ && __GNUC__ >= 4) || defined __INTEL_COMPILER
# define $(PROJECT.PREFIX)_PRIVATE __attribute__ ((visibility ("hidden")))
# else
# define $(PROJECT.PREFIX)_PRIVATE
# endif
#endif
.if !project.stable
// Project has no stable classes, so we build the draft API
#undef $(PROJECT.PREFIX)_BUILD_DRAFT_API
#define $(PROJECT.PREFIX)_BUILD_DRAFT_API
.endif
// Opaque class structures to allow forward references
// These classes are stable or legacy and built in all releases
.for project.class where scope = "public" & !draft
typedef struct _$(class.c_name)_t $(class.c_name)_t;
#define $(CLASS.C_NAME)_T_DEFINED
.endfor
.for project.class where scope = "public" & draft
. if first ()
// Draft classes are by default not built in stable releases
#ifdef $(PROJECT.PREFIX)_BUILD_DRAFT_API
. endif
typedef struct _$(class.c_name)_t $(class.c_name)_t;
#define $(CLASS.C_NAME)_T_DEFINED
. if last ()
#endif // $(PROJECT.PREFIX)_BUILD_DRAFT_API
. endif
.endfor
.for constant
. resolve_c_constant (constant, "draft")
.endfor
.if count (constant, scope = "public")
// Public constants
.endif
.for constant where scope = "public" & !draft
#define $(CONSTANT.NAME)\
$(constant.value) // $(constant.?'')
.endfor
.for constant where scope = "public" & draft
. if first ()
#ifdef $(PROJECT.PREFIX)_BUILD_DRAFT_API
. endif
#define $(CONSTANT.NAME)\
$(constant.value) // $(constant.?'')
. if last ()
#endif // $(PROJECT.PREFIX)_BUILD_DRAFT_API
. endif
.endfor
// Public classes, each with its own header file
.for header where scope = "public"
#include "$(header.name).h"
.endfor
.for class where scope = "public" & class.c_name <> "$(project.name)" & !draft
#include "$(class.c_name).h"
.endfor
.for class where scope = "public" & class.c_name <> "$(project.name)" & draft
. if first ()
#ifdef $(PROJECT.PREFIX)_BUILD_DRAFT_API
. endif
#include "$(class.c_name).h"
. if last ()
#endif // $(PROJECT.PREFIX)_BUILD_DRAFT_API
. endif
.endfor
#endif
/*
$(project.GENERATED_WARNING_HEADER:)
*/
.#
.# Build the project selftest program
.#
.if project.use_cxx
.output "src/$(project.prefix)_selftest.cc"
.else
.output "src/$(project.prefix)_selftest.c"
.endif
/* =========================================================================
$(project.prefix)_selftest.c - run selftests
Runs all selftests.
-------------------------------------------------------------------------
. for project.license
$(string.trim (license.):block )
. endfor
$(project.GENERATED_WARNING_HEADER:)
=========================================================================
*/
#include "$(project.prefix)_classes.h"
typedef struct {
const char *testname;
void (*test) (bool);
} test_item_t;
static test_item_t
all_tests [] = {
.for class where !draft & selftest & private ?<> 1
{ "$(class.c_name)", $(class.c_name)_test },
.endfor
.for class where draft & selftest & private ?<> 1
. if first ()
#ifdef $(PROJECT.PREFIX)_BUILD_DRAFT_API
. endif
{ "$(class.c_name)", $(class.c_name)_test },
. if last ()
#endif // $(PROJECT.PREFIX)_BUILD_DRAFT_API
. endif
.endfor
{0, 0} // Sentinel
};
// -------------------------------------------------------------------------
// Test whether a test is available.
// Return a pointer to a test_item_t if available, NULL otherwise.
//
test_item_t *
test_available (const char *testname)
{
test_item_t *item;
for (item = all_tests; item->test; item++) {
if (streq (testname, item->testname))
return item;
}
return NULL;
}
// -------------------------------------------------------------------------
// Run all tests.
//
static void
test_runall (bool verbose)
{
test_item_t *item;
printf ("Running $(project.name) selftests...\\n");
for (item = all_tests; item->test; item++)
item->test (verbose);
printf ("Tests passed OK\\n");
}
int
main (int argc, char **argv)
{
bool verbose = false;
test_item_t *test = 0;
int argn;
for (argn = 1; argn < argc; argn++) {
if (streq (argv [argn], "--help")
|| streq (argv [argn], "-h")) {
puts ("$(project.prefix)_selftest.c [options] ...");
puts (" --verbose / -v verbose test output");
puts (" --number / -n report number of tests");
puts (" --list / -l list all tests");
puts (" --test / -t [name] run only test 'name'");
puts (" --continue / -c continue on exception (on Windows)");
return 0;
}
if (streq (argv [argn], "--verbose")
|| streq (argv [argn], "-v"))
verbose = true;
else
if (streq (argv [argn], "--number")
|| streq (argv [argn], "-n")) {
puts ("$(count (project.class))");
return 0;
}
else
if (streq (argv [argn], "--list")
|| streq (argv [argn], "-l")) {
puts ("Available tests:");
.for class
puts (" $(class.c_name)");
.endfor
return 0;
}
else
if (streq (argv [argn], "--test")
|| streq (argv [argn], "-t")) {
argn++;
if (argn >= argc) {
fprintf (stderr, "--test needs an argument\\n");
return 1;
}
test = test_available (argv [argn]);
if (!test) {
fprintf (stderr, "%s not valid, use --list to show tests\\n", argv [argn]);
return 1;
}
}
else
if (streq (argv [argn], "--continue")
|| streq (argv [argn], "-c")) {
#ifdef _MSC_VER
// When receiving an abort signal, only print to stderr (no dialog)
_set_abort_behavior (0, _WRITE_ABORT_MSG);
#endif
}
else {
printf ("Unknown option: %s\\n", argv [argn]);
return 1;
}
}
if (test) {
printf ("Running $(project.name) test '%s'...\\n", test->testname);
test->test (verbose);
}
else
test_runall (verbose);
return 0;
}
/*
$(project.GENERATED_WARNING_HEADER:)
*/
.-
.- Build the classes header for private header files and constants
.-
.output "src/$(project.prefix)_classes.h"
/* =========================================================================
$(project.prefix)_classes - private header file
. for project.license
$(string.trim (license.):block )
. endfor
$(project.GENERATED_WARNING_HEADER:)
=========================================================================
*/
#ifndef $(PROJECT.PREFIX)_CLASSES_H_INCLUDED
#define $(PROJECT.PREFIX)_CLASSES_H_INCLUDED
// Platform definitions, must come first
#include "platform.h"
// External API
#include "../include/$(project.header:)"
// Internal API
.for class where scope = "private"
#include "$(class.c_name).h"
.endfor
.for header where scope = "private"
#include "$(header.name).h"
.endfor
// *** To avoid double-definitions, only define if building without draft ***
#ifndef $(PROJECT.PREFIX)_BUILD_DRAFT_API
.for class where draft = 0
. for constant where draft = 1
. if first ()
// *** Draft constants, defined for internal use only ***
. endif
#define $(CLASS.NAME:c)_$(NAME:c) $(value) // $(constant.description:no,block)
. endfor
. for class.callback_type where draft = 1
. if first ()
// *** Draft callbacks, defined for internal use only ***
. endif
// $(callback_type.description:no,block)
$(c_callback_typedef (callback_type))
. endfor
. for . as method where draft = 1
. if name () = "constructor" | name () = "method" | name () = "destructor"
// *** Draft method, defined for internal use only ***
// $(.description:no,block)
. if ->return.fresh
// Caller owns return value and must destroy it when done.
. endif
$(PROJECT.PREFIX)_PRIVATE $(c_method_signature (method):)\
. if defined(method.format_index)
CHECK_PRINTF ($(method.format_index));
. else
;
. endif
. endif
. endfor
.endfor
#endif // $(PROJECT.PREFIX)_BUILD_DRAFT_API
.for constant where scope = "private"
. if first ()
// Private constants
. endif
#define $(CONSTANT.NAME)\t$(constant.value) // $(constant.?'')
.endfor
#endif
.close
.# Generate API interface
.macro method_state_comment (state)
. if my.state = "draft"
// *** Draft method, for development use, may change without warning ***
. elsif my.state = "legacy"
// *** Deprecated method, slated for removal: avoid using it ***
. endif
.endmacro
.macro generate_interface (draft)
. for class.constant where draft = my.draft
#define $(CLASS.NAME:c)_$(NAME:c) $(value) // $(constant.description:no,block)
. if last ()
. endif
. endfor
. for class.callback_type where draft = my.draft
// $(callback_type.description:no,block)
$(c_callback_typedef (callback_type))
. endfor
. for class.constructor where draft = my.draft
. method_state_comment (state)
// $(constructor.description:no,block)
$(PROJECT.PREFIX)_EXPORT $(c_method_signature (constructor):)\
. if defined(constructor.format_index)
CHECK_PRINTF ($(constructor.format_index));
. else
;
. endif
. endfor
. for class.destructor where draft = my.draft
. method_state_comment (state)
// $(destructor.description:no,block)
$(PROJECT.PREFIX)_EXPORT $(c_method_signature (destructor):)\
. if defined(destructor.format_index)
CHECK_PRINTF ($(destructor.format_index));
. else
;
. endif
. endfor
. for class.actor where draft = my.draft
// $(actor.description:no,block)
$(PROJECT.PREFIX)_EXPORT void
$(actor.name:c) (zsock_t *pipe, void *args);
. endfor
. for class.method where draft = my.draft
. method_state_comment (state)
// $(method.description:no,block)
. if method->return.fresh
// Caller owns return value and must destroy it when done.
. endif
$(PROJECT.PREFIX)_EXPORT $(c_method_signature (method):)\
. if defined(method.format_index)
CHECK_PRINTF ($(method.format_index));
. else
;
. endif
. endfor
.endmacro
.template 0
#
# Regenerate the @interface for the class if it has an API model
#
for class
skeleton_class_header ()
skeleton_class_source ()
if !defined (class.api)
resolve_c_class (class)
endif
if defined (class.api)
handle = file.open (class.header, "ra")
current_loc = "prefix"
error = # undefined
HEADER_FILE_PREFIX = "" # All lines preceding the @interface line
HEADER_FILE_SUFFIX = "" # All lines following the @end line
# Read each line from the file and pull out the prefix and suffix blocks
while defined (handle)
this_line = handle.read (error)? ""
if defined (error)
handle.close ()
handle = # undefined
elsif current_loc = "prefix"
if regexp.match ("//\\s*@interface", this_line)
current_loc = "interface"
elsif regexp.match ("//\\s*@warning", this_line)
# ignore the warning line - do not copy it
else
HEADER_FILE_PREFIX += this_line
endif
elsif current_loc = "interface"
if regexp.match ("//\\s*@end", this_line)
current_loc = "suffix"
endif
elsif current_loc = "suffix"
HEADER_FILE_SUFFIX += this_line
endif
endwhile
# Now output the file with stored prefix, generated interface,
# and stored suffix
output class.header
template 1
$(HEADER_FILE_PREFIX:)\
// @warning THE FOLLOWING @INTERFACE BLOCK IS AUTO-GENERATED BY ZPROJECT
// @warning Please edit the model at "$(class.api)" to make changes.
// @interface
. if !project.stable
// This API is a draft, and may change without notice.
. elsif class.state = "draft"
// This is a draft class, and may change without notice. It is disabled in
// stable builds by default. If you use this in applications, please ask
// for it to be pushed to stable state. Use --enable-drafts to enable.
. elsif class.state = "stable"
// This is a stable class, and may not change except for emergencies. It
// is provided in stable builds.
. if count (class.method, method.state = "draft")
// This class has draft methods, which may change over time. They are not
// in stable releases, by default. Use --enable-drafts to enable.
. endif
. if count (class.method, method.state = "legacy")
// This class has legacy methods, which will be removed over time. You
// should not use them, and migrate any code that is still using them.
. endif
. elsif class.state = "legacy"
// This is a deprecated class, and will be removed over time. It is
// provided in stable builds to support old applications. You should
// stop using this class, and migrate any code that is still using it.
. endif
. generate_interface (0)
. if count (class., count.draft ?= 1)
#ifdef $(PROJECT.PREFIX)_BUILD_DRAFT_API
. endif
. generate_interface (1)
. if count (class., count.draft ?= 1)
#endif // $(PROJECT.PREFIX)_BUILD_DRAFT_API
. endif
// @end
$(HEADER_FILE_SUFFIX:)\
. endtemplate
close
endif
endfor
.endtemplate
.endmacro
if count (class)
for project.class
if !defined (class.api)
resolve_c_class (class)
endif
endfor
generate_project_api ()
endif