annotate test/callback_plain_c++/test_main.cc @ 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
author Tassilo Philipp
date Mon, 20 Jun 2022 17:42:13 +0200
parents ca28e9e3c644
children b36a738c8975
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 {
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
114 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
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
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
122 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
123 {
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
124 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
125
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
126 {
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
127 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
128 DCaggr *aggrs[1] = { NULL }; // one non-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
129 cb = dcbNewCallback2(")A", &cbNonTrivAggrReturnHandler, NULL, 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
130
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
131 NonTriv result = ((NonTriv(*)())cb)(); // potential copy elision on construction
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
132
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
133 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
134 int b = NonTriv::b-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
135 printf("successfully returned from callback 1/2\n");
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
136 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
137
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
138 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
139
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
140 // avoid copy elision on construction
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
141 result.i = result.j = -77;
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
142 result = ((NonTriv(*)())cb)(); // potential copy elision
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
143
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
144 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
145 b = NonTriv::b-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
146 printf("successfully returned from callback 2/2\n");
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
147 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
148
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
149 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
150
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 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
152 }
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
153
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
154 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
155 }
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
156 #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
157
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
158
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
159 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
160 {
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
161 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
162
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
163 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
164
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
165 #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
166 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
167 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
168 #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
169
ca28e9e3c644 - test/callback_plain_c++, to test C++ non-trivial aggrs by value, as well as method callback handlers
Tassilo Philipp
parents:
diff changeset
170 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
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 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
173
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 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
175 }
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