| 1 | /***************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | *****************************************************************************/ |
| 16 | #include "jpype.h" |
| 17 | #include "pyjp.h" |
| 18 | #include "jp_buffer.h" |
| 19 | #include "jp_primitive_accessor.h" |
| 20 | #include "jp_buffertype.h" |
| 21 | |
| 22 | JPBuffer::JPBuffer(const JPValue &value) |
| 23 | : m_Object(value.getValue().l) |
| 24 | { |
| 25 | m_Class = dynamic_cast<JPBufferType*>( value.getClass()); |
| 26 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 27 | JP_TRACE_IN("JPBuffer::JPBuffer"); |
| 28 | m_Address = frame.GetDirectBufferAddress(m_Object.get()); |
| 29 | m_Capacity = (Py_ssize_t) frame.GetDirectBufferCapacity(m_Object.get()); |
| 30 | m_Buffer.buf = m_Address; |
| 31 | m_Buffer.format = m_Format; |
| 32 | m_Format[0] = frame.orderBuffer(m_Object.get()) ? '<' : '>'; |
| 33 | m_Format[1] = m_Class->getType()[0]; |
| 34 | m_Format[2] = 0; |
| 35 | m_Buffer.itemsize = (Py_ssize_t) m_Class->getSize(); |
| 36 | m_Buffer.ndim = 1; |
| 37 | m_Buffer.readonly = frame.isBufferReadOnly(m_Object.get()); |
| 38 | m_Buffer.shape = &m_Capacity; |
| 39 | m_Buffer.strides = &m_Buffer.itemsize; |
| 40 | m_Buffer.suboffsets = 0; |
| 41 | m_Buffer.len = m_Buffer.itemsize * m_Capacity; |
| 42 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 43 | } |
| 44 | |
| 45 | JPBuffer::~JPBuffer() |
| 46 | = default; |
| 47 | |
| 48 | bool JPBuffer::isReadOnly() const |
| 49 | { |
| 50 | return m_Buffer.readonly != 0; |
| 51 | } |
| 52 | |
| 53 | Py_buffer& JPBuffer::getView() |
| 54 | { |
| 55 | return m_Buffer; |
| 56 | } |
| 57 | |
| 58 | bool JPBuffer::isValid() const |
| 59 | { |
| 60 | return m_Capacity != -1; |
| 61 | } |