mirror of
https://github.com/JakeHillion/object-introspection.git
synced 2024-11-12 21:56:54 +00:00
Turn FeatureSet into a generic EnumBitset
This commit is contained in:
parent
cfc3cc0221
commit
1723611411
42
oi/EnumBitset.h
Normal file
42
oi/EnumBitset.h
Normal 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;
|
||||
};
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user