Turn FeatureSet into a generic EnumBitset

This commit is contained in:
Alastair Robertson 2023-05-31 04:18:00 -07:00 committed by Alastair Robertson
parent cfc3cc0221
commit 1723611411
3 changed files with 46 additions and 25 deletions

42
oi/EnumBitset.h Normal file
View File

@ -0,0 +1,42 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <bitset>
template <typename T, size_t N>
class EnumBitset {
private:
using BitsetType = std::bitset<N>;
public:
EnumBitset() = default;
EnumBitset(std::initializer_list<T> values) {
for (auto v : values) {
(*this)[v] = true;
}
}
constexpr bool operator[](T v) const {
return bitset[static_cast<size_t>(v)];
}
typename BitsetType::reference operator[](T v) {
return bitset[static_cast<size_t>(v)];
}
private:
BitsetType bitset;
};

View File

@ -45,10 +45,4 @@ const char* featureToStr(Feature f) {
}
}
FeatureSet::FeatureSet(std::initializer_list<Feature> features) {
for (auto f : features) {
(*this)[f] = true;
}
}
} // namespace ObjectIntrospection

View File

@ -16,9 +16,10 @@
#pragma once
#include <array>
#include <bitset>
#include <string_view>
#include "oi/EnumBitset.h"
#define OI_FEATURE_LIST \
X(ChaseRawPointers, "chase-raw-pointers") \
X(PackStructs, "pack-structs") \
@ -45,23 +46,7 @@ constexpr std::array allFeatures = {
#undef X
};
class FeatureSet {
private:
using BitsetType = std::bitset<allFeatures.size() + 1>;
public:
FeatureSet() = default;
FeatureSet(std::initializer_list<Feature>);
constexpr bool operator[](Feature f) const {
return bitset[(size_t)f];
}
BitsetType::reference operator[](Feature f) {
return bitset[(size_t)f];
}
private:
BitsetType bitset;
};
// Use "size+1" to account for UnknownFeature"
using FeatureSet = EnumBitset<Feature, allFeatures.size() + 1>;
} // namespace ObjectIntrospection