Add a buffer example
This commit is contained in:
1 parent
3ac1a30e35
commit
bb0d076a88
6 files changed
+98
No files matched your search
@@ -0,0 +1,25 @@
|
||||
#include "c.h"
|
||||
|
||||
static const int LEN = 1024*1024;
|
||||
|
||||
static unsigned short int buffer[LEN] = {0,};
|
||||
|
||||
unsigned short int* GetBuffer()
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int GetBufferLen()
|
||||
{
|
||||
return LEN;
|
||||
}
|
||||
|
||||
unsigned short int GetBufferChecksum()
|
||||
{
|
||||
unsigned short int sum = 0;
|
||||
for (int i = 0; i < LEN; sum += buffer[i++]);
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
unsigned short* GetBuffer();
|
||||
int GetBufferLen();
|
||||
unsigned short GetBufferChecksum();
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import sys
|
||||
sys.path.insert(0, "../../build/default/examples/buffer")
|
||||
import c
|
||||
|
||||
print c.GetBufferLen()
|
||||
print c.GetBufferChecksum()
|
||||
buf = c.GetBuffer()
|
||||
buf[10] = chr(123)
|
||||
print c.GetBufferChecksum()
|
||||
print buf[10]
|
||||
@@ -0,0 +1,36 @@
|
||||
#! /usr/bin/env python
|
||||
|
||||
import sys
|
||||
|
||||
import pybindgen
|
||||
from pybindgen import ReturnValue, Parameter, Module, Function, FileCodeSink
|
||||
from pybindgen import CppMethod, CppConstructor, CppClass, Enum
|
||||
from pybindgen.typehandlers.base import ForwardWrapperBase
|
||||
|
||||
|
||||
class BufferReturn(ReturnValue):
|
||||
CTYPES = []
|
||||
|
||||
def __init__(self, ctype, length_expression):
|
||||
super(BufferReturn, self).__init__(ctype, is_const=False)
|
||||
self.length_expression = length_expression
|
||||
|
||||
def convert_c_to_python(self, wrapper):
|
||||
pybuf = wrapper.after_call.declare_variable("PyObject*", "pybuf")
|
||||
wrapper.after_call.write_code("%s = PyBuffer_FromReadWriteMemory(retval, (%s)*sizeof(short int));" % (pybuf, self.length_expression))
|
||||
wrapper.build_params.add_parameter("N", [pybuf], prepend=True)
|
||||
|
||||
|
||||
def my_module_gen(out_file):
|
||||
|
||||
mod = Module('c')
|
||||
mod.add_include('"c.h"')
|
||||
|
||||
mod.add_function("GetBuffer", BufferReturn("unsigned short int*", "GetBufferLen()"), [])
|
||||
mod.add_function("GetBufferLen", ReturnValue.new("int"), [])
|
||||
mod.add_function("GetBufferChecksum", ReturnValue.new("unsigned short"), [])
|
||||
|
||||
mod.generate(FileCodeSink(out_file))
|
||||
|
||||
if __name__ == '__main__':
|
||||
my_module_gen(sys.stdout)
|
||||
@@ -0,0 +1,20 @@
|
||||
## -*- python -*-
|
||||
|
||||
def build(bld):
|
||||
|
||||
gen = bld.new_task_gen(
|
||||
features='command',
|
||||
source='modulegen.py',
|
||||
target='cmodule.cc',
|
||||
command='${PYTHON} ${SRC[0]} > ${TGT[0]}')
|
||||
|
||||
if bld.env['CXX']:
|
||||
obj = bld.new_task_gen('cxx', 'shlib', 'pyext')
|
||||
obj.source = [
|
||||
'c.cc',
|
||||
'cmodule.cc'
|
||||
]
|
||||
obj.target = 'c'
|
||||
obj.install_path = None # do not install
|
||||
obj.includes = '.'
|
||||
|
||||
@@ -6,6 +6,7 @@ def set_options(opt):
|
||||
def build(bld):
|
||||
bld.add_subdirs('a b c d e f g h')
|
||||
bld.add_subdirs('callback')
|
||||
bld.add_subdirs('buffer')
|
||||
#if bld.env['HAVE_BOOST']:
|
||||
# bld.add_subdirs('shared_ptr')
|
||||
|
||||
|
||||
Reference in new issue
Block a user