Furrent
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bitfield.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <ostream>
5 #include <vector>
6 
7 #include "tfriend_fw.hpp"
8 
9 namespace fur::download::bitfield {
24 class Bitfield {
25  public:
27  explicit Bitfield(int64_t len);
28 
32  Bitfield(std::vector<uint8_t> storage, int64_t len)
33  : len{len}, storage{std::move(storage)} {}
34 
36  void set(int64_t index, bool value);
37 
39  void set(int64_t index) { set(index, true); }
41  void unset(int64_t index) { set(index, false); }
42 
44  [[nodiscard]] bool get(int64_t index) const;
45 
47  [[nodiscard]] std::vector<uint8_t> get_bytes() const {
48  // Basically just return a copy of `storage`.
49  return storage;
50  }
51 
54  friend std::ostream& operator<<(std::ostream& os, const Bitfield& bitfield);
55 
61  const int64_t len;
62 
63  private:
65  std::vector<uint8_t> storage;
66 
67  // Befriend this class so the unit tests are able to access private members.
68  friend TestingFriend;
69 };
70 } // namespace fur::download::bitfield
fur::download::bitfield::Bitfield::get
bool get(int64_t index) const
Get the value of the bit at the provided index.
Definition: bitfield.cpp:45
fur::download::bitfield::Bitfield::operator<<
friend std::ostream & operator<<(std::ostream &os, const Bitfield &bitfield)
Definition: bitfield.cpp:52
fur::download::bitfield::Bitfield::Bitfield
Bitfield(std::vector< uint8_t > storage, int64_t len)
Definition: bitfield.hpp:32
fur::download::bitfield::Bitfield
Definition: bitfield.hpp:24
fur::download::bitfield::Bitfield::get_bytes
std::vector< uint8_t > get_bytes() const
Get the bitfield as an array of bytes.
Definition: bitfield.hpp:47
fur::download::bitfield::Bitfield::set
void set(int64_t index, bool value)
Set the bit to the provided index to either 1 or 0, depending on value.
Definition: bitfield.cpp:32
fur::download::bitfield::Bitfield::len
const int64_t len
Definition: bitfield.hpp:61
fur::download::bitfield::Bitfield::set
void set(int64_t index)
Set the bit to the provided index to 1.
Definition: bitfield.hpp:39
fur::download::bitfield::Bitfield::Bitfield
Bitfield(int64_t len)
Create a new Bitfield with the provided length (in bits).
Definition: bitfield.cpp:25
fur::download::bitfield::Bitfield::unset
void unset(int64_t index)
Set the bit to the provided index to 0.
Definition: bitfield.hpp:41
fur::download::bitfield
Definition: bitfield.cpp:24