DAWorkbench 0.0.1
DAWorkbench API
载入中...
搜索中...
未找到
DAPyObjectWrapper.h
1#ifndef DAPYOBJECTWRAPPER_H
2#define DAPYOBJECTWRAPPER_H
3#include "DAPyBindQtGlobal.h"
4#include "DAPybind11InQt.h"
5#include <QVariant>
6#include <functional>
7
8namespace DA
9{
14class DAPYBINDQT_API DAPyObjectWrapper
15{
16public:
20 using ErrCallback = std::function< void(const char*) >;
21
22public:
26 DAPyObjectWrapper(const pybind11::object& obj);
27 DAPyObjectWrapper(pybind11::object&& obj);
28 virtual ~DAPyObjectWrapper();
29 // 判断是否为none
30 bool isNone() const;
31 // 操作符
32 DAPyObjectWrapper& operator=(const DAPyObjectWrapper& obj);
33 DAPyObjectWrapper& operator=(DAPyObjectWrapper&& obj);
34 DAPyObjectWrapper& operator=(const pybind11::object& obj);
35 DAPyObjectWrapper& operator=(pybind11::object&& obj);
36 // 比较操作符
37 bool operator==(void* ptr) const;
38 bool operator==(const pybind11::object& obj) const;
39 bool operator==(const DAPyObjectWrapper& obj) const;
40 bool operator!=(void* ptr) const
41 {
42 return !(*this == ptr);
43 }
44 bool operator!=(const pybind11::object& obj) const
45 {
46 return !(*this == obj);
47 }
48 bool operator!=(const DAPyObjectWrapper& obj) const
49 {
50 return !(*this == obj);
51 }
52 // bool操作符可直接进行isNone判断
53 explicit operator bool() const;
54 // 统一异常处理函数
55 void dealException(const std::exception& e) const;
56 // 深拷贝
57 DAPyObjectWrapper deepCopy() const;
58
59public:
60 pybind11::object& object()
61 {
62 return _object;
63 }
64 const pybind11::object& object() const
65 {
66 return _object;
67 }
68
69public:
70 // 转换为QVariant
71 QVariant toVariant() const;
72 // 判断类型
73 bool isinstance(const pybind11::handle& type) const;
74 bool isInt() const;
75 bool isModule() const;
76 bool isFloat() const;
77 bool isStr() const;
78 bool isBool() const;
79 bool isList() const;
80 bool isDict() const;
81 bool isTuple() const;
82 bool isCallable() const;
83 bool isSequence() const;
84 bool isNumeric() const;
85 // 设置错误处理回调
86 void setErrCallback(const ErrCallback& fun);
87 ErrCallback getErrCallback() const;
88
89public:
90 // 重载
91 pybind11::object attr(const char* c_att);
92 pybind11::object attr(const char* c_att) const;
93 // 方法调用
94 template< typename... Args >
95 pybind11::object call(Args&&... args);
96
97public:
98 // 通用的python函数封装
99 QString __name__() const;
100 QString __str__() const;
101 QString __repr__() const;
102 // 对象信息
103 QString typeName() const;
104 size_t refCount() const;
105
106protected:
107 pybind11::object _object;
108 ErrCallback _errcallback;
109};
110
148template< typename... Args >
149pybind11::object DAPyObjectWrapper::call(Args&&... args)
150{
151 if (!isCallable()) {
152 return pybind11::none();
153 }
154 return _object(std::forward< Args >(args)...);
155}
156
157} // namespace DA
158Q_DECLARE_METATYPE(DA::DAPyObjectWrapper)
159#endif // DAPYOBJECTWRAPPER_H
这是针对pubind11::object的封装
Definition DAPyObjectWrapper.h:15
std::function< void(const char *) > ErrCallback
异常错误回调函数
Definition DAPyObjectWrapper.h:20
pybind11::object call(Args &&... args)
直接调用Python可调用对象
Definition DAPyObjectWrapper.h:149
序列化类都是带异常的,使用中需要处理异常
Definition AppMainWindow.cpp:44