dengzedong
2024-11-05 20748641c04dc9a692d7bea3697f08c9c884c3d0
提交 | 用户 | 时间
449017 1 \#include "stdafx.h"
D 2 \#include "pch.h"
3 \#include "${pyName}.h"
4 \#include <string>
5 \#include "pyutils.h"
6 \#include "jni.h"
7 \#include "Python.h"
8 \#include "PyGILThreadLock.h"
9 \#include "convertutils.h"
10
11 using namespace std;
12
13 #{foreach} ($entity in $modelMethods)
14 jobject ${pyName}_${entity.methodName}(JNIEnv* env, #{foreach} ($column in [1..$entity.dataLength])jobjectArray data${column}, #{end}#{if}($entity.model==1)jobject model_path_, #{end}jobject settings)
15 {
16     try
17     {
18
19         PyGILThreadLock lock;
20         PyObject* pModule = create_py_module("${pyModule}.${pyName}");
21         if (pModule == NULL)
22         {
23             cout << "model error" << endl;
24             throw "模块调用失败!";
25         }
26
27         //*************导入函数******************
28         PyObject* pFunc = PyObject_GetAttrString(pModule, "${entity.methodName}");
29         if (pFunc == NULL)
30         {
31             cout << "func error" << endl;
32             throw "函数调用失败!";
33         }
34 #{foreach} ($column in [1..$entity.dataLength])
35         PyObject* data_${column} = ConvertJdarrToPydarr(env, data${column});
36 #{end}
37 #{if}($entity.model==1)
38         PyObject* model_path = ConvertJhmapToPydict(env, model_path_);
39 #{end}
40         PyObject* settings_1 = ConvertJhmapToPydict(env, settings);
41
42
43         //************构造参数数据 ********************
44 #{if}($entity.model==1)
45 #set($a=$entity.dataLength + 2)
46         PyObject* pArg = PyTuple_New(${a});
47 #{else}
48 #set($a=$entity.dataLength + 1)
49         PyObject* pArg = PyTuple_New(${a});
50 #{end}
51 #{foreach} ($column in [1..$entity.dataLength])
52 #set($a=$column - 1)
53         PyTuple_SetItem(pArg, ${a}, data_${column});
54 #{end}
55 #{if}($entity.model==1)
56         PyTuple_SetItem(pArg, ${entity.dataLength}, model_path);
57 #set($a=$entity.dataLength + 1)
58         PyTuple_SetItem(pArg, ${a}, settings_1);
59 #{else}
60         PyTuple_SetItem(pArg, ${entity.dataLength}, settings_1);
61 #{end}
62
63         /*PyTuple_SetItem(pArg, 1, parseCppData(data_in->feature_range));*/
64
65
66         //********************调用python方法计算*****************
67         PyObject* pReturn = PyEval_CallObject(pFunc, pArg);
68         if (pReturn == NULL)
69         {
70             cout << "return error" << endl;
71             throw "函数返回值异常!";
72         }
73         //*******************返回数据封装************************
74        // 开辟返回数据内存空间,转换数据,拆分数据
75
76         /*jobject result = jhmappydict_(env, pReturn);*/
77         jobject result = ConvertPydictToJhmap(env, pReturn);
78         if (result == NULL)
79         {
80             cout << "ConvertPydictToJhmap error" << endl;
81             throw "函数返回值异常!";
82         }
c7009e 83         /*delete pModule;
449017 84         pModule = nullptr;
D 85         delete pModule;
86
87         delete pFunc;
88         pFunc = nullptr;
89         delete pFunc;
90
91 #{foreach} ($column in [1..$entity.dataLength])
92         delete[] data_${column};
93         data_${column} = nullptr;
94         delete data_${column};
95
96 #{end}
97 #{if}($entity.model==1)
98         delete model_path;
99         model_path = nullptr;
100         delete model_path;
101 #{end}
102
103         delete settings_1;
104         settings_1 = nullptr;
105         delete settings_1;
106
107         delete[] pArg;
108         pArg = nullptr;
207486 109         delete pArg;
D 110
111         Py_DECREF(pReturn);
112         Py_DECREF(pFunc);
113         Py_DECREF(pModule);
114         Py_CLEAR(pModule);*/
449017 115
D 116         return result;
117     }
118     catch (const char* msg)
119     {
120         cout << "${entity.methodName} error" << endl;
121         return NULL;
122     }
123 }
124
125 #{end}