datareader.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2019 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. #ifndef NCNN_DATAREADER_H
  15. #define NCNN_DATAREADER_H
  16. #include "platform.h"
  17. #if NCNN_STDIO
  18. #include <stdio.h>
  19. #endif
  20. #if NCNN_PLATFORM_API
  21. #if __ANDROID_API__ >= 9
  22. #include <android/asset_manager.h>
  23. #endif
  24. #endif // NCNN_PLATFORM_API
  25. namespace ncnn {
  26. // data read wrapper
  27. class NCNN_EXPORT DataReader
  28. {
  29. public:
  30. DataReader();
  31. virtual ~DataReader();
  32. #if NCNN_STRING
  33. // parse plain param text
  34. // return 1 if scan success
  35. virtual int scan(const char* format, void* p) const;
  36. #endif // NCNN_STRING
  37. // read binary param and model data
  38. // return bytes read
  39. virtual size_t read(void* buf, size_t size) const;
  40. // get model data reference
  41. // return bytes referenced
  42. virtual size_t reference(size_t size, const void** buf) const;
  43. };
  44. #if NCNN_STDIO
  45. class DataReaderFromStdioPrivate;
  46. class NCNN_EXPORT DataReaderFromStdio : public DataReader
  47. {
  48. public:
  49. explicit DataReaderFromStdio(FILE* fp);
  50. virtual ~DataReaderFromStdio();
  51. #if NCNN_STRING
  52. virtual int scan(const char* format, void* p) const;
  53. #endif // NCNN_STRING
  54. virtual size_t read(void* buf, size_t size) const;
  55. private:
  56. DataReaderFromStdio(const DataReaderFromStdio&);
  57. DataReaderFromStdio& operator=(const DataReaderFromStdio&);
  58. private:
  59. DataReaderFromStdioPrivate* const d;
  60. };
  61. #endif // NCNN_STDIO
  62. class DataReaderFromMemoryPrivate;
  63. class NCNN_EXPORT DataReaderFromMemory : public DataReader
  64. {
  65. public:
  66. explicit DataReaderFromMemory(const unsigned char*& mem);
  67. virtual ~DataReaderFromMemory();
  68. #if NCNN_STRING
  69. virtual int scan(const char* format, void* p) const;
  70. #endif // NCNN_STRING
  71. virtual size_t read(void* buf, size_t size) const;
  72. virtual size_t reference(size_t size, const void** buf) const;
  73. private:
  74. DataReaderFromMemory(const DataReaderFromMemory&);
  75. DataReaderFromMemory& operator=(const DataReaderFromMemory&);
  76. private:
  77. DataReaderFromMemoryPrivate* const d;
  78. };
  79. #if NCNN_PLATFORM_API
  80. #if __ANDROID_API__ >= 9
  81. class DataReaderFromAndroidAssetPrivate;
  82. class NCNN_EXPORT DataReaderFromAndroidAsset : public DataReader
  83. {
  84. public:
  85. explicit DataReaderFromAndroidAsset(AAsset* asset);
  86. virtual ~DataReaderFromAndroidAsset();
  87. #if NCNN_STRING
  88. virtual int scan(const char* format, void* p) const;
  89. #endif // NCNN_STRING
  90. virtual size_t read(void* buf, size_t size) const;
  91. private:
  92. DataReaderFromAndroidAsset(const DataReaderFromAndroidAsset&);
  93. DataReaderFromAndroidAsset& operator=(const DataReaderFromAndroidAsset&);
  94. private:
  95. DataReaderFromAndroidAssetPrivate* const d;
  96. };
  97. #endif // __ANDROID_API__ >= 9
  98. #endif // NCNN_PLATFORM_API
  99. } // namespace ncnn
  100. #endif // NCNN_DATAREADER_H