1
0
Fork 0

Codechange: Add StrongBitSet helper to simplify making a bitset from a StrongType.

pull/14292/head
Peter Nelson 2025-03-16 13:12:09 +00:00
parent 98430f99d1
commit 329a2da122
No known key found for this signature in database
GPG Key ID: 8EF8F0A467DF75ED
2 changed files with 43 additions and 0 deletions

View File

@ -29,6 +29,7 @@ add_files(
string_consumer.hpp
string_inplace.cpp
string_inplace.hpp
strong_bitset_type.hpp
strong_typedef_type.hpp
utf8.cpp
utf8.hpp

View File

@ -0,0 +1,42 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file strong_bitset_type.hpp Type helper for making a BitSet out of a Strong Typedef. */
#ifndef STRONG_BITSET_TYPE_HPP
#define STRONG_BITSET_TYPE_HPP
#include "base_bitset_type.hpp"
/**
* Strong bit set.
* @tparam Tvalue_type Type of values to wrap.
* @tparam Tstorage Storage type required to hold values.
*/
template <typename Tvalue_type, typename Tstorage, Tstorage Tmask = std::numeric_limits<Tstorage>::max()>
class StrongBitSet : public BaseBitSet<StrongBitSet<Tvalue_type, Tstorage, Tmask>, Tvalue_type, Tstorage> {
public:
constexpr StrongBitSet() : BaseClass() {}
constexpr StrongBitSet(Tvalue_type value) : BaseClass() { this->Set(value); }
explicit constexpr StrongBitSet(Tstorage data) : BaseClass(data) {}
constexpr StrongBitSet(std::initializer_list<const Tvalue_type> values) : BaseClass()
{
for (const Tvalue_type &value : values) {
this->Set(value);
}
}
static constexpr size_t DecayValueType(Tvalue_type value) { return value.base(); }
constexpr auto operator <=>(const StrongBitSet &) const noexcept = default;
private:
using BaseClass = BaseBitSet<StrongBitSet<Tvalue_type, Tstorage, Tmask>, Tvalue_type, Tstorage, Tmask>;
};
#endif /* STRONG_BITSET_TYPE_HPP */