SectorBuffer.h
Go to the documentation of this file.
1 /****
2  * SectorBuffer.h
3  *
4  * Copyright 2022 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the DiskStorage Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include <cstdint>
23 #include <memory>
24 
25 namespace Storage
26 {
27 namespace Disk
28 {
32 class SectorBuffer : public std::unique_ptr<uint8_t[]>
33 {
34 public:
36  {
37  }
38 
39  SectorBuffer(size_t sectorSize, size_t sectorCount) : mSectorCount(sectorCount), mSize(sectorSize * sectorCount)
40  {
41  reset(new uint8_t[mSize]);
42  }
43 
44  template <typename T> T& as()
45  {
46  return *reinterpret_cast<T*>(get());
47  }
48 
49  template <typename T> const T& as() const
50  {
51  return *reinterpret_cast<const T*>(get());
52  }
53 
54  size_t size() const
55  {
56  return mSize;
57  }
58 
59  uint32_t sectors() const
60  {
61  return mSectorCount;
62  }
63 
64  void clear()
65  {
66  fill(0);
67  }
68 
69  void fill(uint8_t value)
70  {
71  std::fill_n(get(), mSize, value);
72  }
73 
74  bool operator==(const SectorBuffer& other) const
75  {
76  return *this && other && mSize == other.mSize && memcmp(get(), other.get(), mSize) == 0;
77  }
78 
79 private:
80  size_t mSectorCount{0};
81  size_t mSize{0};
82 };
83 
84 } // namespace Disk
85 } // namespace Storage
Buffer for working with disk sectors.
Definition: SectorBuffer.h:33
uint32_t sectors() const
Definition: SectorBuffer.h:59
T & as()
Definition: SectorBuffer.h:44
bool operator==(const SectorBuffer &other) const
Definition: SectorBuffer.h:74
SectorBuffer(size_t sectorSize, size_t sectorCount)
Definition: SectorBuffer.h:39
size_t size() const
Definition: SectorBuffer.h:54
SectorBuffer()
Definition: SectorBuffer.h:35
void clear()
Definition: SectorBuffer.h:64
const T & as() const
Definition: SectorBuffer.h:49
void fill(uint8_t value)
Definition: SectorBuffer.h:69
Definition: FileDevice.h:26