cpp-mate  0.7
Helpful library for C++.
BinaryData.hpp
1 /*
2  * Copyright (C) 2019-2020 Alexander Kornilov (akornilov.82@gmail.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CPP_MATE_BINARY_DATA_HPP
18 #define CPP_MATE_BINARY_DATA_HPP
19 
20 #include <CppMate/Checkers.hpp>
21 
22 #include <memory>
23 #include <vector>
24 #include <stdexcept>
25 
26 #include <cstdint>
27 
28 namespace CppMate {
29 
34 {
35 public:
36  class Wrapper;
37  class Empty;
38  class Window;
39 
43  virtual ~BinaryData() = default;
44 
49  virtual const uint8_t* getData() const = 0;
50 
55  virtual uint64_t getSize() const = 0;
56 
61  bool hasData() const { return getData() && getSize(); }
62 
68  bool operator==(const BinaryData& other) const { return getData() == other.getData() && getSize() == other.getSize(); }
69 
75  bool operator!=(const BinaryData& other) const { return !(*this == other); }
76 };
77 
83 {
84 public:
85 
93  Wrapper(const uint8_t* data, uint64_t size, bool validate = true):
94  _data(data),
95  _size(size)
96  {
97  checkArg(!validate || hasData());
98  }
99 
104  Wrapper(const std::vector<uint8_t>& buffer):
105  _data(&buffer[0]),
106  _size(buffer.size())
107  {
108  }
109 
110  const uint8_t* getData() const override { return _data; }
111  uint64_t getSize() const override { return _size; }
112 
113 private:
114  const uint8_t* _data;
115  uint64_t _size;
116 };
117 
122 {
123 public:
124  const uint8_t* getData() const override { return nullptr; }
125  uint64_t getSize() const override { return 0; }
126 
131  static const BinaryData& instance();
132 };
133 
138 {
139 public:
140 
148  Window(const BinaryData* data, uint64_t offset, uint64_t size = 0):
149  _data(nullptr),
150  _size(0)
151  {
152  using namespace CppMate;
153  checkArg(data && data->hasData());
154  checkArg(offset < data->getSize());
155  _size = size > 0 ? size : data->getSize() - offset;
156  checkArg(offset + _size <= data->getSize());
157  _data = data->getData() + offset;
158  }
159 
167  Window(std::unique_ptr<BinaryData> data, uint64_t offset, uint64_t size = 0):
168  Window(data.get(), offset, size)
169  {
170  _sourceData = std::move(data);
171  }
172 
180  Window(std::shared_ptr<BinaryData> data, uint64_t offset, uint64_t size = 0):
181  Window(data.get(), offset, size)
182  {
183  _sourceData = std::move(data);
184  }
185 
186  const uint8_t* getData() const override { return _data; }
187  uint64_t getSize() const override { return _size; }
188 
189 private:
190  std::shared_ptr<BinaryData> _sourceData;
191  const uint8_t* _data;
192  uint64_t _size;
193 
194 };
195 
196 } // namespace CppMate
197 
198 #endif // CPP_MATE_BINARY_DATA_HPP
Represents implementation of BinaryData interface for empty data.
Definition: BinaryData.hpp:122
static const BinaryData & instance()
Returns singleton instance of the empty BinaryData.
const uint8_t * getData() const override
Returns address of data.
Definition: BinaryData.hpp:124
uint64_t getSize() const override
Returns size of data in bytes.
Definition: BinaryData.hpp:125
Represents window within provided BinaryData.
Definition: BinaryData.hpp:138
Window(const BinaryData *data, uint64_t offset, uint64_t size=0)
Constructor.
Definition: BinaryData.hpp:148
uint64_t getSize() const override
Returns size of data in bytes.
Definition: BinaryData.hpp:187
const uint8_t * getData() const override
Returns address of data.
Definition: BinaryData.hpp:186
Window(std::unique_ptr< BinaryData > data, uint64_t offset, uint64_t size=0)
Constructor.
Definition: BinaryData.hpp:167
Window(std::shared_ptr< BinaryData > data, uint64_t offset, uint64_t size=0)
Constructor.
Definition: BinaryData.hpp:180
Represents implementation of BinaryData interface for exisitng data.
Definition: BinaryData.hpp:83
uint64_t getSize() const override
Returns size of data in bytes.
Definition: BinaryData.hpp:111
Wrapper(const std::vector< uint8_t > &buffer)
Constructor.
Definition: BinaryData.hpp:104
Wrapper(const uint8_t *data, uint64_t size, bool validate=true)
Constructor.
Definition: BinaryData.hpp:93
const uint8_t * getData() const override
Returns address of data.
Definition: BinaryData.hpp:110
Represents interface of abstract binary data.
Definition: BinaryData.hpp:34
bool operator!=(const BinaryData &other) const
operator !=
Definition: BinaryData.hpp:75
virtual uint64_t getSize() const =0
Returns size of data in bytes.
bool hasData() const
Returns status of a data presence.
Definition: BinaryData.hpp:61
virtual const uint8_t * getData() const =0
Returns address of data.
bool operator==(const BinaryData &other) const
operator ==
Definition: BinaryData.hpp:68
virtual ~BinaryData()=default
Definition: BinaryData.hpp:28