nndeploy C++ API  0.2.0
nndeploy C++ API
file_loader.h
Go to the documentation of this file.
1 //
2 // FileLoader.hpp
3 // MNN
4 //
5 // Created by MNN on 2019/07/04.
6 // Copyright © 2018, Alibaba Group Holding Limited
7 //
8 
9 #include <mutex>
10 #include <vector>
11 
13 #include "nndeploy/base/macro.h"
14 
15 namespace nndeploy {
16 namespace base {
17 
19  public:
20  BaseLoader() = default;
21  virtual ~BaseLoader() = default;
22  virtual bool read(char* buffer, int64_t size) = 0;
23 };
24 
26  public:
27  FileLoader(const char* file, bool init = false);
28 
30 
31  bool read();
32 
33  static bool write(const char* filePath,
34  std::pair<const void*, size_t> cacheInfo);
35 
36  bool valid() const { return mFile != nullptr; }
37  inline size_t size() const { return mTotalSize; }
38  inline std::string path() const { return mFilePath; }
39 
40  // bool merge(AutoStorage<uint8_t>& buffer);
41 
42  int offset(int64_t offset);
43 
44  bool read(char* buffer, int64_t size);
45 
46  private:
47  void _init();
48  std::vector<std::pair<size_t, void*>> mBlocks;
49  FILE* mFile = nullptr;
50  static const int gCacheSize = 4096;
51  size_t mTotalSize = 0;
52  std::string mFilePath;
53  bool mInited = false;
54 };
55 
57  public:
58  MemoryLoader(unsigned char* ptr) : buffer_(ptr) {}
59  virtual bool read(char* dst, int64_t size) override {
60  ::memcpy(dst, buffer_, size);
61  buffer_ += size;
62  return true;
63  }
64 
65  private:
66  unsigned char* buffer_ = nullptr;
67 };
68 
69 } // namespace base
70 } // namespace nndeploy
virtual ~BaseLoader()=default
virtual bool read(char *buffer, int64_t size)=0
int offset(int64_t offset)
static bool write(const char *filePath, std::pair< const void *, size_t > cacheInfo)
std::string path() const
Definition: file_loader.h:38
bool read(char *buffer, int64_t size)
FileLoader(const char *file, bool init=false)
MemoryLoader(unsigned char *ptr)
Definition: file_loader.h:58
virtual bool read(char *dst, int64_t size) override
Definition: file_loader.h:59
#define NNDEPLOY_CC_API
api
Definition: macro.h:29