forked from lballabio/QuantLib-SWIG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnull.i
168 lines (140 loc) · 4.03 KB
/
null.i
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
/*
Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
Copyright (C) 2003, 2004, 2005 StatPro Italia srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<[email protected]>. The license is also available online at
<http://quantlib.org/license.shtml>.
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 license for more details.
*/
#ifndef quantlib_null_values_i
#define quantlib_null_values_i
%include exception.i
%{
using QuantLib::Null;
typedef int intOrNull;
typedef double doubleOrNull;
%}
%inline%{
int nullInt() { return Null<int>(); }
double nullDouble() { return Null<double>(); }
%}
#if defined(SWIGPYTHON)
%typemap(in) intOrNull {
if ($input == Py_None)
$1 = Null<int>();
else if (PyInt_Check($input))
$1 = int(PyInt_AsLong($input));
else
SWIG_exception(SWIG_TypeError,"int expected");
}
%typecheck(SWIG_TYPECHECK_INTEGER) intOrNull {
$1 = ($input == Py_None || PyInt_Check($input)) ? 1 : 0;
}
%typemap(out) intOrNull {
if ($1 == Null<int>()) {
Py_INCREF(Py_None);
$result = Py_None;
} else {
$result = PyInt_FromLong(long($1));
}
}
%typemap(in) doubleOrNull {
if ($input == Py_None)
$1 = Null<double>();
else if (PyFloat_Check($input))
$1 = PyFloat_AsDouble($input);
else
SWIG_exception(SWIG_TypeError,"double expected");
}
%typecheck(SWIG_TYPECHECK_DOUBLE) doubleOrNull {
$1 = ($input == Py_None || PyFloat_Check($input)) ? 1 : 0;
}
%typemap(out) doubleOrNull {
if ($1 == Null<double>()) {
Py_INCREF(Py_None);
$result = Py_None;
} else {
$result = PyFloat_FromDouble($1);
}
}
#elif defined(SWIGRUBY)
%typemap(in) intOrNull {
if ($input == Qnil)
$1 = Null<int>();
else if (FIXNUM_P($input))
$1 = int(FIX2INT($input));
else
SWIG_exception(SWIG_TypeError,"not an integer");
}
%typecheck(SWIG_TYPECHECK_INTEGER) intOrNull {
$1 = ($input == Qnil || FIXNUM_P($input)) ? 1 : 0;
}
%typemap(out) intOrNull {
if ($1 == Null<int>())
$result = Qnil;
else
$result = INT2NUM($1);
}
%typemap(in) doubleOrNull {
if ($input == Qnil)
$1 = Null<double>();
else if (TYPE($input) == T_FLOAT)
$1 = NUM2DBL($input);
else if (FIXNUM_P($input))
$1 = double(FIX2INT($input));
else
SWIG_exception(SWIG_TypeError,"not a double");
}
%typecheck(SWIG_TYPECHECK_DOUBLE) doubleOrNull {
$1 = ($input == Qnil || TYPE($input) == T_FLOAT ||
FIXNUM_P($input)) ? 1 : 0;
}
%typemap(out) doubleOrNull {
if ($1 == Null<double>())
$result = Qnil;
else
$result = rb_float_new($1);
}
#elif defined(SWIGJAVA)
typedef int intOrNull;
typedef double doubleOrNull;
#elif defined(SWIGCSHARP)
typedef int intOrNull;
typedef double doubleOrNull;
#elif defined(SWIGR)
%typemap(rtype) intOrNull "numeric";
%typemap(scoercein) intOrNull
%{ $input = as($input, "integer"); %}
%typemap(scoerceout) intOrNull %{ %}
%typemap(in) intOrNull {
$1 = ($1_ltype) INTEGER($input)[0];
if ($1 == R_NaInt) $1 = Null<int>();
}
%typemap(out) intOrNull {
if ($1 == Null<int>())
$result = Rf_ScalarLogical(NA_LOGICAL)
else
$result = ScalarInteger($1);
}
%typemap(rtype) doubleOrNull "numeric";
%typemap(scoercein) doubleOrNull
%{ $input = as($input, "numeric"); %}
%typemap(scoerceout) doubleOrNull %{ %}
%typemap(in) doubleOrNull {
$1 = ($1_ltype) REAL($input)[0];
if (R_IsNA($1)) $1 = Null<double>();
}
%typemap(out) doubleOrNull {
if ($1 == Null<double>())
$result = Rf_ScalarLogical(NA_LOGICAL);
else
$result = Rf_ScalarReal($1);
}
#endif
#endif