File: null.i

package info (click to toggle)
quantlib-swig 1.40-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,276 kB
  • sloc: python: 6,024; java: 1,552; cs: 774; makefile: 309; sh: 22
file content (140 lines) | stat: -rw-r--r-- 3,565 bytes parent folder | download
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

/*
 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
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <https://www.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 (PyLong_Check($input))
        $1 = int(PyLong_AsLong($input));
    else
        SWIG_exception(SWIG_TypeError, "int expected");
%}
%typecheck(SWIG_TYPECHECK_INTEGER) intOrNull %{
    $1 = ($input == Py_None || PyLong_Check($input)) ? 1 : 0;
%}
%typemap(out) intOrNull %{
    if ($1 == Null<int>()) {
        Py_INCREF(Py_None);
        $result = Py_None;
    } else {
        $result = PyLong_FromLong($1);
    }
%}

%typemap(in) doubleOrNull %{
    if ($input == Py_None)
        $1 = Null<double>();
    else if (PyFloat_Check($input))
        $1 = PyFloat_AsDouble($input);
    else if (PyLong_Check($input))
        $1 = PyLong_AsDouble($input);
    else
        SWIG_exception(SWIG_TypeError, "double expected");
%}
%typecheck(SWIG_TYPECHECK_DOUBLE) doubleOrNull %{
    $1 = ($input == Py_None || PyFloat_Check($input) || PyLong_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(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(rtypecheck) intOrNull %{
  ((length($arg) == 0) || (length($arg) == 1 && is.integer($arg)) || (length($arg) == 1 && is.numeric($arg)) || (length($arg) == 1 && is.na($arg)))
%}


%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(rtypecheck) doubleOrNull %{
  ((length($arg) == 0) || (length($arg) == 1 && is.numeric($arg)) || (length($arg) == 1 && is.na($arg)))
%}


%typemap(out) doubleOrNull %{
    if ($1 == Null<double>())
        $result = Rf_ScalarLogical(NA_LOGICAL);
    else
        $result = Rf_ScalarReal($1);
%}

#endif


#endif