| 1 | /* Tencent is pleased to support the open source community by making ncnn available. |
| 2 | * |
| 3 | * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | * |
| 5 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | * in compliance with the License. You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://opensource.org/licenses/BSD-3-Clause |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software distributed |
| 11 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | * specific language governing permissions and limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #ifndef PYBIND11_NCNN_DATAREADER_H |
| 17 | #define PYBIND11_NCNN_DATAREADER_H |
| 18 | |
| 19 | #include <datareader.h> |
| 20 | |
| 21 | class DataReaderFromEmpty : public ncnn::DataReader |
| 22 | { |
| 23 | public: |
| 24 | #if NCNN_STRING |
| 25 | virtual int scan(const char* format, void* p) const |
| 26 | { |
| 27 | return 0; |
| 28 | } |
| 29 | #endif // NCNN_STRING |
| 30 | virtual size_t read(void* buf, size_t size) const |
| 31 | { |
| 32 | memset(buf, 0, size); |
| 33 | return size; |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | template<class Base = ncnn::DataReader> |
| 38 | class PyDataReader : public Base |
| 39 | { |
| 40 | public: |
| 41 | using Base::Base; // Inherit constructors |
| 42 | #if NCNN_STRING |
| 43 | int scan(const char* format, void* p) const override |
| 44 | { |
| 45 | PYBIND11_OVERRIDE(int, Base, scan, format, p); |
| 46 | } |
| 47 | #endif // NCNN_STRING |
| 48 | size_t read(void* buf, size_t size) const override |
| 49 | { |
| 50 | PYBIND11_OVERRIDE(size_t, Base, read, buf, size); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | template<class Other> |
| 55 | class PyDataReaderOther : public PyDataReader<Other> |
| 56 | { |
| 57 | public: |
| 58 | using PyDataReader<Other>::PyDataReader; |
| 59 | #if NCNN_STRING |
| 60 | int scan(const char* format, void* p) const override |
| 61 | { |
| 62 | PYBIND11_OVERRIDE(int, Other, scan, format, p); |
| 63 | } |
| 64 | #endif // NCNN_STRING |
| 65 | size_t read(void* buf, size_t size) const override |
| 66 | { |
| 67 | PYBIND11_OVERRIDE(size_t, Other, read, buf, size); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | #endif |