-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython_common.h
More file actions
107 lines (82 loc) · 3.42 KB
/
python_common.h
File metadata and controls
107 lines (82 loc) · 3.42 KB
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
#ifndef DELTIX_API_PYTHON_COMMON_H_
#define DELTIX_API_PYTHON_COMMON_H_
#include "Python.h"
#include "common.h"
#include <iostream>
namespace TbApiImpl {
namespace Python {
const std::string MODULE_NAME = "tbapi";
const std::string MESSAGE_OBJECT_CLASS_NAME = "InstrumentMessage";
const std::string TYPE_ID_PROPERTY = "typeId";
const std::string TYPE_NAME_PROPERTY = "typeName";
const std::string INSTRUMENT_ID_PROPERTY = "instrumentId";
const std::string INSTRUMENT_TYPE_PROPERTY = "instrumentType";
const std::string SYMBOL_PROPERTY = "symbol";
const std::string TIMESTAMP_PROPERTY = "timestamp";
//functions return false, if field_value is NULL, Py_None or type_mismatch is true
//type_mismatch is true, when type of field_value not corresponds to return value of function
bool getStringValue(PyObject *field_value, std::string &ret_value, bool &type_mismatch);
bool getInt32Value(PyObject *field_value, int32_t &ret_value, bool &type_mismatch);
bool getInt64Value(PyObject *field_value, int64_t &ret_value, bool &type_mismatch);
bool getDoubleValue(PyObject *field_value, double &ret_value, bool &type_mismatch);
bool getBooleanValue(PyObject *field_value, bool &ret_value);
bool getStringValue(PyObject *message, const std::string &field_name, PyObject *field_key, std::string &ret_value);
bool getInt32Value(PyObject *message, const std::string &field_name, PyObject *field_key, int32_t &ret_value);
bool getInt64Value(PyObject *message, const std::string &field_name, PyObject *field_key, int64_t &ret_value);
//bool getDoubleValue(PyObject *message, const std::string &field_name, PyObject *field_key, double &ret_value);
//bool getBooleanValue(PyObject *message, const std::string &field_name, PyObject *field_key, bool &ret_value);
//todo: should be singletone
class PythonTbApiModule {
public:
PythonTbApiModule() {
tbapi_module_ = PyImport_ImportModule(MODULE_NAME.c_str());
if (tbapi_module_ == NULL)
THROW_EXCEPTION("Module '%32s' is not loaded.", MODULE_NAME.c_str());
module_dict_ = PyModule_GetDict(tbapi_module_);
instrument_message_class_ = PyDict_GetItemString(module_dict_, MESSAGE_OBJECT_CLASS_NAME.c_str());
if (instrument_message_class_ == NULL)
THROW_EXCEPTION("Class '%32s' not found in module '%32s'.", MESSAGE_OBJECT_CLASS_NAME.c_str(), MODULE_NAME.c_str());
}
~PythonTbApiModule() {
Py_XDECREF(tbapi_module_);
}
PyObject * newInstrumentMessageObject() {
if (instrument_message_class_ == NULL)
THROW_EXCEPTION("Class '%32s' not found in module '%32s'.", MESSAGE_OBJECT_CLASS_NAME.c_str(), MODULE_NAME.c_str());
PyObject *message_object = PyObject_CallObject(instrument_message_class_, NULL);
return message_object;
}
private:
PyObject *tbapi_module_ = NULL;
PyObject *module_dict_ = NULL;
PyObject *instrument_message_class_ = NULL;
};
//RAII for new reference of PyObject *
class PythonRefHolder {
public:
PythonRefHolder(PyObject *object) {
object_ = object;
}
~PythonRefHolder() {
Py_XDECREF(object_);
}
PyObject * getReference() {
return object_;
}
private:
PyObject *object_;
};
class PythonGILLockHolder {
public:
PythonGILLockHolder() {
state_ = PyGILState_Ensure();
}
~PythonGILLockHolder() {
PyGILState_Release(state_);
}
private:
PyGILState_STATE state_;
};
}
}
#endif //DELTIX_API_PYTHON_COMMON_H_