TPIE

2362a60
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 
71 template <typename T>
72 class flags {
73 private:
74  typedef typename bits::enum_storage_type<T>::type st;
75  typedef void (flags::*bool_type)() const;
76  void bool_type_true() const {}
77 public:
78  flags(): m_value(0) {}
79  flags(T t): m_value(static_cast<st>(t)) {}
80  friend flags operator|(const flags & l, const T & r) {return l | flags(r);}
81  friend flags operator|(const flags & l, const flags & r) {return flags(l.m_value | r.m_value);}
82  friend flags operator|(const T & l, const flags & r) {return flags(l) | r;}
83  friend flags operator&(const flags & l, const flags & r) {return flags(l.m_value & r.m_value);}
84  friend flags operator&(const T & l, const flags & r) {return flags(flags(l).m_value & r.m_value);}
85  friend flags operator&(const flags & l, const T & r) {return flags(l.m_value & flags(r).m_value);}
86  flags & operator|=(const T & r) { return *this |= flags(r); }
87  flags & operator|=(const flags & r) { m_value |= r.m_value; return *this; }
88  // See http://www.artima.com/cppsource/safebool2.html
89  operator bool_type() const {return m_value?&flags::bool_type_true:NULL;}
90  flags operator~() const {return flags(~m_value);}
91 
92  template <typename D>
93  friend void serialize(D & dst, const flags<T> & f) {
94  serialize(dst, f.m_value);
95  }
96 
97  template <typename S>
98  friend void unserialize(S & src, flags<T> & f) {
99  unserialize(src, f.m_value);
100  }
101 private:
102  explicit flags(st value): m_value(value) {}
103  st m_value;
104 };
105 
106 #define TPIE_DECLARE_OPERATORS_FOR_FLAGS(T) \
107 inline tpie::flags<T> operator|(const T & l, const T & r) {return tpie::flags<T>(l) | r;} \
108 inline tpie::flags<T> operator~(const T & l) {return ~tpie::flags<T>(l);}
109 
110 
111 } //namespace tpie
112 
113 #endif //#_TPIE_FLAGS_H
Binary serialization and unserialization.
void unserialize(S &src, foo &v)
Sample tpie::unserialize prototype.
void serialize(D &dst, const foo &v)
Sample tpie::serialize prototype.