File: common.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 (248 lines) | stat: -rw-r--r-- 5,897 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
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

/*
 Copyright (C) 2000, 2001, 2002, 2003 RiskMap 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_common_i
#define quantlib_common_i

%{
    namespace QuantLib { namespace ext {} }
    namespace ext = QuantLib::ext;
%}
#define SWIG_SHARED_PTR_NAMESPACE ext

%include stl.i
%include exception.i
%include boost_shared_ptr.i

%define QL_TYPECHECK_BOOL       7210    %enddef

%{
// This is necessary to avoid compile failures on 
// GCC 4
// see http://svn.boost.org/trac/boost/ticket/1793

#if defined(NDEBUG)
#define BOOST_DISABLE_ASSERTS 1
#endif

#include <boost/algorithm/string/case_conv.hpp>
%}

#if defined(SWIGPYTHON)
%typemap(in) ext::optional<bool> %{
    if ($input == Py_None)
        $1 = ext::nullopt;
    else if (PyBool_Check($input))
        $1 = $input == Py_True;
    else
        SWIG_exception(SWIG_TypeError, "bool expected");
%}
%typecheck (QL_TYPECHECK_BOOL) ext::optional<bool> %{
    $1 = (PyBool_Check($input) || $input == Py_None) ? 1 : 0;
%}
%typemap(out) ext::optional<bool> %{
    $result = !$1 ? Py_None : *$1 ? Py_True : Py_False;
    Py_INCREF($result);
%}
#else
#if defined(SWIGCSHARP)
%typemap(cscode) ext::optional<bool> %{
    public static implicit operator OptionalBool(bool b) => new OptionalBool(b);
%}
#endif
namespace ext {
    template<class T>
    class optional {
      public:
        optional(T t);
    };
}
%template(OptionalBool) ext::optional<bool>;
#endif

%{
// generally useful classes
using QuantLib::Error;
using QuantLib::Handle;
using QuantLib::RelinkableHandle;
%}

namespace ext {

    %extend shared_ptr {
        T* operator->() {
            return (*self).operator->();
        }
        #if defined(SWIGPYTHON)
        bool __bool__() {
            return !!(*self);
        }
        #else
        bool isNull() {
            return !(*self);
        }
        #endif
    }
}


template <class T>
class Handle {
  public:
    Handle();
    explicit Handle(const ext::shared_ptr<T>& p, bool registerAsObserver = true);
    ext::shared_ptr<T> operator->();
    ext::shared_ptr<T> currentLink();
    #if defined(SWIGPYTHON)
    %extend {
        bool __bool__() {
            return !self->empty();
        }
    }
    #else
    bool empty();
    #endif
};

template <class T>
class RelinkableHandle : public Handle<T> {
  public:
    RelinkableHandle();
    explicit RelinkableHandle(const ext::shared_ptr<T>& p, bool registerAsObserver = true);
    void linkTo(const ext::shared_ptr<T>& h, bool registerAsObserver = true);
    void reset();
};

%define swigr_list_converter(ContainerRType,
                             ContainerCType, ElemCType)
#if defined(SWIGR)
%Rruntime %{
setMethod('print', 'ContainerCType',
function(x) print(as(x, "ElemCType")))

setAs("ContainerCType", "ElemCType",
function(from) {if (from$size()) from[1:from$size()] else NULL} )

setAs("ElemCType", "ContainerCType",
function(from) { a <- ContainerRType(length(from));
sapply(1:length(from), function(n) {
a[n] <- from[n] } )
a
})
%}
#endif
%enddef

%inline %{
#if defined(SWIGPYTHON)
// This should be Py_hash_t, but SWIG does not know this type.
typedef long hash_t;
#else
typedef int hash_t;
#endif
%}

%define deprecate_feature(OldName, NewName)
#if defined(SWIGPYTHON)
%pythoncode %{
def OldName(*args, **kwargs):
    from warnings import warn
    warn(f'{OldName.__name__} is deprecated; use {NewName.__name__}', FutureWarning, stacklevel=2)
    return NewName(*args, **kwargs)
%}
#endif
%enddef

%{
#if defined(SWIGPYTHON)
#include <stdexcept>
#include <string_view>

class PyPtr {
  public:
    static PyPtr fromResult(PyObject* ptr, std::string_view errorMsg) {
        if (!ptr)
            throw std::runtime_error(errorMsg.data());
        return PyPtr(ptr);
    }

    static PyPtr fromBorrowed(PyObject* ptr) {
        Py_INCREF(ptr);
        return PyPtr(ptr);
    }

    static PyPtr fromNew(PyObject* ptr) { return PyPtr(ptr); }

    constexpr PyPtr() noexcept : ptr_(nullptr) {}
    constexpr PyPtr(std::nullptr_t) noexcept : ptr_(nullptr) {}

    PyPtr(const PyPtr& other) noexcept : ptr_(other.ptr_) {
        Py_XINCREF(ptr_);
    }

    PyPtr(PyPtr&& other) noexcept : ptr_(other.release()) {}

    ~PyPtr() {
        Py_XDECREF(ptr_);
    }

    PyPtr& operator=(const PyPtr& other) {
        Py_XINCREF(other.ptr_);
        auto prevPtr = std::exchange(ptr_, other.ptr_);
        Py_XDECREF(prevPtr);
        return *this;
    }

    PyPtr& operator=(PyPtr&& other) {
        auto newPtr = other.release();
        auto prevPtr = std::exchange(ptr_, newPtr);
        Py_XDECREF(prevPtr);
        return *this;
    }

    PyObject* get() const noexcept {
        return ptr_;
    }

    explicit operator bool() const noexcept {
        return ptr_ != nullptr;
    }

    [[nodiscard]] PyObject* release() noexcept {
        return std::exchange(ptr_, nullptr);
    }

    void reset() {
        Py_CLEAR(ptr_);
    }

  private:
    // NOTE: takes ownership of the passed pointer.
    explicit PyPtr(PyObject* ptr) noexcept : ptr_(ptr) {}

    PyObject* ptr_;
};

#define cpp_deprecate_feature(OldName, NewName) \
    PyErr_WarnEx(PyExc_FutureWarning, (#OldName " is deprecated; use " #NewName), 1)
#else
#define cpp_deprecate_feature(OldName, NewName)
#endif
%}

#endif