-
Notifications
You must be signed in to change notification settings - Fork 23
/
handle.cpp
261 lines (220 loc) · 6 KB
/
handle.cpp
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
// handle.cpp - Embed C++ objects in Excel using xll::handle<T>
// https://xlladdins.com
//#define XLOPERX XLOPER
#include <concepts>
#include "../xll/xll.h"
using namespace xll;
// get and set a type
template<std::semiregular T>
class base {
T x;
public:
base() { }
base(const T& x)
: x(x)
{ }
base(const base&) = default;
base& operator=(const base&) = default;
virtual ~base()
{ }
T& get()
{
return x;
}
void set(const T& _x)
{
x = _x;
}
};
// embed base in Excel
AddIn xai_base(
Function(XLL_HANDLEX, "xll_base", "\\XLL.BASE")
.Arguments({
Arg(XLL_LPOPER, "cell", "is a cell or range of cells")
})
.Uncalced() // required for functions creating handles
.FunctionHelp("Return a handle to a base object.")
);
HANDLEX WINAPI xll_base(LPOPER px)
{
#pragma XLLEXPORT
xll::handle<base<OPER>> h(new base<OPER>(*px));
return h.get();
}
// call base member function
AddIn xai_base_get(
Function(XLL_LPOPER, "xll_base_get", "XLL.BASE.GET")
.Arguments({
Arg(XLL_HANDLEX, "handle", "is a handle returned by \\XLL.BASE")
})
.FunctionHelp("Return the value stored in base.")
);
LPOPER WINAPI xll_base_get(HANDLEX _h)
{
#pragma XLLEXPORT
static OPER o;
xll::handle<base<OPER>> h(_h);
o = h ? h->get() : ErrNA;
return &o;
}
AddIn xai_base_set(
Function(XLL_HANDLEX, "xll_base_set", "XLL.BASE.SET")
.Arguments({
Arg(XLL_HANDLEX, "handle", "is a handle returned by \\XLL.BASE."),
Arg(XLL_LPOPER, "cell", "is a cell or range of cells")
})
.FunctionHelp("Set the value of base to cell.")
);
HANDLEX WINAPI xll_base_set(HANDLEX _h, LPOPER px)
{
#pragma XLLEXPORT
xll::handle<base<OPER>> h(_h);
if (h) {
h->set(*px);
}
return _h;
}
// single inheritance
template<std::semiregular T>
class derived : public base<T> {
T x2;
public:
derived()
{ }
// put x in base and x2 in derived
derived(const T& x, const T& x2)
: base<T>(x), x2(x2)
{ }
derived(const derived&) = default;
derived& operator=(const derived&) = default;
~derived() = default;
T& get2()
{
return x2;
}
};
AddIn xai_derived(
Function(XLL_HANDLEX, "xll_derived", "\\XLL.DERIVED")
.Arguments({
Arg(XLL_LPOPER, "cell", "is a cell or range of cells."),
Arg(XLL_LPOPER, "cell2", "is a cell or range of cells.")
})
.Uncalced() // required for functions creating handles
.FunctionHelp("Return a handle to a derived object.")
);
HANDLEX WINAPI xll_derived(LPOPER px, LPOPER px2)
{
#pragma XLLEXPORT
// derived isa base
xll::handle<base<OPER>> h(new derived<OPER>(*px, *px2));
return h.get();
}
// XLL.BASE.GET calls base::get for handles returned by XLL.DERIVED.
AddIn xai_derived_get(
Function(XLL_LPOPER, "xll_derived_get", "XLL.DERIVED.GET2")
.Arguments({
Arg(XLL_HANDLEX, "handle", "is a handle returned by \\XLL.DERIVED.")
})
.FunctionHelp("Return the second value stored in derived.")
);
LPOPER WINAPI xll_derived_get(HANDLEX _h)
{
#pragma XLLEXPORT
static OPER o;
// get handle to base class
xll::handle<base<OPER>> h(_h);
if (!h) {
o = ErrNA;
}
else {
// downcast to derived
auto pd = h.as<derived<OPER>>();
o = pd ? pd->get2() : ErrValue;
}
return &o;
}
// convert pointers to and from "\\base[0x<hexdigits>]"
static handle<base<OPER>>::codec<XLOPERX> base_codec; // ("\\base[0x", "]");
AddIn xai_ebase(
Function(XLL_LPOPER, "xll_ebase", "\\XLL.EBASE")
.Arguments({
Arg(XLL_LPOPER, "cell", "is a cell or range of cells")
})
.Uncalced()
.FunctionHelp("Return a handle to a base object.")
);
LPOPER WINAPI xll_ebase(LPOPER px)
{
#pragma XLLEXPORT
xll::handle<base<OPER>> h(new base<OPER>(*px));
auto& x = base_codec.encode(h.get());
return const_cast<LPOPER>(&x);
}
AddIn xai_ebase_get(
Function(XLL_LPOPER, "xll_ebase_get", "XLL.EBASE.GET")
.Arguments({
Arg(XLL_LPOPER, "handle", "is a handle returned by \\XLL.EBASE")
})
.FunctionHelp("Return the value stored in base.")
);
LPOPER WINAPI xll_ebase_get(LPOPER ph)
{
#pragma XLLEXPORT
xll::handle<base<OPER>> h(base_codec.decode(*ph));
return xll_base_get(h.get());
}
#ifdef _DEBUG
// Documentation for the Excel() function.
// https://xlladdins.github.io/Excel4Macros/
AddIn xai_handle_test(Macro("xll_handle_test", "HANDLE.TEST"));
int WINAPI xll_handle_test()
{
#pragma XLLEXPORT
try {
// enter data in cell RiCj
// https://xlladdins.github.io/Excel4Macros/formula.html
auto Formula = [](auto i, auto j, auto data) {
return Excel(xlcFormula, OPER(data), OPER(REF(i,j)));
};
// get contents of cell RiCj;
// https://xlladdins.github.io/Excel4Macros/get.cell.html
auto Contents = [](auto i, auto j) {
// 5 - contents
return Excel(xlfGetCell, OPER(5), OPER(REF(i, j)));
};
// https://xlladdins.github.io/Excel4Macros/new.html
// 1 - worksheet
Excel(xlcNew, OPER(1), Missing, Missing); // worksheet
// test base
Formula(0, 0, 1.23);
ensure(Contents(0, 0) == 1.23);
Formula(1, 0, "=\\XLL.BASE(R[-1]C[0])");
Formula(2, 0, "=XLL.BASE.GET(R[-1]C[0])");
ensure(Contents(2, 0) == 1.23);
Formula(0, 0, "foo");
ensure(Contents(2, 0) == "foo");
// test derived
Formula(4, 0, 4.56);
Formula(5, 0, "bar");
Formula(6, 0, "=\\XLL.DERIVED(R[-2]C[0], R[-1]C[0])");
Formula(7, 0, "=XLL.BASE.GET(R[-1]C[0])");
Formula(8, 0, "=XLL.DERIVED.GET2(R[-2]C[0])");
ensure(Contents(7, 0) == 4.56); // derived isa base
ensure(Contents(8, 0) == "bar");
// use pretty handles
Formula(10, 0, "=\\XLL.EBASE(R1C1)");
auto base = Contents(10, 0); // pretty name
//ensure(Excel(xlfLeft, base, OPER(8)) == "\\base[0x"); // check prefix
Formula(11, 0, "=XLL.EBASE.GET(R[-1]C[0])");
auto R1C1 = Contents(0, 0);
//ensure(Contents(11, 0) == R1C1);
//ensure(Contents(2, 0) = R1C1); // XLL.BASE.GET also called
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
return FALSE;
}
return TRUE;
}
Auto<OpenAfter> xaoa_handle_test(xll_handle_test);
#endif // _DEBUG