简体   繁体   English

具有函数和python ctypes的结构

[英]structures with functions and python ctypes

I have ac library I would like to wrap with python 2.7 ctypes. 我有一个ac库我想用python 2.7 ctypes包装。

typedef struct SRSLTE_API{
  void *ptr;
  uint32_t R;
  uint32_t K;
  uint32_t framebits;
  bool tail_biting;
  float gain_quant; 
  int16_t gain_quant_s; 
  int (*decode) (void*, uint8_t*, uint8_t*, uint32_t);
  int (*decode_f) (void*, float*, uint8_t*, uint32_t);
  void (*free) (void*);
  uint8_t *tmp;
  uint8_t *symbols_uc;
}srslte_viterbi_t;

How do I create this structure in python? 如何在python中创建此结构? This is what I currently have. 这就是我现在拥有的。

from ctypes import *
class srslte_viterbi_t(Structure):
    _fields_ = [("ptr", c_void_p),
                ("R", c_uint),
                ("K", c_uint),
                ("framebits", c_uint),
                ("tail_biting", c_bool),
                ("gain_quant", c_float),
                ("gain_quant_s", c_short),
                ("decode", POINTER(c_int)),
                ("decode_f", POINTER(c_int)),
                ("free", c_void_p),
                ("tmp", POINTER(c_ubyte)),
                ("symbols_uc", POINTER(c_ubyte))
                ]
viterbi_t = srslte_viterbi_t(None, c_uint(0), c_uint(0), c_uint(0), 
    c_bool(False), c_float(0.0), c_short(0), None, None, None, None, None)

This structure compiles but does not give the correct results. 此结构编译但未给出正确的结果。 I fear I am not allocating the decode functions correctly? 我担心我没有正确分配解码功能? What is the line 什么是线

int (*decode) (void*, uint8_t*, uint8_t*, uint32_t);

Doing in a structure anyway? 无论如何做一个结构?

This is the correct definition of the struct: 这是结构的正确定义:

from ctypes import *

class srslte_viterbi_t(Structure):
    _fields_ = [
        ('ptr',c_void_p),
        ('R',c_uint32),
        ('K',c_uint32),
        ('framebits',c_uint32),
        ('tail_biting',c_bool),
        ('gain_quant',c_float),
        ('gain_quant_s',c_int16),
        ('decode',CFUNCTYPE(c_int,c_void_p,POINTER(c_uint8),POINTER(c_uint8),c_uint32)),
        ('decode_f',CFUNCTYPE(c_int,c_void_p,POINTER(c_float),POINTER(c_uint8),c_uint32)),
        ('free',CFUNCTYPE(None,c_void_p)),
        ('tmp',POINTER(c_uint8)),
        ('symbols_uc',POINTER(c_uint8))]

viterbi_t = srslte_viterbi_t()

The structure is zero-initialized by ctypes when instantiated. 实例化时,结构由ctypes零初始化。

The decode line in the structure is a function pointer. 结构中的解码行是函数指针。 CFUNCTYPE is used to define the function pointer return type and arguments. CFUNCTYPE用于定义函数指针返回类型和参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM