Mercurial > pub > dyncall > dyncall
annotate test/callback_plain_c++/test_main.cc @ 634:e3441521ad6b
"
author | Tassilo Philipp |
---|---|
date | Sun, 20 Nov 2022 19:46:01 +0100 |
parents | 87b5f5d7af1f |
children |
rev | line source |
---|---|
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
1 /* |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
2 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
3 Package: dyncall |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
4 Library: test |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
5 File: test/callback_plain_c++/test_main.c |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
6 Description: |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
7 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
8 Tests only C++ specifics: |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
9 - method callback handlers (= standard dyncallback handler, but testing |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
10 calling convention mode setting and hidden this ptr) |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
11 - calling functions or methods that take/return non-trivial C++ aggregates |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
12 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
13 License: |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
14 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
15 Copyright (c) 2022 Tassilo Philipp <tphilipp@potion-studios.com> |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
16 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
17 Permission to use, copy, modify, and distribute this software for any |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
18 purpose with or without fee is hereby granted, provided that the above |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
19 copyright notice and this permission notice appear in all copies. |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
20 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
21 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
22 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
23 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
24 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
25 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
26 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
27 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
28 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
29 */ |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
30 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
31 #include "../../dyncallback/dyncall_callback.h" |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
32 #include "../common/platformInit.h" |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
33 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */ |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
34 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
35 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
36 #if defined(DC__Feature_AggrByVal) |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
37 struct Triv { |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
38 float t; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
39 }; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
40 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
41 struct NonTriv { |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
42 int i, j; |
555
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
43 static int a, b; |
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
44 NonTriv(int a, int b) : i(a),j(b) { printf("%s\n", "NonTriv::NonTriv(int,int) called"); } |
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
45 NonTriv(const NonTriv& rhs) { printf("%s\n", "NonTriv::NonTriv(const NonTriv&) called"); i = a++; j = b++; } |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
46 }; |
555
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
47 int NonTriv::a = 13; |
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
48 int NonTriv::b = 37; |
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
49 |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
50 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
51 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
52 char cbNonTrivAggrArgHandler(DCCallback* cb, DCArgs* args, DCValue* result, void* userdata) |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
53 { |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
54 int* ud = (int*)userdata; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
55 int arg1; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
56 NonTriv arg2(0, 0); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
57 NonTriv arg3(0, 0); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
58 Triv arg4; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
59 double arg5; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
60 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
61 printf("reached callback\n"); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
62 printf("userdata: %d\n", *ud); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
63 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
64 // the non-triv arg4 test basically assures that the aggr description list is |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
65 // respected (with first two being NULL as non-trivial aggrs) |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
66 arg1 = dcbArgInt(args); printf("1st argument: %d\n", arg1); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
67 arg2 = *(NonTriv*)dcbArgAggr(args, NULL); printf("2nd argument: %d %d\n", arg2.i, arg2.j); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
68 arg3 = *(NonTriv*)dcbArgAggr(args, NULL); printf("3nd argument: %d %d\n", arg3.i, arg3.j); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
69 dcbArgAggr(args, (DCpointer)&arg4); printf("4th argument: %f\n", arg4.t); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
70 arg5 = dcbArgDouble(args); printf("5th argument: %f\n", arg5); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
71 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
72 // the non-triv aggregate members are 14, 38 and 15, 39, respectively, b/c of |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
73 // copy-ctor call on *passing* them to this handler (not b/c of assignment |
555
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
74 // operator, above); see above where 13 and 37 are initialized |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
75 result->d = *ud + arg1 + arg2.i + arg2.j + arg3.i + arg3.j + arg4.t + arg5; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
76 return 'd'; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
77 } |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
78 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
79 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
80 int testNonTrivAggrArgsCallback() |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
81 { |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
82 DCCallback* cb; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
83 Triv t = { 1.75f }; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
84 NonTriv a(1, 2); |
555
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
85 NonTriv b(a); |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
86 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
87 int ret = 1; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
88 double result = 0; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
89 int userdata = 1337; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
90 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
91 DCaggr *triv_aggr = dcNewAggr(1, sizeof(t)); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
92 dcAggrField(triv_aggr, DC_SIGCHAR_FLOAT, offsetof(Triv, t), 1); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
93 dcCloseAggr(triv_aggr); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
94 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
95 DCaggr *aggrs[3] = { NULL, NULL, triv_aggr }; // NULL b/c non-trivial aggrs, see manpage |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
96 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
97 cb = dcbNewCallback2("iAAAd)d", &cbNonTrivAggrArgHandler, &userdata, aggrs); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
98 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
99 result = ((double(*)(int, NonTriv, NonTriv, Triv, double))cb)(123, a, b, t, 4.5); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
100 dcbFreeCallback(cb); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
101 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
102 printf("successfully returned from callback\n"); |
555
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
103 printf("retval (should be 1572.25): %f\n", result); |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
104 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
105 ret = result == 1572.25 && ret; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
106 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
107 return ret; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
108 } |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
109 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
110 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
111 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
112 char cbNonTrivAggrReturnHandler(DCCallback* cb, DCArgs* args, DCValue* result, void* userdata) |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
113 { |
557
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
114 printf("reached callback (for sig \"%s\")\n", *(const char**)userdata); |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
115 dcbReturnAggr(args, result, NULL); // this sets result->p to the non-triv aggr space allocated by the calling convention |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
116 *(NonTriv*)result->p = NonTriv(1, 3); // explicit non-copy ctor and assignment operator, so not using NonTriv's statics a and b |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
117 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
118 return 'A'; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
119 } |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
120 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
121 |
557
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
122 // class with single vtable entry, used to get the compiler to generate a |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
123 // method call; entry is at beginning w/o offset (see plain_c++/test_main.cc |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
124 // about potential vtable entry offsets) |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
125 struct Dummy { virtual NonTriv f() = 0; }; |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
126 |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
127 |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
128 int testNonTrivAggrReturnCallback() |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
129 { |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
130 int ret = 1; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
131 |
557
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
132 const char *sigs[] = { |
578
87b5f5d7af1f
callback_plain_c++: improved cross-platform robustness of faked thiscall used for testing
Tassilo Philipp
parents:
557
diff
changeset
|
133 ")A", // standard call |
557
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
134 "_*p)A" // thiscall w/ this-ptr arg (special retval register handling in win/x64 calling conv) |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
135 }; |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
136 |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
137 |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
138 for(int i=0; i<sizeof(sigs)/sizeof(sigs[0]); ++i) |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
139 { |
557
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
140 int is_method = (sigs[i][0] == '_' && sigs[i][1] == '*'); |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
141 |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
142 DCaggr *aggrs[1] = { NULL }; // one non-triv aggr |
557
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
143 DCCallback* cb = dcbNewCallback2(sigs[i], &cbNonTrivAggrReturnHandler, sigs+i, aggrs); |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
144 |
578
87b5f5d7af1f
callback_plain_c++: improved cross-platform robustness of faked thiscall used for testing
Tassilo Philipp
parents:
557
diff
changeset
|
145 DCpointer fakeClass[sizeof(Dummy)/sizeof(DCpointer)]; |
87b5f5d7af1f
callback_plain_c++: improved cross-platform robustness of faked thiscall used for testing
Tassilo Philipp
parents:
557
diff
changeset
|
146 for(int j=0; j<sizeof(Dummy)/sizeof(DCpointer); ++j) |
87b5f5d7af1f
callback_plain_c++: improved cross-platform robustness of faked thiscall used for testing
Tassilo Philipp
parents:
557
diff
changeset
|
147 fakeClass[j] = &cb; // hack: write method ptr f over entire vtable to be sure |
557
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
148 |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
149 // potential copy elision on construction |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
150 NonTriv result = is_method ? ((Dummy*)&fakeClass)->f() : ((NonTriv(*)())cb)(); |
555
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
151 |
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
152 int a = NonTriv::a-1; |
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
153 int b = NonTriv::b-1; |
557
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
154 printf("successfully returned from callback 1/2 of \"%s\"\n", sigs[i]); |
555
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
155 printf("retval w/ potential retval optimization and copy-init (should be %d %d for init or %d %d for copy, both allowed by C++): %d %d\n", 1, 3, a, b, result.i, result.j); |
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
156 |
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
157 ret = ((result.i == 1 && result.j == 3) || (result.i == a && result.j == b)) && ret; |
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
158 |
557
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
159 // avoid copy elision on construction |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
160 result.i = result.j = -77; |
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
161 result = is_method ? ((Dummy*)&fakeClass)->f() : ((NonTriv(*)())cb)(); // potential copy elision |
555
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
162 |
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
163 a = NonTriv::a-1; |
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
164 b = NonTriv::b-1; |
557
b36a738c8975
- dyncallback: fix for calling back win/x64 C++ methods returning non-trivial aggregates (thanks Raphael!)
Tassilo Philipp
parents:
555
diff
changeset
|
165 printf("successfully returned from callback 2/2 of \"%s\"\n", sigs[i]); |
555
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
166 printf("retval w/ potential retval optimization and copy-init (should be %d %d for init or %d %d for copy, both allowed by C++): %d %d\n", 1, 3, a, b, result.i, result.j); |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
167 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
168 dcbFreeCallback(cb); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
169 |
555
214c4efc104f
- test/callback_plain_c++: added check and a slightly different test for C++ copy-elision/retval-optimization, as C++ finds ambiguities totally ok
Tassilo Philipp
parents:
545
diff
changeset
|
170 ret = ((result.i == 1 && result.j == 3) || (result.i == a && result.j == b)) && ret; |
545
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
171 } |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
172 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
173 return ret; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
174 } |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
175 #endif |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
176 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
177 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
178 int main() |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
179 { |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
180 int result = 1; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
181 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
182 dcTest_initPlatform(); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
183 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
184 #if defined(DC__Feature_AggrByVal) |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
185 result = testNonTrivAggrArgsCallback() && result; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
186 result = testNonTrivAggrReturnCallback() && result; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
187 #endif |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
188 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
189 printf("result: callback_plain_c++: %d\n", result); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
190 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
191 dcTest_deInitPlatform(); |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
192 |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
193 return !result; |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
194 } |
ca28e9e3c644
- test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff
changeset
|
195 |