-
Notifications
You must be signed in to change notification settings - Fork 23
/
calc_linkinlibrary.cc
350 lines (283 loc) · 8.5 KB
/
calc_linkinlibrary.cc
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
#ifndef INCLUDED_STDDEFX
#include "stddefx.h"
#define INCLUDED_STDDEFX
#endif
#ifndef INCLUDED_CALC_LINKINLIBRARY
#include "calc_linkinlibrary.h"
#define INCLUDED_CALC_LINKINLIBRARY
#endif
// Library headers.
#ifndef INCLUDED_SSTREAM
#include <sstream>
#define INCLUDED_SSTREAM
#endif
// PCRaster library headers.
#ifndef INCLUDED_PCRLINKIN
#include "pcrlinkin.h"
#define INCLUDED_PCRLINKIN
#endif
#ifndef INCLUDED_COM_DYNAMICLIBRARY
#include "com_dynamiclibrary.h"
#define INCLUDED_COM_DYNAMICLIBRARY
#endif
#ifdef WIN32
#ifndef INCLUDED_COM_WIN32
#include "com_win32.h"
#define INCLUDED_COM_WIN32
#endif
#include <windows.h>
#include <excpt.h>
#endif
#ifndef INCLUDED_PCRASTERXSD
#include "PCRasterXSD.h"
#define INCLUDED_PCRASTERXSD
#endif
#ifndef INCLUDED_PCRXSD_DOMINPUT
#include "pcrxsd_dominput.h"
#define INCLUDED_PCRXSD_DOMINPUT
#endif
// Module headers.
#ifndef INCLUDED_CALC_XMLDATATYPE
#include "calc_xmldatatype.h"
#define INCLUDED_CALC_XMLDATATYPE
#endif
#include <filesystem>
/*!
\file
This file contains the implementation of the LinkInLibrary class.
*/
namespace calc {
//------------------------------------------------------------------------------
class LinkInLibraryPrivate
{
#ifndef WIN32
#define LINKIN_CALL
#define TRY_LINKIN_CALL
#define EXCEPT_LINKIN_CALL
#else
// NOTE that WIN32 exception handler
// can only be done in a context where
// obects needs unwinding.
// hence we always do in a wrapper
void throwExcep(DWORD e) const {
std::ostringstream errorStream;
errorStream << "ERROR System exception:\n "
<< com::win32ExceptionString(e)
<< std::endl;
throw com::Exception(errorStream.str());
}
#define LINKIN_CALL __stdcall
#define TRY_LINKIN_CALL __try
#define EXCEPT_LINKIN_CALL \
__except(EXCEPTION_EXECUTE_HANDLER) { \
throwExcep(GetExceptionCode()); \
}
#endif
typedef const char* (LINKIN_CALL *ExecuteFun)(const char *xml,
LinkInTransferArray linkInTransferArray);
typedef void (LINKIN_CALL *RunContextFun)(
int nrRows,
int nrCols,
double cellSize,
double xLowerLeftCorner,
double yLowerLeftCorner,
int currentTimeStep);
typedef const char* (LINKIN_CALL *CheckFun)(const char *xml);
ExecuteFun d_execute;
RunContextFun d_runContext;
CheckFun d_check;
std::string d_name;
com::DynamicLibrary* d_dl;
std::unique_ptr<pcrxml::LinkInLibraryManifest> d_manifest;
void throwException(std::string const& msg)
{
std::ostringstream s;
s << "'" << d_name << "': " << msg;
throw com::Exception(s.str());
}
//! wrapper
void runContext(
int nrRows,
int nrCols,
double cellSize,
double xLowerLeftCorner,
double yLowerLeftCorner,
int currentTimeStep) const
{
TRY_LINKIN_CALL {
d_runContext(
nrRows,
nrCols,
cellSize,
xLowerLeftCorner,
yLowerLeftCorner,
currentTimeStep);
} EXCEPT_LINKIN_CALL
}
void runContext(pcrxml::RunContext const& rc) const
{
PRECOND(d_runContext);
PRECOND(rc.areaMap().cellSize().present());
PRECOND(rc.areaMap().xLowerLeftCorner().present());
PRECOND(rc.areaMap().yLowerLeftCorner().present());
// PROBLEM MSC ICE's on fundamentalBaseCast to int
runContext(
pcrxsd::fundamentalBaseCast<size_t>((rc.areaMap().nrRows())),
pcrxsd::fundamentalBaseCast<size_t>((rc.areaMap().nrCols())),
rc.areaMap().cellSize().get(),
rc.areaMap().xLowerLeftCorner().get(),
rc.areaMap().yLowerLeftCorner().get(),
pcrxsd::fundamentalBaseCast<size_t>((rc.timer().current())));
}
public:
LinkInLibraryPrivate(const std::string& name):
d_name(name),
d_dl(nullptr)
{
try {
d_dl= new com::DynamicLibrary(d_name);
} catch(com::DynamicLibraryException const& e) {
throwException(e.messages());
}
// ExecuteFun must be there
#if defined(_WIN32) && !defined(_WIN64)
// for windows 32 bit only
d_execute = (ExecuteFun)d_dl->addressAndSetLibPath("_pcr_LinkInExecute@8");
d_check = (CheckFun)d_dl->address("_pcr_LinkInCheck@4");
d_runContext = (RunContextFun)d_dl->address("_pcr_LinkInRunContext@36");
#else
// _WIN64 and linux
d_execute = (ExecuteFun)d_dl->addressAndSetLibPath("pcr_LinkInExecute");
d_check = (CheckFun)d_dl->address("pcr_LinkInCheck");
d_runContext = (RunContextFun)d_dl->address("pcr_LinkInRunContext");
#endif
if (!d_execute)
throwException("code library does not contain pcr_LinkInExecute");
std::filesystem::path xmlFile(d_dl->directory());
xmlFile /= (d_name+".xml");
pcrxsd::DOMInput d(pcrxsd::DOMInput::CompiledIn);
d.setValidate(true);
d.setFile(xmlFile.string());
try {
d_manifest.reset(pcrxml::linkInLibraryManifest(*d.document()).release());
} catch(pcrxsd::Exception const& e) {
throwException(e.msg());
}
}
~LinkInLibraryPrivate()
{
delete d_dl;
}
pcrxml::LinkInLibraryManifest const& manifest() const
{
PRECOND(d_manifest.get());
return *(d_manifest.get());
}
//! wrapper
void execute(const char *xml,
void ** transferArray) const
{
TRY_LINKIN_CALL {
d_execute(xml,transferArray);
} EXCEPT_LINKIN_CALL
}
void execute(pcrxml::LinkInExecuteInput const& li,
void ** transferArray) const
{
if (d_runContext)
runContext(li.context());
std::ostringstream s;
pcrxml::linkInExecuteInput(s,li,pcrxsd::namespaceInfoMap("PCRaster.xsd"));
execute(s.str().c_str(),transferArray);
}
//! wrapper
const char* check(const char *xml) const
{
TRY_LINKIN_CALL {
return d_check(xml);
} EXCEPT_LINKIN_CALL
return "implementation error"; // should never come here
}
pcrxml::LinkInCheckResult check(
pcrxml::LinkInCheckInput const& in) const
{
if (d_check) {
std::ostringstream inS;
pcrxml::linkInCheckInput(inS,in,pcrxsd::namespaceInfoMap("PCRaster.xsd"));
const char *outCstr=check(inS.str().c_str());
std::istringstream outS(outCstr);
#ifdef DEBUG_DEVELOP
return *pcrxml::linkInCheckResult(outS);
#else
::xsd::cxx::tree::flags f = xml_schema::flags::dont_validate;
return *pcrxml::linkInCheckResult(outS, f);
#endif
}
// no check do the default thing
pcrxml::LinkInCheckResult out;
out.result(in.result());
out.argument(in.argument());
return out;
}
};
//------------------------------------------------------------------------------
// DEFINITION OF STATIC LINKINLIBRARY MEMBERS
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// DEFINITION OF LINKINLIBRARY MEMBERS
//------------------------------------------------------------------------------
LinkInLibrary::LinkInLibrary(const std::string& name):
d_data(new LinkInLibraryPrivate(name))
{
}
/* NOT IMPLEMENTED
//! Copy constructor.
LinkInLibrary::LinkInLibrary(
LinkInLibrary const& rhs)
: Base(rhs)
{
}
*/
LinkInLibrary::~LinkInLibrary()
{
delete d_data;
}
/* NOT IMPLEMENTED
//! Assignment operator.
LinkInLibrary& LinkInLibrary::operator=(
LinkInLibrary const& rhs)
{
if(this != &rhs) {
}
return *this;
}
*/
pcrxml::LinkInLibraryManifest const& LinkInLibrary::manifest() const
{
return d_data->manifest();
}
void LinkInLibrary::execute(
pcrxml::LinkInExecuteInput const& li,
void ** transferArray) const
{
d_data->execute(li,transferArray);
}
pcrxml::LinkInCheckResult LinkInLibrary::check(
pcrxml::LinkInCheckInput const& in) const
{
return d_data->check(in);
}
/*
void LinkInLibrary::areaMap(geo::RasterSpace const& rs,
size_t currentTimeStep) const
{
d_data->areaMap(rs,currentTimeStep);
}
*/
//------------------------------------------------------------------------------
// DEFINITION OF FREE OPERATORS
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// DEFINITION OF FREE FUNCTIONS
//------------------------------------------------------------------------------
} // namespace calc