This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
callback.h
4295 lines (3992 loc) · 171 KB
/
callback.h
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if 0 /* -*-perl-*- */
# /*
# $Id: callback.h,v 1.22 2006/07/20 03:21:43 max Exp $
#
# This header file was automatically generated by itself.
# (Yup, it's a C++ header file and a perl script.)
#
# Copyright (C) 1998-2004 David Mazieres (dm@uun.org)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
#
# Note: For the purposes of the GNU General Public License, source
# includes the source to the perl script, not just the resulting
# header file, callback.h. However, since the perl script is
# conveniently embedded in the header file, you needn't do anything
# special to distribute source as long as you don't take the perl out
# of the header.
#
# WHAT IS THIS?
#
# This C++ header file defines the template class callback--a type
# that approximates function currying.
#
# An object of type callback<R, B1, B2> contains a member R operator()
# (B1, B2)--Thus callbacks are function objects with the first
# template type specifying the return of the function and the
# remaining arguments specifying the types of the arguments to pass
# the function.
#
# Callbacks can have any number of arguments up to $NA below.
# Template arguments that aren't specified default to void and don't
# need to be passed in. Thus, a callback<int> acts like a function
# with signature "int fn ()", a callback<int, char *> acts like a
# function with signature "int fn (char *)", and so forth.
#
# Each callback class has two type members, ptr and ref, specyfing
# refcounted pointers and references to the callback object
# respectively. (See refcnt.h for an explanation of ptr and ref).
#
# The function wrap is used to create references to callbacks. Given
# a function with signature "R fn (A1, A2, A3)", wrap can generate
# the following references:
#
# wrap (fn) -> callback<R, A1, A2, A3>::ref
# wrap (fn, a1) -> callback<R, A2, A3>::ref
# wrap (fn, a1, a2) -> callback<R, A3>::ref
# wrap (fn, a1, a2, a3) -> callback<R>::ref
#
# When the resulting callback is actually called, it invokes fn. The
# argument list it passes fn starts with whatever arguments were
# initially passed to wrap and then contains whatever arguments are
# given to the callback object. For example, given fn above, this
# code ends up calling "fn (a1, a2, a3)" and assigning the return
# value to r;
#
# R r;
# A1 a1;
# A2 a2;
# A3 a3;
# callback<R, A1>::ptr cb;
#
# cb = wrap (fn, a1);
# r = (*cb) (a2, a3);
#
# One can create callbacks from class methods as well as global
# functions. To do this, simply pass the object as the first
# parameter to wrap, and the method as the second. For example:
#
# struct foo {
# void bar (int, char *);
# callback<void, char *>::ref baz () {
# return wrap (this, &foo::bar, 7);
# }
# };
#
# Note the only way to generate pointers to class members in ANSI C++
# is with fully qualified member names. "&foo::bar" cannot be
# abbreviated to "&bar" in the above example, though most C++
# compilers still accept that syntax.
#
# If wrap is called with a refcounted ref or ptr to a object, instead
# of a simple pointer, the resulting callback will maintain a
# reference to the object, ensuring it is not deleted. For example,
# in the following code, baz returns a callback with a reference to
# the current object. This ensures that a foo will not be deleted
# until after the callback has been deleted. Without mkref, if a
# callback happened after the reference count on a foo object went to
# zero, the foo object would previously have been deleted and its
# vtable pointer likely clobbered, resulting in a core dump.
#
# struct foo : public virtual refcount {
# virtual void bar (int, char *);
# callback<void, char *>::ref baz () {
# return wrap (mkref (this), &foo::bar, 7);
# }
# };
#
# An example:
#
# void
# printstrings (char *a, char *b, char *c)
# {
# printf ("%s %s %s\n", a, b, c);
# }
#
# int
# main ()
# {
# callback<void, char *>::ref cb1 = wrap (printstrings, "cb1a", "cb1b");
# callback<void, char *, char *>::ref cb2 = wrap (printstrings, "cb2a");
# callback<void, char *, char *, char *>::ref cb3 = wrap (printstrings);
#
# (*cb1) ("cb1c"); // prints: cb1a cb1b cb1c
# (*cb2) ("cb2b", "cb2c"); // prints: cb2a cb2b cb2c
# (*cb3) ("cb3a", "cb3b", "cb3c"); // prints: cb3a cb3b cb3c
#
# return 0;
# }
eval 'exec perl -w -S $0 ${1+"$@"}'
if 0;
use strict;
my $NA = 5;
my $NB = 3;
my $PARTIALSPEC = 0; # 0 May work around compiler bugs
my $RETURNVOID = 1; # 0 May work around compiler bugs
my $verbosedestruct = 0;
my ($ia, $ib);
my $enddebug = '#endif /' . '* WRAP_DEBUG *' . '/';
my $elsenotdebug = '#else /' . '* !WRAP_DEBUG *' . '/';
my $endnotdebug = '#endif /' . '* !WRAP_DEBUG *' . '/';
my $bc = '/' . '*';
my $ec = '*' . '/';
sub jc {
join (', ', @_);
}
sub mklist ($$) {
my ($pat, $n) = @_;
my @res = map { my $ret = $pat; $ret =~ s/%/$_/g; $ret; } (1 .. $n);
wantarray ? @res : jc (@res);
}
sub pcallback ($) {
my $b = shift;
my $tmpparam = jc ('class R', mklist ('class B%', $b));
my $tmpargs = jc ('R', mklist ('B%', $b));
my $specargs = $b == $NB ? '' : "<" . $tmpargs . ">";
my $cbdecl = mklist ('B% b%', $b);
my $cbargs = mklist ('b%', $b);
print <<"EOF";
template<$tmpparam>
class callback$specargs {
public:
typedef struct ref<callback<$tmpargs> > ref;
typedef struct ptr<callback<$tmpargs> > ptr;
const char *_closure_type;
const void *_closure_void;
#if WRAP_DEBUG
const char *const dest;
const char *const src;
const char *const line;
callback (const char *df, const char *f, const char *l)
: dest (df[0] == '&' ? df + 1 : df), src (f), line (l) {}
$enddebug
virtual R operator() ($cbdecl) = 0;
virtual void trigger ($cbdecl) { (void) (*this) ($cbargs); }
virtual ~callback () {}
callback () : _closure_type (NULL), _closure_void (NULL) {}
void set_gdb_info (const char *typ, const void *v)
{ _closure_type = typ; _closure_void = v; }
};
EOF
return if (!$PARTIALSPEC);
my $rbparam = jc ('class R', mklist ('class B%', $b));
my $bargs = jc (mklist ('const B% &b%', $b));
my $blist = jc (mklist ('b%', $b));
print <<"EOF";
template<$rbparam>
class refops<callback<$tmpargs> >
typedef callback<$tmpargs> cb_t;
REFOPS_DEFAULT (cb_t);
R operator() ($bargs) const
{ return (*p) ($blist); }
};
EOF
return if ($RETURNVOID);
my $bparam = mklist ('class B%', $b);
$tmpargs = jc ('void', mklist ('B%', $b));
print <<"EOF";
template<$bparam>
class refops<callback<$tmpargs> >
typedef callback<$tmpargs> cb_t;
REFOPS_DEFAULT (cb_t);
void operator() ($bargs) const
{ (*p) ($blist); }
};
EOF
}
sub pcallback_b_a ($$) {
my ($b, $a) = @_;
my $type = "callback_${b}_${a}";
my $tmpparam = jc ('class R', mklist ('class B%', $b),
mklist ('class A%', $a));
my $RBlist = jc ('R', mklist ('B%', $b));
my $ABlist = jc (mklist ('A%', $a), mklist ('B%', $b));
my $adecl = join ('', mklist (" A% a%;\n", $a));
my $aargs = jc ('cb_t ff', mklist ('const A% &aa%', $a));
my $ainit = jc ('f (ff)', mklist ('a% (aa%)', $a));
my $bargs = mklist ('B% b%', $b);
my $ablist = jc (mklist ('a%', $a), mklist ('b%', $b));
print <<"EOF";
template<$tmpparam>
class $type
: public callback<$RBlist> {
typedef R (*cb_t) ($ABlist);
cb_t f;
${adecl}public:
$type (callback_line_param $aargs)
: callback_line_init (callback<$RBlist>) $ainit {}
R operator() ($bargs)
{ return f ($ablist); }
};
EOF
return if ($RETURNVOID);
my $specparam = jc (mklist ('class B%', $b), mklist ('class A%', $a));
my $specargs = jc ('void', mklist ('B%', $b), mklist ('A%', $a));
$RBlist = jc ('void', mklist ('B%', $b));
print <<"EOF";
template<$specparam>
class $type<$specargs>
: public callback<$RBlist> {
typedef void (*cb_t) ($ABlist);
cb_t f;
${adecl}public:
$type (callback_line_param $aargs)
: callback_line_init (callback<$RBlist>) $ainit {}
void operator() ($bargs)
{ f ($ablist); }
};
EOF
}
sub pcallback_c_b_a ($$$) {
my ($b, $a, $const) = @_;
my $type = "callback_c_${b}_${a}";
$type .= "_const" if ($const);
my $tmpparam = jc ('class P, class C, class R', mklist ('class B%', $b),
mklist ('class A%', $a));
my $RBlist = jc ('R', mklist ('B%', $b));
my $ABlist = jc (mklist ('A%', $a), mklist ('B%', $b));
my $adecl = join ('', mklist (" A% a%;\n", $a));
my $aargs = jc ('const P &cc, cb_t ff', mklist ('const A% &aa%', $a));
my $ainit = jc ('c (cc), f (ff)', mklist ('a% (aa%)', $a));
my $bargs = mklist ('B% b%', $b);
my $ablist = jc (mklist ('a%', $a), mklist ('b%', $b));
print <<"EOF";
template<$tmpparam>
class $type
: public callback<$RBlist> {
typedef R (C::*cb_t) ($ABlist) $const;
P c;
cb_t f;
#if WRAP_USE_NODELETE
${adecl} int deleted;
public:
$type (callback_line_param $aargs)
: callback_line_init (callback<$RBlist>) $ainit,
deleted (false)
{ maybe_nodelete_addptr (c, this->line, &deleted); }
~$type () { maybe_nodelete_remptr (c, this->line, &deleted); }
R operator() ($bargs)
{
if (deleted)
panic ("callback from %s to %s on deleted object\\n",
this->line, this->dest);
return ((*c).*f) ($ablist);
}
#else $bc !WRAP_USE_NODELETE $ec
${adecl}public:
$type (callback_line_param $aargs)
: callback_line_init (callback<$RBlist>) $ainit {}
R operator() ($bargs)
{ return ((*c).*f) ($ablist); }
#endif $bc !WRAP_USE_NODELETE $ec
};
EOF
return if ($RETURNVOID);
my $specparam = jc ('class P, class C', mklist ('class B%', $b),
mklist ('class A%', $a));
my $specargs = jc ('P, C, void' , mklist ('B%', $b), mklist ('A%', $a));
$RBlist = jc ('void', mklist ('B%', $b));
print <<"EOF";
template<$specparam>
class $type<$specargs>
: public callback<$RBlist> {
typedef void (C::*cb_t) ($ABlist);
P c;
cb_t f;
${adecl}public:
$type (callback_line_param $aargs)
: callback_line_init (callback<$RBlist>) $ainit {}
void operator() ($bargs)
{ ((*c).*f) ($ablist); }
};
EOF
}
sub pwrap_b_a ($$) {
my ($b, $a) = @_;
my $tmpparam = jc ('class R', mklist ('class B%', $b),
mklist ('class A%, class AA%', $a));
my $rtargs = jc ('R', mklist ('B%', $b), mklist ('A%', $a));
my $rtype = "refcounted<callback_${b}_${a}<$rtargs> >";
my $ABlist = jc (mklist ('A%', $a), mklist ('B%', $b));
my $fargs = jc ("R (*f) ($ABlist)", mklist ('const AA% &a%', $a));
my $falist = jc ('f', mklist ('a%', $a));
print <<"EOF";
template<$tmpparam>
static inline $rtype *
wrap (wrap_line_param $fargs)
{
return wNew ($rtype) (wrap_line_arg $falist);
}
EOF
}
sub pwrap_c_b_a ($$$) {
my ($b, $a, $const) = @_;
my $typesuffix = $const ? '_const' : '';
my $tmpparam = jc ('class C', 'class R', mklist ('class B%', $b),
mklist ('class A%, class AA%', $a));
my $rtargs = jc ("$const C *", 'C', 'R', mklist ('B%', $b),
mklist ('A%', $a));
my $rtype = "refcounted<callback_c_${b}_${a}${typesuffix}<$rtargs> >";
my $ABlist = jc (mklist ('A%', $a), mklist ('B%', $b));
my $fargs = jc ("$const C *p, R (C::*f) ($ABlist) $const", mklist ('const AA% &a%', $a));
my $falist = jc ('p, f', mklist ('a%', $a));
print <<"EOF";
template<$tmpparam>
static inline $rtype *
wrap (wrap_line_param $fargs)
{
return wNew ($rtype) (wrap_c_line_arg $falist);
}
EOF
$rtargs = jc ("ref<$const C>", "$const C", 'R', mklist ('B%', $b), mklist ('A%', $a));
$rtype = "refcounted<callback_c_${b}_${a}${typesuffix}<$rtargs> >";
$fargs = jc ("const ref<$const C> &p, R (C::*f) ($ABlist) $const",
mklist ('const AA% &a%', $a));
my $f2args = jc ("const ptr<$const C> &p, R (C::*f) ($ABlist) $const",
mklist ('const AA% &a%', $a));
my $f3args = jc ("const refcounted<$const C> *p, R (C::*f) ($ABlist) $const",
mklist ('const AA% &a%', $a));
$falist = jc ('p, f', mklist ('a%', $a));
print <<"EOF";
template<$tmpparam>
static inline $rtype *
wrap (wrap_line_param $fargs)
{
return wNew ($rtype) (wrap_c_line_arg $falist);
}
template<$tmpparam>
static inline $rtype *
wrap (wrap_line_param $f2args)
{
return wNew ($rtype) (wrap_c_line_arg $falist);
}
template<$tmpparam>
static inline $rtype *
wrap (wrap_line_param $f3args)
{
return wNew ($rtype) (wrap_c_line_arg $falist);
}
EOF
}
sub prefops () {
return if ($PARTIALSPEC);
my ($b, $opfn);
$opfn = "R operator() () const { return (*p) (); }\n";
for ($b = 1; $b <= $NB; $b++) {
my $tmpparam = mklist ("class BB%", $b);
my $bparam = mklist ("const BB% &b%", $b);
my $blist = mklist ("b%", $b);
$opfn .= sprintf <<"EOF",
template<$tmpparam>
R operator() ($bparam) const
{ return (*p) ($blist); }
EOF
}
printf <<"EOF",
template<%s>
class refops<callback<%s> > {
typedef callback<%s> cb_t;
REFOPS_DEFAULT (cb_t);
$opfn};
EOF
jc ('class R', mklist ("class B%", $NB)),
jc ('R', mklist ("B%", $NB)),
jc ('R', mklist ("B%", $NB));
return if ($RETURNVOID);
$opfn = "void operator() () { return (*p) (); }\n";
for ($b = 1; $b <= $NB; $b++) {
my $tmpparam = mklist ("class BB%", $b);
my $bparam = mklist ("const BB% &b%", $b);
my $blist = mklist ("b%", $b);
$opfn .= sprintf <<"EOF",
template<$tmpparam>
void operator() ($bparam)
{ (*p) ($blist); }
EOF
}
printf <<"EOF",
template<%s>
class refops<callback<%s> > {
typedef callback<%s> cb_t;
REFOPS_DEFAULT (cb_t);
$opfn};
EOF
scalar mklist ("class B%", $NB),
jc ('void', mklist ("B%", $NB)),
jc ('void', mklist ("B%", $NB));
}
sub pfile () {
printf "template<%s> class callback;\n",
jc ('class R', mklist ('class B% = void', $NB));
my ($a, $b);
for ($b = 0; $b <= $NB; $b++) {
pcallback ($b);
for ($a = 0; $a <= $NA; $a++) {
if ($a > 3) {
print "#ifndef SFS_CALLBACK_A3\n";
}
pcallback_b_a ($b, $a);
pwrap_b_a ($b, $a);
pcallback_c_b_a ($b, $a, '');
pcallback_c_b_a ($b, $a, 'const');
pwrap_c_b_a ($b, $a, '');
pwrap_c_b_a ($b, $a, 'const');
if ($a > 3) {
print "#endif // SFS_CALLBACK_A3\n\n";
}
}
}
prefops;
}
seek DATA, 0, 0;
while (<DATA>) {
print;
last if m/^\#endif \/\* perl \*\/$/;
}
# Note: The debugging feature requires GCC varargs macros
print <<'EOF';
#ifndef _CALLBACK1_H_INCLUDED_
#define _CALLBACK1_H_INCLUDED_ 1
#include "refcnt.h"
#ifndef WRAP_DEBUG
# if defined (DMALLOC) && __GNUC__ >= 2
# define WRAP_DEBUG 1
EOF
print '# endif /', '* DMALLOC && gcc >= 2 *', "/\n";
print '#endif /', '* !WRAP_DEBUG *', "/\n";
print <<"EOF";
#define wNew New
#if WRAP_DEBUG
# define callback_line_param \\
const char *dfunc, const char *func, const char *line,
# define wrap_line_param const char *wa1, const char *wa2, \\
const char *func, const char *line,
# define wrap_line_arg wa1, func, line,
# define wrap_c_line_arg wa2, func, line,
# define callback_line_init(super...) super (dfunc, func, line),
# define wrap _wrap
# ifdef DMALLOC
# undef wNew
# define wNew new (dmalloc, line, __LINE__)
# define WRAP_USE_NODELETE 1
template<class C> inline void
maybe_nodelete_addptr (const ref<C> &p, const char *fl, int *fp)
{
}
template<class C> inline void
maybe_nodelete_addptr (const ptr<C> &p, const char *fl, int *fp)
{
}
template<class C> inline void
maybe_nodelete_addptr (C *p, const char *fl, int *fp)
{
nodelete_addptr (p, fl, fp);
}
template<class C> inline void
maybe_nodelete_remptr (const ref<C> &p, const char *fl, int *fp)
{
}
template<class C> inline void
maybe_nodelete_remptr (const ptr<C> &p, const char *fl, int *fp)
{
}
template<class C> inline void
maybe_nodelete_remptr (C *p, const char *fl, int *fp)
{
nodelete_remptr (p, fl, fp);
}
EOF
print '# endif /', '* DMALLOC *', "/\n";
print '#else /', '* !WRAP_DEBUG *', "/\n";
print <<'EOF';
# define callback_line_param
# define wrap_line_param
# define wrap_line_arg
# define wrap_c_line_arg
# define callback_line_init(super...)
EOF
print '#endif /', '* !WRAP_DEBUG *', "/\n\n";
pfile;
print <<"EOF";
#undef callback_line_param
#undef wrap_line_param
#undef wrap_line_arg
#undef wrap_c_line_arg
#if WRAP_DEBUG
# undef wrap
# define wrap_arg1(arg_1, arg_rest...) #arg_1
# define wrap_arg2(arg_1, arg_rest...) wrap_arg1 (arg_rest)
# define do_wrap(__func_name, __do_wrap_args...) \\
_wrap (wrap_arg1(__do_wrap_args), wrap_arg2(__do_wrap_args), \\
__func_name, __FL__, ## __do_wrap_args)
# define wrap(__wrap_args...) do_wrap(__PRETTY_FUNCTION__, __wrap_args)
# define gwrap(__wrap_args...) \\
(nodelete_ignore () ? do_wrap(__FL__, __wrap_args) : 0)
$elsenotdebug
# define gwrap wrap
$endnotdebug
EOF
# Note that gwrap is for use outside of functions, because with some
# versions of gcc, using __PRETTY_FUNCTION__ outside of any function
# causes link-time errors
print "\n", '#endif /', '* !_CALLBACK1_H_INCLUDED_ *', '/', "\n";
__END__
# */
#endif /* perl */
#ifndef _CALLBACK1_H_INCLUDED_
#define _CALLBACK1_H_INCLUDED_ 1
#include "refcnt.h"
#ifndef WRAP_DEBUG
# if defined (DMALLOC) && __GNUC__ >= 2
# define WRAP_DEBUG 1
# endif /* DMALLOC && gcc >= 2 */
#endif /* !WRAP_DEBUG */
#define wNew New
#if WRAP_DEBUG
# define callback_line_param \
const char *dfunc, const char *func, const char *line,
# define wrap_line_param const char *wa1, const char *wa2, \
const char *func, const char *line,
# define wrap_line_arg wa1, func, line,
# define wrap_c_line_arg wa2, func, line,
# define callback_line_init(super...) super (dfunc, func, line),
# define wrap _wrap
# ifdef DMALLOC
# undef wNew
# define wNew new (dmalloc, line, __LINE__)
# define WRAP_USE_NODELETE 1
template<class C> inline void
maybe_nodelete_addptr (const ref<C> &p, const char *fl, int *fp)
{
}
template<class C> inline void
maybe_nodelete_addptr (const ptr<C> &p, const char *fl, int *fp)
{
}
template<class C> inline void
maybe_nodelete_addptr (C *p, const char *fl, int *fp)
{
nodelete_addptr (p, fl, fp);
}
template<class C> inline void
maybe_nodelete_remptr (const ref<C> &p, const char *fl, int *fp)
{
}
template<class C> inline void
maybe_nodelete_remptr (const ptr<C> &p, const char *fl, int *fp)
{
}
template<class C> inline void
maybe_nodelete_remptr (C *p, const char *fl, int *fp)
{
nodelete_remptr (p, fl, fp);
}
# endif /* DMALLOC */
#else /* !WRAP_DEBUG */
# define callback_line_param
# define wrap_line_param
# define wrap_line_arg
# define wrap_c_line_arg
# define callback_line_init(super...)
#endif /* !WRAP_DEBUG */
template<class R, class B1 = void, class B2 = void, class B3 = void> class callback;
template<class R>
class callback<R> {
public:
typedef struct ref<callback<R> > ref;
typedef struct ptr<callback<R> > ptr;
const char *_closure_type;
const void *_closure_void;
#if WRAP_DEBUG
const char *const dest;
const char *const src;
const char *const line;
callback (const char *df, const char *f, const char *l)
: dest (df[0] == '&' ? df + 1 : df), src (f), line (l) {}
#endif /* WRAP_DEBUG */
virtual R operator() () = 0;
virtual void trigger () { (void) (*this) (); }
virtual ~callback () {}
callback () : _closure_type (NULL), _closure_void (NULL) {}
void set_gdb_info (const char *typ, const void *v)
{ _closure_type = typ; _closure_void = v; }
};
template<class R>
class callback_0_0
: public callback<R> {
typedef R (*cb_t) ();
cb_t f;
public:
callback_0_0 (callback_line_param cb_t ff)
: callback_line_init (callback<R>) f (ff) {}
R operator() ()
{ return f (); }
};
template<class R>
static inline refcounted<callback_0_0<R> > *
wrap (wrap_line_param R (*f) ())
{
return wNew (refcounted<callback_0_0<R> >) (wrap_line_arg f);
}
template<class P, class C, class R>
class callback_c_0_0
: public callback<R> {
typedef R (C::*cb_t) () ;
P c;
cb_t f;
#if WRAP_USE_NODELETE
int deleted;
public:
callback_c_0_0 (callback_line_param const P &cc, cb_t ff)
: callback_line_init (callback<R>) c (cc), f (ff),
deleted (false)
{ maybe_nodelete_addptr (c, this->line, &deleted); }
~callback_c_0_0 () { maybe_nodelete_remptr (c, this->line, &deleted); }
R operator() ()
{
if (deleted)
panic ("callback from %s to %s on deleted object\n",
this->line, this->dest);
return ((*c).*f) ();
}
#else /* !WRAP_USE_NODELETE */
public:
callback_c_0_0 (callback_line_param const P &cc, cb_t ff)
: callback_line_init (callback<R>) c (cc), f (ff) {}
R operator() ()
{ return ((*c).*f) (); }
#endif /* !WRAP_USE_NODELETE */
};
template<class P, class C, class R>
class callback_c_0_0_const
: public callback<R> {
typedef R (C::*cb_t) () const;
P c;
cb_t f;
#if WRAP_USE_NODELETE
int deleted;
public:
callback_c_0_0_const (callback_line_param const P &cc, cb_t ff)
: callback_line_init (callback<R>) c (cc), f (ff),
deleted (false)
{ maybe_nodelete_addptr (c, this->line, &deleted); }
~callback_c_0_0_const () { maybe_nodelete_remptr (c, this->line, &deleted); }
R operator() ()
{
if (deleted)
panic ("callback from %s to %s on deleted object\n",
this->line, this->dest);
return ((*c).*f) ();
}
#else /* !WRAP_USE_NODELETE */
public:
callback_c_0_0_const (callback_line_param const P &cc, cb_t ff)
: callback_line_init (callback<R>) c (cc), f (ff) {}
R operator() ()
{ return ((*c).*f) (); }
#endif /* !WRAP_USE_NODELETE */
};
template<class C, class R>
static inline refcounted<callback_c_0_0< C *, C, R> > *
wrap (wrap_line_param C *p, R (C::*f) () )
{
return wNew (refcounted<callback_c_0_0< C *, C, R> >) (wrap_c_line_arg p, f);
}
template<class C, class R>
static inline refcounted<callback_c_0_0<ref< C>, C, R> > *
wrap (wrap_line_param const ref< C> &p, R (C::*f) () )
{
return wNew (refcounted<callback_c_0_0<ref< C>, C, R> >) (wrap_c_line_arg p, f);
}
template<class C, class R>
static inline refcounted<callback_c_0_0<ref< C>, C, R> > *
wrap (wrap_line_param const ptr< C> &p, R (C::*f) () )
{
return wNew (refcounted<callback_c_0_0<ref< C>, C, R> >) (wrap_c_line_arg p, f);
}
template<class C, class R>
static inline refcounted<callback_c_0_0<ref< C>, C, R> > *
wrap (wrap_line_param const refcounted< C> *p, R (C::*f) () )
{
return wNew (refcounted<callback_c_0_0<ref< C>, C, R> >) (wrap_c_line_arg p, f);
}
template<class C, class R>
static inline refcounted<callback_c_0_0_const<const C *, C, R> > *
wrap (wrap_line_param const C *p, R (C::*f) () const)
{
return wNew (refcounted<callback_c_0_0_const<const C *, C, R> >) (wrap_c_line_arg p, f);
}
template<class C, class R>
static inline refcounted<callback_c_0_0_const<ref<const C>, const C, R> > *
wrap (wrap_line_param const ref<const C> &p, R (C::*f) () const)
{
return wNew (refcounted<callback_c_0_0_const<ref<const C>, const C, R> >) (wrap_c_line_arg p, f);
}
template<class C, class R>
static inline refcounted<callback_c_0_0_const<ref<const C>, const C, R> > *
wrap (wrap_line_param const ptr<const C> &p, R (C::*f) () const)
{
return wNew (refcounted<callback_c_0_0_const<ref<const C>, const C, R> >) (wrap_c_line_arg p, f);
}
template<class C, class R>
static inline refcounted<callback_c_0_0_const<ref<const C>, const C, R> > *
wrap (wrap_line_param const refcounted<const C> *p, R (C::*f) () const)
{
return wNew (refcounted<callback_c_0_0_const<ref<const C>, const C, R> >) (wrap_c_line_arg p, f);
}
template<class R, class A1>
class callback_0_1
: public callback<R> {
typedef R (*cb_t) (A1);
cb_t f;
A1 a1;
public:
callback_0_1 (callback_line_param cb_t ff, const A1 &aa1)
: callback_line_init (callback<R>) f (ff), a1 (aa1) {}
R operator() ()
{ return f (a1); }
};
template<class R, class A1, class AA1>
static inline refcounted<callback_0_1<R, A1> > *
wrap (wrap_line_param R (*f) (A1), const AA1 &a1)
{
return wNew (refcounted<callback_0_1<R, A1> >) (wrap_line_arg f, a1);
}
template<class P, class C, class R, class A1>
class callback_c_0_1
: public callback<R> {
typedef R (C::*cb_t) (A1) ;
P c;
cb_t f;
#if WRAP_USE_NODELETE
A1 a1;
int deleted;
public:
callback_c_0_1 (callback_line_param const P &cc, cb_t ff, const A1 &aa1)
: callback_line_init (callback<R>) c (cc), f (ff), a1 (aa1),
deleted (false)
{ maybe_nodelete_addptr (c, this->line, &deleted); }
~callback_c_0_1 () { maybe_nodelete_remptr (c, this->line, &deleted); }
R operator() ()
{
if (deleted)
panic ("callback from %s to %s on deleted object\n",
this->line, this->dest);
return ((*c).*f) (a1);
}
#else /* !WRAP_USE_NODELETE */
A1 a1;
public:
callback_c_0_1 (callback_line_param const P &cc, cb_t ff, const A1 &aa1)
: callback_line_init (callback<R>) c (cc), f (ff), a1 (aa1) {}
R operator() ()
{ return ((*c).*f) (a1); }
#endif /* !WRAP_USE_NODELETE */
};
template<class P, class C, class R, class A1>
class callback_c_0_1_const
: public callback<R> {
typedef R (C::*cb_t) (A1) const;
P c;
cb_t f;
#if WRAP_USE_NODELETE
A1 a1;
int deleted;
public:
callback_c_0_1_const (callback_line_param const P &cc, cb_t ff, const A1 &aa1)
: callback_line_init (callback<R>) c (cc), f (ff), a1 (aa1),
deleted (false)
{ maybe_nodelete_addptr (c, this->line, &deleted); }
~callback_c_0_1_const () { maybe_nodelete_remptr (c, this->line, &deleted); }
R operator() ()
{
if (deleted)
panic ("callback from %s to %s on deleted object\n",
this->line, this->dest);
return ((*c).*f) (a1);
}
#else /* !WRAP_USE_NODELETE */
A1 a1;
public:
callback_c_0_1_const (callback_line_param const P &cc, cb_t ff, const A1 &aa1)
: callback_line_init (callback<R>) c (cc), f (ff), a1 (aa1) {}
R operator() ()
{ return ((*c).*f) (a1); }
#endif /* !WRAP_USE_NODELETE */
};
template<class C, class R, class A1, class AA1>
static inline refcounted<callback_c_0_1< C *, C, R, A1> > *
wrap (wrap_line_param C *p, R (C::*f) (A1) , const AA1 &a1)
{
return wNew (refcounted<callback_c_0_1< C *, C, R, A1> >) (wrap_c_line_arg p, f, a1);
}
template<class C, class R, class A1, class AA1>
static inline refcounted<callback_c_0_1<ref< C>, C, R, A1> > *
wrap (wrap_line_param const ref< C> &p, R (C::*f) (A1) , const AA1 &a1)
{
return wNew (refcounted<callback_c_0_1<ref< C>, C, R, A1> >) (wrap_c_line_arg p, f, a1);
}
template<class C, class R, class A1, class AA1>
static inline refcounted<callback_c_0_1<ref< C>, C, R, A1> > *
wrap (wrap_line_param const ptr< C> &p, R (C::*f) (A1) , const AA1 &a1)
{
return wNew (refcounted<callback_c_0_1<ref< C>, C, R, A1> >) (wrap_c_line_arg p, f, a1);
}
template<class C, class R, class A1, class AA1>
static inline refcounted<callback_c_0_1<ref< C>, C, R, A1> > *
wrap (wrap_line_param const refcounted< C> *p, R (C::*f) (A1) , const AA1 &a1)
{
return wNew (refcounted<callback_c_0_1<ref< C>, C, R, A1> >) (wrap_c_line_arg p, f, a1);
}
template<class C, class R, class A1, class AA1>
static inline refcounted<callback_c_0_1_const<const C *, C, R, A1> > *
wrap (wrap_line_param const C *p, R (C::*f) (A1) const, const AA1 &a1)
{
return wNew (refcounted<callback_c_0_1_const<const C *, C, R, A1> >) (wrap_c_line_arg p, f, a1);
}
template<class C, class R, class A1, class AA1>
static inline refcounted<callback_c_0_1_const<ref<const C>, const C, R, A1> > *
wrap (wrap_line_param const ref<const C> &p, R (C::*f) (A1) const, const AA1 &a1)
{
return wNew (refcounted<callback_c_0_1_const<ref<const C>, const C, R, A1> >) (wrap_c_line_arg p, f, a1);
}
template<class C, class R, class A1, class AA1>
static inline refcounted<callback_c_0_1_const<ref<const C>, const C, R, A1> > *
wrap (wrap_line_param const ptr<const C> &p, R (C::*f) (A1) const, const AA1 &a1)
{
return wNew (refcounted<callback_c_0_1_const<ref<const C>, const C, R, A1> >) (wrap_c_line_arg p, f, a1);
}
template<class C, class R, class A1, class AA1>
static inline refcounted<callback_c_0_1_const<ref<const C>, const C, R, A1> > *
wrap (wrap_line_param const refcounted<const C> *p, R (C::*f) (A1) const, const AA1 &a1)
{
return wNew (refcounted<callback_c_0_1_const<ref<const C>, const C, R, A1> >) (wrap_c_line_arg p, f, a1);
}
template<class R, class A1, class A2>
class callback_0_2
: public callback<R> {
typedef R (*cb_t) (A1, A2);
cb_t f;
A1 a1;
A2 a2;
public:
callback_0_2 (callback_line_param cb_t ff, const A1 &aa1, const A2 &aa2)
: callback_line_init (callback<R>) f (ff), a1 (aa1), a2 (aa2) {}
R operator() ()
{ return f (a1, a2); }
};
template<class R, class A1, class AA1, class A2, class AA2>
static inline refcounted<callback_0_2<R, A1, A2> > *
wrap (wrap_line_param R (*f) (A1, A2), const AA1 &a1, const AA2 &a2)
{
return wNew (refcounted<callback_0_2<R, A1, A2> >) (wrap_line_arg f, a1, a2);