Skip to content

Commit

Permalink
Add getMemoryviewPy feature
Browse files Browse the repository at this point in the history
  • Loading branch information
bengineerd committed Oct 15, 2024
1 parent 986489a commit 86a87ed
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
11 changes: 9 additions & 2 deletions include/rogue/interfaces/stream/Frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,17 @@ class Frame : public rogue::EnableSharedFromThis<rogue::interfaces::stream::Fram
//! Python Frame data read function
/** Read data from Frame into a python bytearray which is allocated and returned
*
* Exposed as readBa() to Python
* Exposed as getBa() to Python
* @param offset First location of Frame data to copy to byte array
*/
boost::python::object readBytearrayPy(uint32_t offset);
boost::python::object getBytearrayPy(uint32_t offset);

//! Python Frame data read function
/** Read data from Frame into a python bytearray which is allocated and returned as a memoryview
*
* Exposed as getMemoryview() to Python
*/
boost::python::object getMemoryviewPy();


//! Python Frame data write function
Expand Down
30 changes: 27 additions & 3 deletions src/rogue/interfaces/stream/Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ void ris::Frame::readPy(boost::python::object p, uint32_t offset) {
PyBuffer_Release(&pyBuf);
}

//! Allocate a bytearray and read bytes from frame into it, starting at offset
bp::object ris::Frame::readBytearrayPy(uint32_t offset) {
//! Allocate a bytearray and read bytes from frame into it starting at offset, return array
bp::object ris::Frame::getBytearrayPy(uint32_t offset) {
// Get the size of the frame
uint32_t size = getPayload();

Expand All @@ -384,6 +384,29 @@ bp::object ris::Frame::readBytearrayPy(uint32_t offset) {
return byteArray;
}

//! Allocate a bytearray and from frame into it starting at offset, return memoryview to array
bp::object ris::Frame::getMemoryviewPy() {
// Get the size of the frame
uint32_t size = getPayload();

// Create a Python bytearray to hold the data
bp::object byteArray(bp::handle<>(PyByteArray_FromStringAndSize(nullptr, size)));

this->readPy(byteArray, 0);

// Create a memoryview from the bytearray
PyObject* memoryView = PyMemoryView_FromObject(byteArray.ptr());
if (!memoryView) {
throw(rogue::GeneralError::create("Frame::getMemoryviewPy",
"Failed to create memoryview."));
}

// Return the memoryview as a Python object
return bp::object(bp::handle<>(memoryView));

}



//! Write python buffer to frame, starting at offset. Python Version
void ris::Frame::writePy(boost::python::object p, uint32_t offset) {
Expand Down Expand Up @@ -530,8 +553,9 @@ void ris::Frame::setup_python() {
.def("getPayload", &ris::Frame::getPayload)
.def("read", &ris::Frame::readPy, (
bp::arg("offset")=0))
.def("readBa", &ris::Frame::readBytearrayPy, (
.def("getBa", &ris::Frame::getBytearrayPy, (
bp::arg("offset")=0))
.def("getMemoryview", &ris::Frame::getMemoryviewPy)
.def("write", &ris::Frame::writePy, (
bp::arg("offset")=0))
.def("setError", &ris::Frame::setError)
Expand Down

0 comments on commit 86a87ed

Please sign in to comment.