summaryrefslogtreecommitdiff
path: root/src/flag_set.hpp
blob: 2e5d46ea88bdc2ececad351a5c6c97ee3d918ca8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#pragma once

#include <array>
#include <cassert>
#include <cstdint>

#include "tome/pp/global_constexpr.hpp"

/**
 * Set of binary flags.
 */
template<std::size_t Tiers> struct flag_set
{
private:
	static constexpr std::size_t tiers = Tiers;
	std::uint32_t m_data[tiers];

public:

	constexpr flag_set()
		: m_data { 0 }
	{
		// It is *extremely* important that there are absolutely
		// NO dependencies on any global objects in here; lest we
		// fall into SIOF territory; see DECLARE_FLAG_ZERO_IMPL
	}

	// This method is a workaround for a a segmentation fault
	// when compiling with GCC 5.3.0 (Arch Linux). We should be
	// able to use make() directly in DECLARE_FLAG_MAKE_INIT,
	// but GCC segfaults.
	template<std::uint32_t tier, std::size_t index> static constexpr flag_set make_static()
	{
		static_assert(tier < tiers, "tier >= tiers");
		static_assert(index < 32, "index >= 32");
		flag_set f;
		f.m_data[tier] = (1UL << index);
		return f;
	}

	static constexpr flag_set make(std::uint32_t tier, std::size_t index)
	{
		assert(tier < tiers);
		assert(index < 32);
		flag_set f;
		f.m_data[tier] = (1UL << index);
		return f;
	}

	constexpr std::size_t size() const
	{
		return tiers;
	}

	constexpr bool empty() const
	{
		for (std::size_t i = 0; i < tiers; i++)
		{
			if (m_data[i])
			{
				return false;
			}
		}
		return true;
	}

	uint32_t &operator[](std::size_t i)
	{
		assert(i < tiers);
		return m_data[i];
	}

	constexpr uint32_t const &operator [](std::size_t i) const
	{
		assert(i < tiers);
		return m_data[i];
	}

	constexpr operator bool() const
	{
		return !empty();
	}

	flag_set &operator |= (flag_set const &other)
	{
		for (std::size_t i = 0; i < tiers; i++)
		{
			m_data[i] |= other.m_data[i];
		}
		return *this;
	}

	constexpr flag_set operator | (flag_set const &other) const
	{
		flag_set f;
		for (std::size_t i = 0; i < tiers; i++)
		{
			f.m_data[i] = m_data[i] | other.m_data[i];
		}
		return f;
	}

	flag_set &operator &= (flag_set const &other)
	{
		for (std::size_t i = 0; i < tiers; i++)
		{
			m_data[i] &= other.m_data[i];
		}
		return *this;
	}

	constexpr flag_set operator & (flag_set const &other) const
	{
		flag_set f;
		for (std::size_t i = 0; i < tiers; i++)
		{
			f.m_data[i] = m_data[i] & other.m_data[i];
		}
		return f;
	}

	constexpr flag_set operator ~ () const
	{
		flag_set f;
		for (std::size_t i = 0; i < tiers; i++)
		{
			f.m_data[i] = ~m_data[i];
		}
		return f;
	}

};

// Implementation details, because preprocessor.
#define DECLARE_FLAG_MAKE_INIT(type, tier, index) \
   type::make_static<tier-1,index>()

/**
 * Macro for declaring a "flag" constant.
 */
#define DECLARE_FLAG(type, name, tier, index) \
  PP_GLOBAL_CONSTEXPR_CONST(type, name, DECLARE_FLAG_MAKE_INIT(type, tier, index))

/**
 * Macro for declaring a zero'ed "flag" variable.
 */
#define DECLARE_FLAG_ZERO_INTF(type, name) \
  extern type name

/**
 * Macro for declaring the implementation of a zero'ed "flag" variable.
 */
#define DECLARE_FLAG_ZERO_IMPL(type, name) \
  type name { };