aboutsummaryrefslogtreecommitdiff
path: root/src/common/io/ProxyStream.cpp
blob: c2e64056af09815c98c33b9443a9682bae28a230 (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
#include "cru/common/io/ProxyStream.h"
#include "cru/common/io/Stream.h"

namespace cru::io {
ProxyStream::ProxyStream(ProxyStreamHandlers handlers)
    : Stream(static_cast<bool>(handlers.seek), static_cast<bool>(handlers.read),
             static_cast<bool>(handlers.write)),
      handlers_(std::move(handlers)) {}

ProxyStream::~ProxyStream() { DoClose(); }

Index ProxyStream::DoSeek(Index offset, SeekOrigin origin) {
  return handlers_.seek(offset, origin);
}

Index ProxyStream::DoRead(std::byte* buffer, Index offset, Index size) {
  return handlers_.read(buffer, offset, size);
}

Index ProxyStream::DoWrite(const std::byte* buffer, Index offset, Index size) {
  return handlers_.write(buffer, offset, size);
}

void ProxyStream::DoFlush() {
  if (handlers_.flush) {
    handlers_.flush();
  }
}

void ProxyStream::DoClose() {
  CRU_STREAM_BEGIN_CLOSE
  if (handlers_.close) {
    handlers_.close();
  }
  handlers_ = {};
}
}  // namespace cru::io