OGLplus (0.52.0) a C++ wrapper for OpenGL

bitfield.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_BITFIELD_1107121519_HPP
14 #define OGLPLUS_BITFIELD_1107121519_HPP
15 
16 #include <oglplus/config/compiler.hpp>
17 
18 #include <type_traits>
19 
20 #if !OGLPLUS_NO_INITIALIZER_LISTS
21 #include <initializer_list>
22 #endif
23 
24 namespace oglplus {
25 
26 namespace enums {
27 template <typename Enum>
28 struct EnumBaseType;
29 
30 template <typename Enum>
31 struct IsBitfieldBit
32 {
33  typedef std::false_type Type;
34 };
35 
36 } // namespace enums
37 
39 
55 template <typename Bit>
56 class Bitfield
57 {
58 private:
59  typedef typename enums::EnumBaseType<Bit>::Type BF;
60  BF _bits;
61 public:
63  Bitfield(void)
64  : _bits(0)
65  { }
66 
68  Bitfield(BF bits)
69  : _bits(bits)
70  { }
71 
73  Bitfield(Bit _bit)
74  : _bits(BF(_bit))
75  { }
76 
77  Bitfield(Bit _bit_a, Bit _bit_b)
78  : _bits(BF(_bit_a) | BF(_bit_b))
79  { }
80 
81 #if OGLPLUS_DOCUMENTATION_ONLY
82  template <typename Iter>
84  Bitfield(Iter pos, Iter end);
85 #else
86  template <typename Iter>
87  Bitfield(Iter pos, Iter end)
88  : _bits(BF(0))
89  {
90  while(pos != end)
91  {
92  _bits |= BF(*pos);
93  ++pos;
94  }
95  }
96 #endif
97 
98 #if OGLPLUS_DOCUMENTATION_ONLY || !OGLPLUS_NO_INITIALIZER_LISTS
99  Bitfield(const std::initializer_list<Bit>& bits)
101  : _bits(BF(0))
102  {
103  for(auto i=bits.begin(),e=bits.end(); i!=e; ++i)
104  _bits |= BF(*i);
105  }
106 #endif
107 
109  friend Bitfield operator | (Bitfield bf, Bit b)
110  {
111  bf._bits |= BF(b);
112  return bf;
113  }
114 
117  {
118  this->_bits |= BF(b);
119  return *this;
120  }
121 
123  bool Test(Bit b) const
124  {
125  return (this->_bits & BF(b)) == BF(b);
126  }
127 
128  OGLPLUS_EXPLICIT operator BF (void) const
129  {
130  return _bits;
131  }
132 };
133 
134 // helper macro used to define bitfield-related functions
135 #define OGLPLUS_MAKE_BITFIELD(BITS) \
136 namespace enums { \
137 template <> struct EnumBaseType<BITS> { typedef GLbitfield Type; }; \
138 template <> struct IsBitfieldBit<BITS> { typedef std::true_type Type; }; \
139 } \
140 inline oglplus::Bitfield<BITS> operator | (BITS b1, BITS b2) \
141 { \
142  return Bitfield<BITS>(b1, b2); \
143 }
144 
145 
146 } // namespace oglplus
147 
148 #endif // include guard
Bitfield(Bit _bit)
Construct a bitfield from a single strongly-typed enumeration value.
Definition: bitfield.hpp:73
This template serves as a wrapper for OpenGL bitfields.
Definition: bitfield.hpp:56
Bitfield & operator|=(Bit b)
Bitwise or operator for combining enumeration values into a bitfield.
Definition: bitfield.hpp:116
friend Bitfield operator|(Bitfield bf, Bit b)
Bitwise or operator for combining enumeration values into a bitfield.
Definition: bitfield.hpp:109
Bitfield(void)
Constructs an empty bitfield.
Definition: bitfield.hpp:63
bool Test(Bit b) const
Test if a specified bit is set.
Definition: bitfield.hpp:123
Bitfield(BF bits)
Construct a bitfield from the underlying type.
Definition: bitfield.hpp:68

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).