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 (std::initializer_list<bool> il)
55 {
56 StdVectorType::reserve (il.size());
57 for (bool e : il)
58 push_back (e);
59 }
60
61 //==============================================================================
62 constexpr bool& operator[] (size_t i) { return toBool (StdVectorType::operator[] (i)); }
63 constexpr const bool& operator[] (size_t i) const { return toBool (StdVectorType::operator[] (i)); }
64 constexpr bool& at (size_t i) { return toBool (StdVectorType::at (i)); }
65 constexpr const bool& at (size_t i) const { return toBool (StdVectorType::at (i)); }
66 constexpr bool& front() { return toBool (StdVectorType::front()); }
67 constexpr const bool& front() const { return toBool (StdVectorType::front()); }
68 constexpr bool& back() { return toBool (StdVectorType::back()); }
69 constexpr const bool& back() const { return toBool (StdVectorType::back()); }
70 constexpr void push_back (bool v) { StdVectorType::push_back (std::byte (v)); }
71
72 bool* data() { return toBool (StdVectorType::data()); }
73 const bool* data() const { return toBool (StdVectorType::data()); }
74 bool* begin() { return data(); }
75 const bool* begin() const { return data(); }
76 bool* end() { return data() + StdVectorType::size(); }
77 const bool* end() const { return data() + StdVectorType::size(); }
78};
79}