EntityComponentMetaSystem
An implementation of an EntityComponent System with template meta-programming.
Loading...
Searching...
No Matches
Bitset.hpp
1
2// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
3// under the Academic Free License.
4// His code is available here: https://github.com/SuperV1234/cppcon2015
5
6
7#ifndef EC_BITSET_HPP
8#define EC_BITSET_HPP
9
10#include <bitset>
11#include "Meta/TypeList.hpp"
12#include "Meta/Combine.hpp"
13#include "Meta/IndexOf.hpp"
14#include "Meta/ForEach.hpp"
15#include "Meta/Contains.hpp"
16
17namespace EC
18{
19 // Note bitset size is sizes of components and tags + 1
20 // This is to use the last extra bit as the result of a query
21 // with a Component or Tag not known to the Bitset.
22 // Those queries should return a false bit every time as long as EC::Manager
23 // does not change that last bit.
24 template <typename ComponentsList, typename TagsList>
25 struct Bitset :
26 public std::bitset<ComponentsList::size + TagsList::size + 1>
27 {
28 using Combined = EC::Meta::Combine<ComponentsList, TagsList>;
29
30 Bitset()
31 {
32 (*this)[Combined::size] = false;
33 }
34
35 template <typename Component>
36 constexpr auto getComponentBit() const
37 {
39 return (*this)[index];
40 }
41
42 template <typename Component>
43 constexpr auto getComponentBit()
44 {
46 return (*this)[index];
47 }
48
49 template <typename Tag>
50 constexpr auto getTagBit() const
51 {
53 return (*this)[index];
54 }
55
56 template <typename Tag>
57 constexpr auto getTagBit()
58 {
60 return (*this)[index];
61 }
62
63 template <typename Contents>
64 static constexpr Bitset<ComponentsList, TagsList> generateBitset()
65 {
67
68 EC::Meta::forEach<Contents>([&bitset] (auto t) {
69 if(EC::Meta::Contains<decltype(t), Combined>::value)
70 {
71 bitset[EC::Meta::IndexOf<decltype(t), Combined>::value] =
72 true;
73 }
74 });
75
76 return bitset;
77 }
78
79 template <typename IntegralType>
80 auto getCombinedBit(const IntegralType& i) {
81 static_assert(std::is_integral<IntegralType>::value,
82 "Parameter must be an integral type");
83 if(i >= Combined::size || i < 0) {
84 return (*this)[Combined::size];
85 } else {
86 return (*this)[i];
87 }
88 }
89
90 template <typename IntegralType>
91 auto getCombinedBit(const IntegralType& i) const {
92 static_assert(std::is_integral<IntegralType>::value,
93 "Parameter must be an integral type");
94 if(i >= Combined::size || i < 0) {
95 return (*this)[Combined::size];
96 } else {
97 return (*this)[i];
98 }
99 }
100 };
101}
102
103#endif
104
Definition Bitset.hpp:27
Definition IndexOf.hpp:24