-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonBuff.cpp
More file actions
65 lines (54 loc) · 1.24 KB
/
PythonBuff.cpp
File metadata and controls
65 lines (54 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright © 2023 CCP ehf.
#include "PythonBuff.h"
//A simple Com wrapper for the python buffer
PythonBuff::PythonBuff( PyObject* obj ) :
m_buff( nullptr ),
m_size( 0 ),
m_pos( 0 ),
m_refcount( 0 )
{
if( !PyObject_CheckBuffer( obj ) )
{
PyErr_SetString( PyExc_TypeError, "PythonbBuff object must have a buffer interface." );
return;
}
Py_buffer view;
if( PyObject_GetBuffer( obj, &view, 0 ) != 0 )
{
PyErr_SetString( PyExc_TypeError, "PythonbBuff object must have a buffer interface." );
return;
}
if( !PyBuffer_IsContiguous( &view, 'A' ) )
{
PyErr_SetString( PyExc_TypeError, "PythonbBuff object buffer must be contiguous." );
}
m_size = view.len;
m_buff = (char*)view.buf;
}
PythonBuff::~PythonBuff()
{}
HRESULT WINAPI PythonBuff::Read(void* pv, ULONG cb, ULONG* got)
{
HRESULT hr = S_OK;
if( cb > m_size - m_pos )
{
cb = (ULONG)( m_size - m_pos );
hr = S_FALSE;
}
memcpy( pv, m_buff + m_pos, cb );
m_pos += cb;
if (got)
*got = cb;
return hr;
}
HRESULT WINAPI PythonBuff::QueryInterface(REFIID riid, void** ppv)
{
if (riid == IID_IUnknown)
*ppv = (IUnknown*)this;
else if (riid == IID_ISequentialStream)
*ppv = (IUnknown*)(ISequentialStream*)this;
else
return E_NOINTERFACE;
++m_refcount;
return S_OK;
}