TPIE

11a2c2d
flags.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 20015 The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
23 
24 #ifndef _TPIE_FLAGS_H
25 #define _TPIE_FLAGS_H
26 
27 #include <cstdint>
28 #include <tpie/serialization2.h>
29 
30 namespace tpie {
31 namespace bits {
32 
33 template <int S>
35 
36 template <>
37 struct enum_storage_type_help<1> {typedef uint8_t type;};
38 
39 template <>
40 struct enum_storage_type_help<2> {typedef uint16_t type;};
41 
42 template <>
43 struct enum_storage_type_help<4> {typedef uint32_t type;};
44 
45 template <>
46 struct enum_storage_type_help<8> {typedef uint64_t type;};
47 
48 template <typename T>
50  typedef typename enum_storage_type_help<sizeof(T)>::type type;
51 };
52 } //namespace bits
53 
72 template <typename T>
73 class flags {
74 private:
75  typedef typename bits::enum_storage_type<T>::type st;
76  typedef void (flags::*bool_type)() const;
77  void bool_type_true() const {}
78 public:
79  flags(): m_value(0) {}
80  flags(T t): m_value(static_cast<st>(t)) {}
81  friend flags operator|(const flags & l, const T & r) {return l | flags(r);}
82  friend flags operator|(const flags & l, const flags & r) {return flags(l.m_value | r.m_value);}
83  friend flags operator|(const T & l, const flags & r) {return flags(l) | r;}
84  friend flags operator&(const flags & l, const flags & r) {return flags(l.m_value & r.m_value);}
85  friend flags operator&(const T & l, const flags & r) {return flags(flags(l).m_value & r.m_value);}
86  friend flags operator&(const flags & l, const T & r) {return flags(l.m_value & flags(r).m_value);}
87  flags & operator|=(const T & r) { return *this |= flags(r); }
88  flags & operator|=(const flags & r) { m_value |= r.m_value; return *this; }
89  // See http://www.artima.com/cppsource/safebool2.html
90  operator bool_type() const {return m_value?&flags::bool_type_true:NULL;}
91  flags operator~() const {return flags(~m_value);}
92 
93  template <typename D>
94  friend void serialize(D & dst, const flags<T> & f) {
95  serialize(dst, f.m_value);
96  }
97 
98  template <typename S>
99  friend void unserialize(S & src, flags<T> & f) {
100  unserialize(src, f.m_value);
101  }
102 private:
103  explicit flags(st value): m_value(value) {}
104  st m_value;
105 };
106 
107 #define TPIE_DECLARE_OPERATORS_FOR_FLAGS(T) \
108 inline tpie::flags<T> operator|(const T & l, const T & r) {return tpie::flags<T>(l) | r;} \
109 inline tpie::flags<T> operator~(const T & l) {return ~tpie::flags<T>(l);}
110 
111 
112 } //namespace tpie
113 
114 #endif //#_TPIE_FLAGS_H
tpie::bits::enum_storage_type_help
Definition: flags.h:34
serialization2.h
tpie::flags
Type safe bitflags over an enumeration T.
Definition: flags.h:73
tpie::bits::enum_storage_type
Definition: flags.h:49
tpie
Definition: access_type.h:26