VCTR
Loading...
Searching...
No Matches
VectorBoolWorkaround.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2022 by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23#pragma once
24
25namespace vctr::detail
26{
27
41template <class Allocator>
42class VectorBoolWorkaround : public std::vector<std::byte, Allocator>
43{
44private:
45 //==============================================================================
46 using StdVectorType = std::vector<std::byte, Allocator>;
47
48 static bool& toBool (std::byte& element) { return *reinterpret_cast<bool*> (&element); }
49 static const bool& toBool (const std::byte& element) { return *reinterpret_cast<const bool*> (&element); }
50 static bool* toBool (std::byte* ptr) { return reinterpret_cast<bool*> (ptr); }
51 static const bool* toBool (const std::byte* ptr) { return reinterpret_cast<const bool*> (ptr); }
52
53public:
54 constexpr VectorBoolWorkaround (size_t size)
55 : StdVectorType (size)
56 {}
57
58 constexpr VectorBoolWorkaround (size_t size, bool value)
59 : StdVectorType (size, std::byte (value))
60 {}
61
62 constexpr VectorBoolWorkaround (std::initializer_list<bool> il)
63 {
64 StdVectorType::reserve (il.size());
65 for (bool e : il)
66 push_back (e);
67 }
68
69 //==============================================================================
70 constexpr bool& operator[] (size_t i) { return toBool (StdVectorType::operator[] (i)); }
71 constexpr const bool& operator[] (size_t i) const { return toBool (StdVectorType::operator[] (i)); }
72 constexpr bool& at (size_t i) { return toBool (StdVectorType::at (i)); }
73 constexpr const bool& at (size_t i) const { return toBool (StdVectorType::at (i)); }
74 constexpr bool& front() { return toBool (StdVectorType::front()); }
75 constexpr const bool& front() const { return toBool (StdVectorType::front()); }
76 constexpr bool& back() { return toBool (StdVectorType::back()); }
77 constexpr const bool& back() const { return toBool (StdVectorType::back()); }
78 constexpr void push_back (bool v) { StdVectorType::push_back (std::byte (v)); }
79
80 bool* data() { return toBool (StdVectorType::data()); }
81 const bool* data() const { return toBool (StdVectorType::data()); }
82 bool* begin() { return data(); }
83 const bool* begin() const { return data(); }
84 bool* end() { return data() + StdVectorType::size(); }
85 const bool* end() const { return data() + StdVectorType::size(); }
86};
87}