python调用c++类方法(2)
testpy.cpp:
#include<iostream>
#include<vector> struct point{
float pointx;
float pointy;
float pointz;
}; struct pose{
float x;
float y;
float z;
int* data; point location;
}; class MyTest
{
public:
MyTest();
~MyTest();
int myTest(int* myData, int dataNum, pose& mypose); private: }; MyTest::MyTest()
{
} MyTest::~MyTest()
{
} int MyTest::myTest(int* myData, int dataNum, pose& mypose)
{
if (dataNum>)
{
for (size_t i = ; i < dataNum; i++)
{
*myData = i + ;
myData += ;
}
}
mypose.x=1.0;
mypose.y=2.0;
mypose.z=3.0;
mypose.location.pointx=4.0;
mypose.location.pointy=5.0;
mypose.location.pointz=6.0; if (dataNum>)
{
for (size_t i = ; i < dataNum; i++)
{
*(mypose.data) = i + ;
mypose.data += ;
}
} return ;
} extern "C" { MyTest myObj; int myTest(int* myData, int dataNum, pose& mypose)
{
return myObj.myTest(myData, dataNum, mypose);
} }
pythonCallCpp.py:
#!/usr/bin/python
# -*- coding: utf-8 -*- from ctypes import *
import ctypes
import numpy INPUT = c_int * 100
myinput = INPUT() INPUT01 = c_int * 100
myinput01 = INPUT01() #arr = numpy.array([1, 2, 3, 4],dtype = numpy.int)
arr1 = numpy.zeros(100, dtype=ctypes.c_int) #if not arr.flags['C_CONTIGUOUS']: # 若不是C连续内存,强制转换
#arr = numpy.ascontiguous(arr, dtype = numpy.dtype)
if not arr1.flags['C_CONTIGUOUS']:
arr1 = numpy.ascontiguous(arr1, dtype = numpy.dtype)
#c_arr = numpy.ctypeslib.as_ctypes(arr) # array 转为ctypes类型
#c_arr1 = numpy.ctypeslib.as_ctypes(arr1) cptr = arr1.ctypes.data_as(POINTER(ctypes.c_int)) # 取指针 so = ctypes.cdll.LoadLibrary
lib = so("/opt/fp100/libtest.so")
print 'myTest(int* myData, int dataNum, pose& mypos)' class point(ctypes.Structure):
_fields_ = [("pointx", ctypes.c_float),("pointy", ctypes.c_float),("pointz", ctypes.c_float)] pointtemp = point(0.0, 0.0, 0.0) class pose(ctypes.Structure):
_fields_ = [("x", ctypes.c_float),("y", ctypes.c_float),("z", ctypes.c_float),("data", POINTER(ctypes.c_int)),("location", point)] #POINTER(ctypes.c_int) posetemp = pose()
posetemp.x=0.0
posetemp.y=0.0
posetemp.z=0.0 posetemp.data = myinput01
posetemp.location = pointtemp #posetemp = pose() lib.myTest(myinput, 100, ctypes.byref(posetemp)) print myinput[0]
print myinput[1]
print myinput[2]
print myinput[3]
print posetemp.x
print posetemp.y
print posetemp.z
print posetemp.location.pointx
print posetemp.location.pointy
print posetemp.location.pointz print posetemp.data[0]
结论:存在一个问题,结构体内套指针时,出现地址紊乱,不知如何解决?
python调用c++类方法(2)的更多相关文章
- python 调用c++类方法(1)
myTest.cpp: #include<iostream> #include<vector> class MyTest { public: MyTest(); ~MyTest ...
- python 静态方法、类方法(二)
<Python静态方法.类方法>一文中曾用在类之外生成函数的方式,来计算类的实例的个数.本文将探讨用静态方法和类方法来实现此功能. 一使用静态方法统计实例 例1.static.py # - ...
- python类:类方法和静态方法
http://blog.csdn.net/pipisorry/article/details/49516185 面相对象程序设计中,类方法和静态方法是经常用到的两个术语.逻辑上讲:类方法是只能由类名调 ...
- Python 实例方法、类方法、静态方法的区别与作用
Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢?如何调用的呢?它们又有何区别和作用呢?且看下文. 首先,这三种方法都定义在类中.下面我先简单说一下怎么定义 ...
- python静态方法和类方法
静态方法和类方法在python2.2中被引用,经典类和新式类都可以使用.同时,一对内建函数:staticmethod和classmethod被引入,用来转化类中某一方法为这两种方法之一. 静态方法: ...
- 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别
[面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...
- Python 静态方法、类方法和属性方法
Python 静态方法.类方法和属性方法 静态方法(staticmethod) staticmethod不与类或者对象绑定,类和实例对象都可以调用,没有自动传值效果,Python内置函数staticm ...
- 【初学python】使用python调用monkey测试
目前公司主要开发安卓平台的APP,平时测试经常需要使用monkey测试,所以尝试了下用python调用monkey,代码如下: import os apk = {'j': 'com.***.test1 ...
- python调用py中rar的路径问题。
1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...
随机推荐
- import cycle not allowed in test
写个 sdk 的测试时报错 import cycle not allowed in test 后发现因为测试文件内多写了导入同包路径. 同 package 下的 xxx_test.go 内不需要额外 ...
- uoj #242【UR #16】破坏蛋糕
uoj 考虑把那最后一条直线拎出来,并且旋转到和\(y\)轴平行(其他直线同时一起旋转),然后它和其他直线相交形成\(n+1\)个区间,现在要知道这些区间是否处在一个面积有限的区域 可以发现一段在有限 ...
- Win7 MongoDB可视化工具Robo 3T 1.2.1(robomongo)的安装使用
软件版本: Robo 3T 1.2.1 下载网址: https://robomongo.org/campaign 进入robomongo官网,点击download,进入下载页面 这里选择下载 Robo ...
- python实现观察者模式
python实现观察者模式 前言 有时,我们希望在一个对象的状态改变时更新另外一组对象.在MVC模式中有这样一个非 常常见的例子,假设在两个视图(例如,一个饼图和一个电子表格)中使用同一个模型的数据, ...
- PHP WEB 引擎缓存加速优化
PHP 缓存加速器介绍 操作码缓存 请求一个 PHP 程序时,PHP 引擎会解析程序,并且将编译码作为特定操作码.这是要执行的代 码的一种二进制表示形式.随后,此操作码有 PHP 引擎执行并丢弃.操作 ...
- windows CMD常用基础命令
一.ping 主要是测试本机TCP/IP协议配置正确性与当前网络现状. ping命令的基本使用格式是: ping IP地址/主机名/域名 [-t] [-a] [-n count] [-l size] ...
- 通过telnet自动下载cfg配置文件
源代码如下: import telnetlib import logging import datetime import csv import time def telnetfunc(enbid, ...
- SpringMVC POJO入参过程分析
SpringMVC确定目标方法POJO类型的入参过程 1.确认一个key: (1).若目标方法的POJO类型的参数没有使用@ModelAttribute作为修饰,则key为POJO类名第一个字母的小写 ...
- 【新手】【十分钟上手系列-一】快速开发vue插件
2018.6.28 在这浮躁的前端娱乐圈,不会三两个新框架都觉得自己不是前端.哦,不是我说的.说到底.原生才是重中之重.加油. vue用了大半年多,一直在用ui库,插件等,没有自己的东西. 想想连个v ...
- Delphi---ShellExecute跨进程调用exe
测试环境:Delphi7 + Win7 发起端 unit uRequest; interface uses Windows, Messages, SysUtils, Variants, Classes ...