aboutsummaryrefslogtreecommitdiff
path: root/include/cru/base/platform/win/Stream.h
blob: e49bf48bb90ba48ee5faef6ea198bc996d443c6a (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once

#ifndef _WIN32
#error "This file can only be included on Windows."
#endif

#include "../../io/Base.h"
#include "../../io/Stream.h"
#include "Base.h"

#include <objidlbase.h>

namespace cru::platform::win {
class CRU_BASE_API Win32HandleStream : public io::Stream {
 public:
  Win32HandleStream(std::string_view path, io::OpenFileFlag flags);
  Win32HandleStream(HANDLE handle, bool auto_close, bool can_seek,
                    bool can_read, bool can_write);
  Win32HandleStream(Win32Handle&& handle, bool can_seek, bool can_read,
                    bool can_write);
  ~Win32HandleStream() override;

 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;

 public:
  const Win32Handle& GetHandle() { return handle_; }

  CRU_STREAM_IMPLEMENT_CLOSE_BY_DO_CLOSE

 private:
  void DoClose();

 private:
  Win32Handle handle_;
};

CRU_BASE_API IStream* ToComStream(io::Stream* stream);

class CRU_BASE_API ComStream : public io::Stream {
 public:
  ComStream(std::string_view path, io::OpenFileFlag flags);
  ComStream(IStream* com_stream, bool auto_release, bool can_seek,
            bool can_read, bool can_write);
  ~ComStream() override;

 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;

 public:
  IStream* GetComStream() { return stream_; }

  CRU_STREAM_IMPLEMENT_CLOSE_BY_DO_CLOSE

 private:
  void DoClose();

 private:
  IStream* stream_;
  bool auto_release_;
};
}  // namespace cru::platform::win