File: jpype.h

package info (click to toggle)
python-jpype 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,984 kB
  • sloc: python: 18,767; cpp: 17,931; java: 8,448; xml: 1,305; makefile: 154; sh: 35
file content (199 lines) | stat: -rw-r--r-- 5,864 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
/*****************************************************************************
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

   See NOTICE file for details.
 *****************************************************************************/
#ifndef _JPYPE_H_
#define _JPYPE_H_

#ifdef __GNUC__
// Python requires char* but C++ string constants are const char*
#pragma GCC diagnostic ignored "-Wwrite-strings"
#endif

#ifdef WIN32

#ifndef __GNUC__ // Then this must mean a variant of GCC on win32 ...
#pragma warning (disable:4786)
#endif

#if defined(__CYGWIN__)
// jni_md.h does not work for cygwin.  Use this instead.
#elif defined(__GNUC__)
// JNICALL causes problem for function prototypes .. since I am not defining any JNI methods there is no need for it
#undef JNICALL
#define JNICALL
#endif

#endif

#include <jni.h>

// Define this and use to allow destructors to throw in C++11 or later
#if defined(_MSC_VER)

// Visual Studio C++ does not seem have changed __cplusplus since 1997
// see: https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170&viewFallbackFrom=vs-2019
#if (_MSVC_LAND >= 201402)
#define NO_EXCEPT_FALSE noexcept(false)
#else
#define NO_EXCEPT_FALSE throw(JPypeException)
#endif

#else

// For all the compilers that understand standards
#if (__cplusplus >= 201103L)
#define NO_EXCEPT_FALSE noexcept(false)
#else
#define NO_EXCEPT_FALSE throw(JPypeException)
#endif

#endif

#include <map>
#include <string>
#include <vector>

using std::map;
using std::string;
using std::vector;

#ifdef JP_INSTRUMENTATION

template <size_t i>
constexpr uint32_t _hash(const char *q, uint32_t v)
{
	return _hash < i - 1 > (q + 1, v * 0x1a481023 + q[0]);
}

template <>
constexpr uint32_t _hash<0>(const char *q, uint32_t v)
{
	return v;
}
#define compile_hash(x) _hash<sizeof(x)-1>(x, 0)

extern void PyJPModuleFault_throw(uint32_t code);
extern int PyJPModuleFault_check(uint32_t code);
#define JP_TRACE_IN(X, ...) try { PyJPModuleFault_throw(compile_hash(X));
#define JP_FAULT_RETURN(X, Y)  if (PyJPModuleFault_check(compile_hash(X))) return Y
#define JP_BLOCK(X)  if (PyJPModuleFault_check(compile_hash(X))==0)
#else
#define JP_FAULT_RETURN(X, Y)  if (false) while (false)
#define JP_BLOCK(X)  if (false) while (false)
#endif

/** Definition of commonly used template types */
using StringVector = vector<string>;

/**
 * Converter are used for bulk byte transfers from Python to Java.
 */
using jconverter = jvalue (*)(void *) ;

/**
 * Create a converter for a bulk byte transfer.
 *
 * Bulk transfers do not check for range and may be lossy.  These are only
 * triggered when a transfer either using memoryview or a slice operator
 * assignment from a buffer object (such as numpy.array).  Converters are
 * created once at the start of the transfer and used to convert each
 * byte by casting the memory and then assigning to the jvalue union with
 * the requested type.
 *
 * Byte order transfers are not supported by the Python buffer API and thus
 * have not been implemented.
 *
 * @param from is a Python struct designation
 * @param itemsize is the size of the Python item
 * @param to is the desired Java primitive type
 * @return a converter function to convert each member.
 */
extern jconverter getConverter(const char* from, int itemsize, const char* to);

extern bool _jp_cpp_exceptions;

// Types
class JPClass;
class JPValue;
class JPProxy;
class JPArray;
class JPArrayClass;
class JPArrayView;
class JPBoxedType;
class JPPrimitiveType;
class JPStringType;

// Members
class JPMethod;
class JPMethodDispatch;
class JPField;

// Services
class JPTypeManager;
class JPClassLoader;
class JPContext;
class JPBuffer;
class JPPyObject;

extern "C" using JCleanupHook = void (*)(void *) ;
extern "C" struct JPConversionInfo;

using JPClassList = vector<JPClass *>;
using JPFieldList = vector<JPField *>;
using JPMethodDispatchList = vector<JPMethodDispatch *>;
using JPMethodList = vector<JPMethod *>;

class JPResource
{
public:
	virtual ~JPResource() = 0;
} ;

// Macros for raising an exception with jpype
//   These must be macros so that we can update the pattern and
//   maintain the appropriate auditing information.  C++ does not
//   have a lot for facilities to make this easy.
#define JP_RAISE_PYTHON()                   { throw JPypeException(JPError::_python_error, nullptr, JP_STACKINFO()); }
#define JP_RAISE_OS_ERROR_UNIX(err, msg)    { throw JPypeException(JPError::_os_error_unix,  msg, err, JP_STACKINFO()); }
#define JP_RAISE_OS_ERROR_WINDOWS(err, msg) { throw JPypeException(JPError::_os_error_windows,  msg, err, JP_STACKINFO()); }
#define JP_RAISE_METHOD_NOT_FOUND(msg)      { throw JPypeException(JPError::_method_not_found, nullptr, msg, JP_STACKINFO()); }
#define JP_RAISE(type, msg)                 { throw JPypeException(JPError::_python_exc, type, msg, JP_STACKINFO()); }

#ifndef PyObject_HEAD
struct _object;
using PyObject = _object;
#endif

// Base utility headers
#include "jp_javaframe.h"
#include "jp_context.h"
#include "jp_exception.h"
#include "jp_tracer.h"
#include "jp_pythontypes.h"
#include "jp_typemanager.h"
#include "jp_encoding.h"
#include "jp_modifier.h"
#include "jp_match.h"

// Other header files
#include "jp_classhints.h"
#include "jp_method.h"
#include "jp_value.h"
#include "jp_class.h"

// Primitives classes
#include "jp_primitivetype.h"

#endif // _JPYPE_H_