IOLink  IOL_v1.1.0_release
StreamAccess.h
1 #pragma once
2 
3 #include <cstdint>
4 
5 #include <iolink/FlagSet.h>
6 #include <iolink/IOLinkAPI.h>
7 #include <iolink/storage/DataAccess.h>
8 
9 namespace iolink
10 {
11 
16 {
17  READ = 0x1,
18  WRITE = 0x2,
19  SEEK = 0x4,
20  MEMORY_ACCESS = 0x8,
21 
22  READ_WRITE = 0x3,
23  READ_SEEK = 0x5,
24  WRITE_SEEK = 0x6,
25 };
26 
27 extern template class IOLINK_API_IMPORT FlagSet<StreamAccessCapability>;
28 
31 
32 // Define bitwise operators for RandomAccessCapability
33 IOLINK_DEFINE_ENUM_BITWISE_OPERATORS(StreamAccessCapability)
34 
35 
38 enum class SeekOrigin
39 {
41  BEGIN = 1,
43  CURRENT = 2,
45  END = 3,
46 };
47 
59 class IOLINK_API StreamAccess : public DataAccess
60 {
61 public:
62  virtual ~StreamAccess() = default;
63 
67  virtual StreamAccessCapabilitySet capabilities() const = 0;
68 
72  inline bool support(StreamAccessCapabilitySet flags) { return this->capabilities().has(flags); }
73 
80  virtual bool good() const;
81 
88  virtual bool eof() const = 0;
89 
90  /*
91  * Checks if an error occured on the stream which forbids its use
92  * Remark: 'end of file' is not considered as an error
93  *
94  * Default implementation returns false.
95  *
96  * @return true if an error occured, false otherwise
97  */
98  virtual bool fail() const = 0;
99 
100  // ==== //
101  // READ //
102  // ==== //
103 
115  virtual size_t read(size_t size, void* dst);
116 
124  virtual uint8_t peek();
125 
126  // ===== //
127  // WRITE //
128  // ===== //
129 
143  virtual size_t write(size_t size, const void* src);
144 
150  virtual void flush();
151 
152  // ==== //
153  // SEEK //
154  // ==== //
155 
161  virtual size_t tell();
162 
180  virtual void seek(int64_t offset, SeekOrigin origin);
181 
182  // ============= //
183  // MEMORY_ACCESS //
184  // ============= //
185 
193  virtual void* buffer();
194 
202  virtual const void* bufferReadOnly() const;
203 
211  virtual size_t bufferSize();
212 };
213 
214 } // namespace iolink