-
Notifications
You must be signed in to change notification settings - Fork 104
/
zproject_skeletons.gsl
487 lines (399 loc) · 12.8 KB
/
zproject_skeletons.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
# Generate skeletons for classes, actors, and mains
#
# This is a code generator built using the iMatix GSL code generation
# language. See https://github.com/zeromq/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/.
## Generate main source if not already present
.directory.create ('src')
.directory.create ('include')
.macro skeleton_main_source
. if !file.exists (main.source)
. echo "Generating skeleton for $(main.source)"
. output main.source
/* =========================================================================
$(main.name:$(project.filename_prettyprint)) - $(string.trim (main.?'description'):no,left)
. for project.license
$(string.trim (license.):block )
. endfor
=========================================================================
*/
/*
@header
$(main.name:$(project.filename_prettyprint)) - $(string.trim (main.?''):no,left)
@discuss
@end
*/
#include "$(project.prefix)_classes.$(project.header_ext)"
int main (int argc, char *argv [])
{
bool verbose = false;
int argn;
for (argn = 1; argn < argc; argn++) {
if (streq (argv [argn], "--help")
|| streq (argv [argn], "-h")) {
puts ("$(main.name) [options] ...");
puts (" --verbose / -v verbose test output");
puts (" --help / -h this information");
return 0;
}
else
if (streq (argv [argn], "--verbose")
|| streq (argv [argn], "-v"))
verbose = true;
else {
printf ("Unknown option: %s\\n", argv [argn]);
return 1;
}
}
// Insert main code here
if (verbose)
zsys_info ("$(main.name:$(project.filename_prettyprint)) - $(string.trim (main.?''):no,left)");
return 0;
}
. close
. else
. echo "NOT regenerating an existing $(main.source) skeleton file; but you probably should not care"
. endif
.endmacro
## Generate actor header and source if not already present
.macro skeleton_actor_header
. if !file.exists (actor.header)
. echo "Generating skeleton for $(actor.header)"
. output actor.header
/* =========================================================================
$(actor.name:$(project.filename_prettyprint)) - $(string.trim (actor.?''):no,left)
. for project.license
$(string.trim (license.):block )
. endfor
=========================================================================
*/
#ifndef $(ACTOR.NAME:c)_H_INCLUDED
#define $(ACTOR.NAME:c)_H_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
. if actor.scope = "private"
typedef struct _$(actor.name:c)_t $(actor.name:c)_t;
. endif
// @interface
// Create new $(actor.name:c) actor instance.
// @TODO: Describe the purpose of this actor!
//
// zactor_t *$(actor.name:c) = zactor_new ($(actor.name:c), NULL);
//
// Destroy $(actor.name:c) instance.
//
// zactor_destroy (&$(actor.name:c));
//
// Enable verbose logging of commands and activity:
//
// zstr_send ($(actor.name:c), "VERBOSE");
//
// Start $(actor.name:c) actor.
//
// zstr_sendx ($(actor.name:c), "START", NULL);
//
// Stop $(actor.name:c) actor.
//
// zstr_sendx ($(actor.name:c), "STOP", NULL);
//
// This is the $(actor.name:c) constructor as a zactor_fn;
$(PROJECT.PREFIX)_EXPORT void
$(actor.name:c)_actor (zsock_t *pipe, void *args);
// Self test of this actor
$(PROJECT.PREFIX)_EXPORT void
$(actor.name:c)_test (bool verbose);
// @end
#ifdef __cplusplus
}
#endif
#endif
. close
. else
. echo "NOT regenerating an existing $(actor.header) skeleton file; but you probably should not care"
. endif
.endmacro
.macro skeleton_actor_source
. if !file.exists (actor.source)
. echo "Generating skeleton for $(actor.source)"
. output actor.source
/* =========================================================================
$(actor.name:c) - $(string.trim (actor.?''):no,left)
. for project.license
$(string.trim (license.):block )
. endfor
=========================================================================
*/
/*
@header
$(actor.name:c) - $(string.trim (actor.?''):no,left)
@discuss
@end
*/
#include "$(project.prefix)_classes.$(project.header_ext)"
// Structure of our actor
struct _$(actor.name:c)_t {
zsock_t *pipe; // Actor command pipe
zpoller_t *poller; // Socket poller
bool terminated; // Did caller ask us to quit?
bool verbose; // Verbose logging enabled?
// TODO: Declare properties
};
// --------------------------------------------------------------------------
// Create a new $(actor.name:c) instance
static $(actor.name:c)_t *
$(actor.name:c)_new (zsock_t *pipe, void *args)
{
$(actor.name:c)_t *self = ($(actor.name:c)_t *) zmalloc (sizeof ($(actor.name:c)_t));
assert (self);
self->pipe = pipe;
self->terminated = false;
self->poller = zpoller_new (self->pipe, NULL);
// TODO: Initialize properties
return self;
}
// --------------------------------------------------------------------------
// Destroy the $(actor.name:c) instance
static void
$(actor.name:c)_destroy ($(actor.name:c)_t **self_p)
{
assert (self_p);
if (*self_p) {
$(actor.name:c)_t *self = *self_p;
// TODO: Free actor properties
// Free object itself
zpoller_destroy (&self->poller);
free (self);
*self_p = NULL;
}
}
// Start this actor. Return a value greater or equal to zero if initialization
// was successful. Otherwise -1.
static int
$(actor.name:c)_start ($(actor.name:c)_t *self)
{
assert (self);
// TODO: Add startup actions
return 0;
}
// Stop this actor. Return a value greater or equal to zero if stopping
// was successful. Otherwise -1.
static int
$(actor.name:c)_stop ($(actor.name:c)_t *self)
{
assert (self);
// TODO: Add shutdown actions
return 0;
}
// Here we handle incoming message from the node
static void
$(actor.name:c)_recv_api ($(actor.name:c)_t *self)
{
// Get the whole message of the pipe in one go
zmsg_t *request = zmsg_recv (self->pipe);
if (!request)
return; // Interrupted
char *command = zmsg_popstr (request);
if (streq (command, "START"))
$(actor.name:c)_start (self);
else
if (streq (command, "STOP"))
$(actor.name:c)_stop (self);
else
if (streq (command, "VERBOSE"))
self->verbose = true;
else
if (streq (command, "$TERM"))
// The $TERM command is send by zactor_destroy() method
self->terminated = true;
else {
zsys_error ("invalid command '%s'", command);
assert (false);
}
zstr_free (&command);
zmsg_destroy (&request);
}
// --------------------------------------------------------------------------
// This is the actor which runs in its own thread.
void
$(actor.name:c)_actor (zsock_t *pipe, void *args)
{
$(actor.name:c)_t * self = $(actor.name:c)_new (pipe, args);
if (!self)
return; // Interrupted
// Signal actor successfully initiated
zsock_signal (self->pipe, 0);
while (!self->terminated) {
zsock_t *which = (zsock_t *) zpoller_wait (self->poller, 0);
if (which == self->pipe)
$(actor.name:c)_recv_api (self);
// Add other sockets when you need them.
}
$(actor.name:c)_destroy (&self);
}
. if actor.selftest
// --------------------------------------------------------------------------
// Self test of this actor.
// If your selftest reads SCMed fixture data, please keep it in
// src/selftest-ro; if your test creates filesystem objects, please
// do so under src/selftest-rw.
// The following pattern is suggested for C selftest code:
// char *filename = NULL;
// filename = zsys_sprintf ("%s/%s", SELFTEST_DIR_RO, "mytemplate.file");
// assert (filename);
// ... use the "filename" for I/O ...
// zstr_free (&filename);
// This way the same "filename" variable can be reused for many subtests.
#define SELFTEST_DIR_RO "src/selftest-ro"
#define SELFTEST_DIR_RW "src/selftest-rw"
void
$(actor.name:c)_test (bool verbose)
{
printf (" * $(actor.name:c): ");
// @selftest
// Simple create/destroy test
zactor_t *$(actor.name:c) = zactor_new ($(actor.name:c)_actor, NULL);
assert ($(actor.name:c));
zactor_destroy (&$(actor.name:c));
// @end
printf ("OK\\n");
}
. endif
. close
. else
. echo "NOT regenerating an existing $(actor.source) skeleton file; but you probably should not care"
. endif
.endmacro
## Generate class header and source if not already present
.macro skeleton_class_header
. if !file.exists (class.header)
. echo "Generating skeleton for $(class.header)"
. output class.header
/* =========================================================================
$(class.c_name) - $(string.trim (class.?'class description'):no,left)
. for project.license
$(string.trim (license.):block )
. endfor
=========================================================================
*/
#ifndef $(CLASS.C_NAME:c)_H_INCLUDED
#define $(CLASS.C_NAME:c)_H_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
. if class.c_name = project.name
// Include the library file with typdefs, public includes and public constants
#include "$(class.name)_library.$(project.header_ext)"
. endif
// @interface
. if class.scope = "private"
. visibility = "$(PROJECT.PREFIX)_PRIVATE"
. else
. visibility = "$(PROJECT.PREFIX)_EXPORT"
. endif
// Create a new $(class.c_name)
$(VISIBILITY) $(class.c_name)_t *
$(class.c_name)_new (void);
// Destroy the $(class.c_name)
$(VISIBILITY) void
$(class.c_name)_destroy ($(class.c_name)_t **self_p);
. if class.selftest
// Self test of this class
$(VISIBILITY) void
$(class.c_name)_test (bool verbose);
. endif
// @end
#ifdef __cplusplus
}
#endif
#endif
. close
. else
. echo "NOT regenerating an existing $(class.header) skeleton file; but you probably should not care"
. endif
.endmacro
.macro skeleton_class_source
. if !file.exists (class.source)
. echo "Generating skeleton for $(class.source)"
. output class.source
/* =========================================================================
$(class.c_name) - $(string.trim (class.?'class description'):no,left)
. for project.license
$(string.trim (license.):block )
. endfor
=========================================================================
*/
/*
@header
$(class.c_name) - $(string.trim (class.?''):no,left)
@discuss
@end
*/
#include "$(project.prefix)_classes.$(project.header_ext)"
// Structure of our class
struct _$(class.c_name:)_t {
int filler; // Declare class properties here
};
// --------------------------------------------------------------------------
// Create a new $(class.c_name)
$(class.c_name:)_t *
$(class.c_name:)_new (void)
{
$(class.c_name:)_t *self = ($(class.c_name:)_t *) zmalloc (sizeof ($(class.c_name:)_t));
assert (self);
// Initialize class properties here
return self;
}
// --------------------------------------------------------------------------
// Destroy the $(class.c_name)
void
$(class.c_name:)_destroy ($(class.c_name:)_t **self_p)
{
assert (self_p);
if (*self_p) {
$(class.c_name:)_t *self = *self_p;
// Free class properties here
// Free object itself
free (self);
*self_p = NULL;
}
}
. if class.selftest
// --------------------------------------------------------------------------
// Self test of this class
// If your selftest reads SCMed fixture data, please keep it in
// src/selftest-ro; if your test creates filesystem objects, please
// do so under src/selftest-rw.
// The following pattern is suggested for C selftest code:
// char *filename = NULL;
// filename = zsys_sprintf ("%s/%s", SELFTEST_DIR_RO, "mytemplate.file");
// assert (filename);
// ... use the "filename" for I/O ...
// zstr_free (&filename);
// This way the same "filename" variable can be reused for many subtests.
#define SELFTEST_DIR_RO "src/selftest-ro"
#define SELFTEST_DIR_RW "src/selftest-rw"
void
$(class.c_name:)_test (bool verbose)
{
printf (" * $(class.c_name): ");
// @selftest
// Simple create/destroy test
$(class.c_name:)_t *self = $(class.c_name:)_new ();
assert (self);
$(class.c_name:)_destroy (&self);
// @end
printf ("OK\\n");
}
. endif
. close
. else
. echo "NOT regenerating an existing $(class.source) skeleton file; but you probably should not care"
. endif
.endmacro