aboutsummaryrefslogtreecommitdiffstats
path: root/libshiboken/shibokenbuffer.cpp
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2011-05-25 16:23:22 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:15:24 -0300
commit0e6d4cc1d151724f56c6ccf8c431077e2c778b26 (patch)
tree3717e96dfd1e22bc75ce409cdca20c136d056c96 /libshiboken/shibokenbuffer.cpp
parent22bed1fb96a37bdf9d032a801f5577f81832fe76 (diff)
Added Shiboken buffer interface.
This interface is just a wrapper to the differents API's provided by Python to deal with memory buffers in various versions of Python, so is recommended to use this API to deal with Python memory buffers instead of the CPython API. If you want to have a Python buffer as argument of any function just change the argument type to "PyBuffer" and the generator will handle it right regarding to type checking. Reviewer: Renato Araújo <renato.filho@openbossa.org> Luciano Wolf <luciano.wolf@openbossa.org>
Diffstat (limited to 'libshiboken/shibokenbuffer.cpp')
-rw-r--r--libshiboken/shibokenbuffer.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/libshiboken/shibokenbuffer.cpp b/libshiboken/shibokenbuffer.cpp
new file mode 100644
index 000000000..67b3ba726
--- /dev/null
+++ b/libshiboken/shibokenbuffer.cpp
@@ -0,0 +1,52 @@
+/*
+* This file is part of the Shiboken Python Bindings Generator project.
+*
+* Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+*
+* Contact: PySide team <contact@pyside.org>
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU Lesser General Public
+* License as published by the Free Software Foundation; either
+* version 2.1 of the License, or (at your option) any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this library; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "shibokenbuffer.h"
+#include <cstdlib>
+#include <cstring>
+
+bool Shiboken::Buffer::checkType(PyObject* pyObj)
+{
+ return PyObject_CheckReadBuffer(pyObj);
+}
+
+void* Shiboken::Buffer::getPointer(PyObject* pyObj, Py_ssize_t* size)
+{
+ const void* buffer = 0;
+ Py_ssize_t bufferSize = 0;
+
+ PyObject_AsReadBuffer(pyObj, &buffer, &bufferSize);
+
+ if (size)
+ *size = bufferSize;
+ return const_cast<void*>(buffer);
+}
+
+PyObject* Shiboken::Buffer::newObject(void* memory, Py_ssize_t size, Type type)
+{
+ return type == ReadOnly ? PyBuffer_FromMemory(memory, size) : PyBuffer_FromReadWriteMemory(memory, size);
+}
+
+PyObject* Shiboken::Buffer::newObject(const void* memory, Py_ssize_t size)
+{
+ return newObject(const_cast<void*>(memory), size, ReadOnly);
+}