aboutsummaryrefslogtreecommitdiff
path: root/include/cru/base/io/ProxyStream.h
blob: 42ec9dfd48732d8ae47df0787e81db37e674c139 (plain)
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
#pragma once

#include "Stream.h"

#include <functional>

namespace cru::io {
struct ProxyStreamHandlers {
  std::function<Index(Index offset, Stream::SeekOrigin origin)> seek;
  std::function<Index(std::byte* buffer, Index offset, Index size)> read;
  std::function<Index(const std::byte* buffer, Index offset, Index size)> write;
  std::function<void()> flush;

  /**
   * @brief This method will be only called once when `Close` is called or the
   * stream is destructed.
   */
  std::function<void()> close;
};

class ProxyStream : public Stream {
 public:
  explicit ProxyStream(ProxyStreamHandlers handlers);

  ~ProxyStream() override;

 public:
  CRU_STREAM_IMPLEMENT_CLOSE_BY_DO_CLOSE

 protected:
  Index DoSeek(Index offset, SeekOrigin origin) override;
  Index DoRead(std::byte* buffer, Index offset, Index size) override;
  Index DoWrite(const std::byte* buffer, Index offset, Index size) override;
  void DoFlush() override;

 private:
  void DoClose();

 private:
  ProxyStreamHandlers handlers_;
};
}  // namespace cru::io