2016-03-04 12:12:37 +00:00
|
|
|
|
2017-09-20 08:16:26 +00:00
|
|
|
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
|
|
|
|
// under the Academic Free License.
|
2016-03-15 10:29:13 +00:00
|
|
|
// His code is available here: https://github.com/SuperV1234/cppcon2015
|
|
|
|
|
|
|
|
|
2016-03-04 12:12:37 +00:00
|
|
|
#ifndef EC_BITSET_HPP
|
|
|
|
#define EC_BITSET_HPP
|
|
|
|
|
|
|
|
#include <bitset>
|
|
|
|
#include "Meta/TypeList.hpp"
|
2016-03-04 12:39:25 +00:00
|
|
|
#include "Meta/Combine.hpp"
|
2016-03-05 14:33:24 +00:00
|
|
|
#include "Meta/IndexOf.hpp"
|
2016-03-13 09:07:49 +00:00
|
|
|
#include "Meta/ForEach.hpp"
|
2016-04-20 12:59:47 +00:00
|
|
|
#include "Meta/Contains.hpp"
|
2016-03-04 12:12:37 +00:00
|
|
|
|
|
|
|
namespace EC
|
|
|
|
{
|
2018-05-17 08:05:49 +00:00
|
|
|
// Note bitset size is sizes of components and tags + 1
|
|
|
|
// This is to use the last extra bit as the result of a query
|
|
|
|
// with a Component or Tag not known to the Bitset.
|
2018-05-19 07:09:31 +00:00
|
|
|
// Those queries should return a false bit every time as long as EC::Manager
|
|
|
|
// does not change that last bit.
|
2016-03-04 12:12:37 +00:00
|
|
|
template <typename ComponentsList, typename TagsList>
|
|
|
|
struct Bitset :
|
2018-05-17 08:05:49 +00:00
|
|
|
public std::bitset<ComponentsList::size + TagsList::size + 1>
|
2016-03-04 12:12:37 +00:00
|
|
|
{
|
2016-03-04 12:39:25 +00:00
|
|
|
using Combined = EC::Meta::Combine<ComponentsList, TagsList>;
|
|
|
|
|
2018-05-17 08:05:49 +00:00
|
|
|
Bitset()
|
|
|
|
{
|
|
|
|
(*this)[Combined::size] = false;
|
|
|
|
}
|
|
|
|
|
2016-03-14 09:16:09 +00:00
|
|
|
template <typename Component>
|
|
|
|
constexpr auto getComponentBit() const
|
|
|
|
{
|
2018-05-17 08:05:49 +00:00
|
|
|
auto index = EC::Meta::IndexOf<Component, Combined>::value;
|
|
|
|
return (*this)[index];
|
2016-03-14 09:16:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 12:12:37 +00:00
|
|
|
template <typename Component>
|
|
|
|
constexpr auto getComponentBit()
|
|
|
|
{
|
2018-05-17 08:05:49 +00:00
|
|
|
auto index = EC::Meta::IndexOf<Component, Combined>::value;
|
|
|
|
return (*this)[index];
|
2016-03-04 12:12:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 09:16:09 +00:00
|
|
|
template <typename Tag>
|
|
|
|
constexpr auto getTagBit() const
|
|
|
|
{
|
2018-05-17 08:05:49 +00:00
|
|
|
auto index = EC::Meta::IndexOf<Tag, Combined>::value;
|
|
|
|
return (*this)[index];
|
2016-03-14 09:16:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 12:12:37 +00:00
|
|
|
template <typename Tag>
|
|
|
|
constexpr auto getTagBit()
|
|
|
|
{
|
2018-05-17 08:05:49 +00:00
|
|
|
auto index = EC::Meta::IndexOf<Tag, Combined>::value;
|
|
|
|
return (*this)[index];
|
2016-03-04 12:12:37 +00:00
|
|
|
}
|
2016-03-05 14:33:24 +00:00
|
|
|
|
|
|
|
template <typename Contents>
|
|
|
|
static constexpr Bitset<ComponentsList, TagsList> generateBitset()
|
|
|
|
{
|
|
|
|
Bitset<ComponentsList, TagsList> bitset;
|
2016-03-13 09:07:49 +00:00
|
|
|
|
|
|
|
EC::Meta::forEach<Contents>([&bitset] (auto t) {
|
|
|
|
if(EC::Meta::Contains<decltype(t), Combined>::value)
|
2016-03-05 14:33:24 +00:00
|
|
|
{
|
2017-09-20 08:16:26 +00:00
|
|
|
bitset[EC::Meta::IndexOf<decltype(t), Combined>::value] =
|
|
|
|
true;
|
2016-03-05 14:33:24 +00:00
|
|
|
}
|
2016-03-13 09:07:49 +00:00
|
|
|
});
|
2016-03-05 14:33:24 +00:00
|
|
|
|
|
|
|
return bitset;
|
|
|
|
}
|
2019-07-11 12:11:03 +00:00
|
|
|
|
|
|
|
template <typename IntegralType>
|
|
|
|
auto getCombinedBit(const IntegralType& i) {
|
|
|
|
static_assert(std::is_integral<IntegralType>::value,
|
|
|
|
"Parameter must be an integral type");
|
|
|
|
if(i >= Combined::size || i < 0) {
|
|
|
|
return (*this)[Combined::size];
|
|
|
|
} else {
|
|
|
|
return (*this)[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IntegralType>
|
|
|
|
auto getCombinedBit(const IntegralType& i) const {
|
|
|
|
static_assert(std::is_integral<IntegralType>::value,
|
|
|
|
"Parameter must be an integral type");
|
|
|
|
if(i >= Combined::size || i < 0) {
|
|
|
|
return (*this)[Combined::size];
|
|
|
|
} else {
|
|
|
|
return (*this)[i];
|
|
|
|
}
|
|
|
|
}
|
2016-03-04 12:12:37 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|