uLib-0.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Flags.h
Go to the documentation of this file.
1 /*//////////////////////////////////////////////////////////////////////////////
2 // CMT Cosmic Muon Tomography project //////////////////////////////////////////
4 
5  Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
6  All rights reserved
7 
8  Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
9 
10  ------------------------------------------------------------------
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 3.0 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library.
23 
25 
26 
27 
28 #ifndef U_CORE_FLAGS_H
29 #define U_CORE_FLAGS_H
30 
31 namespace uLib {
32 
33 template<typename T> class Flags
34 {
35  T i;
36 public:
37  typedef T EnumT;
38 
39  inline Flags(const Flags &f) : i(f.i) {}
40  inline Flags() : i((T)0) {}
41  inline Flags(T f) : i(f) {}
42 
43  inline Flags &operator=(const Flags &f) { i = f.i; return *this; }
44  inline Flags &operator&=(T mask) { i &= mask; return *this; }
45 
46  inline Flags &operator|=(const Flags f) { i |= f.i; return *this; }
47  inline Flags &operator|=(const T f) { i |= f; return *this; }
48  inline Flags &operator^=(const Flags f) { i ^= f.i; return *this; }
49  inline Flags &operator^=(const T f) { i ^= f; return *this; }
50 
51  inline Flags operator|(const Flags f) const { return Flags(i | f.i); }
52  inline Flags operator|(const T f) const { return Flags(i | f); }
53  inline Flags operator^(const Flags f) const { return Flags(i ^ f.i); }
54  inline Flags operator^(const T f) const { return Flags(i ^ f); }
55  inline Flags operator&(const Flags f) const { return Flags(i & f.i); }
56  inline Flags operator&(const T f) const { return Flags(i & f); }
57  inline Flags operator~() const { return Flags(~i); }
58 
59  inline bool operator !() const { return !i; }
60  inline bool testFlag(const T f) const
61  { return (i & f) == f && (f != 0 || i == f ); }
62  // inline bool operator==(const T f) const { return i==f; }
63 
64  inline T operator()() { return i; }
65  inline operator T() const { return i; }
66 };
67 
68 
69 typedef Flags<unsigned short> Flags2B;
70 typedef Flags<unsigned int> Flags4B;
71 typedef Flags<unsigned long> Flags8B;
72 
73 } // uLib
74 
75 
76 #endif // FLAGS_H