CUBRID Engine  latest
base_flag.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * base_flag.hpp - template for flagged fields
21  */
22 
23 #ifndef _BASE_FLAG_HPP_
24 #define _BASE_FLAG_HPP_
25 
26 template <typename T>
27 class flag
28 {
29  public:
30  inline flag ();
31  inline flag (const flag<T> &other);
32  inline flag (const T &init_flags);
33 
34  inline flag &operator= (const flag<T> &other);
35  inline flag &operator= (const T &flags);
36 
37  inline flag &set (const T &flags);
38  inline flag &clear (const T &flags);
39  inline bool is_set (const T &flags);
40  inline bool is_all_set (const T &flags);
41  inline bool is_any_set (const T &flags);
42  inline T get_flags (void);
43 
44  static inline void set_flag (T &where_to_set, const T &what_to_set);
45  static inline void clear_flag (T &where_to_clear, const T &what_to_clear);
46  static inline bool is_flag_set (const T &where_to_check, const T &what_to_check);
47  static inline bool is_flag_all_set (const T &where_to_check, const T &what_to_check);
48  static inline bool is_flag_any_set (const T &where_to_check, const T &what_to_check);
49 
50  private:
52 };
53 
54 template<typename T>
55 inline
57 {
58  m_flags = 0;
59 }
60 
61 template<typename T>
62 inline flag<T>::flag (const flag &other)
63 {
64  m_flags = other.m_flags;
65 }
66 
67 template<typename T>
68 inline
69 flag<T>::flag (const T &init_flags)
70 {
71  m_flags = init_flags;
72 }
73 
74 template<typename T>
75 inline flag<T> &
77 {
78  this->m_flags = other.m_flags;
79  return *this;
80 }
81 
82 template<typename T>
83 inline flag<T> &
84 flag<T>::operator= (const T &flags)
85 {
86  this->m_flags = flags;
87  return *this;
88 }
89 
90 template<typename T>
91 inline flag<T> &
92 flag<T>::set (const T &flags)
93 {
94  m_flags = m_flags | flags;
95  return *this;
96 }
97 
98 template<typename T>
99 inline flag<T> &
100 flag<T>::clear (const T &flags)
101 {
102  m_flags = m_flags & (~flags);
103  return *this;
104 }
105 
106 template<typename T>
107 inline bool
108 flag<T>::is_set (const T &flags)
109 {
110  /* sugar syntax */
111  return is_any_set (flags);
112 }
113 
114 template<typename T>
115 inline bool flag<T>::is_all_set (const T &flags)
116 {
117  return (m_flags & flags) == flags;
118 }
119 
120 template<typename T>
121 inline bool flag<T>::is_any_set (const T &flags)
122 {
123  return (m_flags & flags) != 0;
124 }
125 
126 template<typename T>
127 inline T
129 {
130  return m_flags;
131 }
132 
133 template<typename T>
134 inline void
135 flag<T>::set_flag (T &where_to_set, const T &what_to_set)
136 {
137  where_to_set = where_to_set | what_to_set;
138 }
139 
140 template<typename T>
141 inline void
142 flag<T>::clear_flag (T &where_to_clear, const T &what_to_clear)
143 {
144  where_to_clear = where_to_clear & (~what_to_clear);
145 }
146 
147 template<typename T>
148 inline bool
149 flag<T>::is_flag_set (const T &where_to_check, const T &what_to_check)
150 {
151  /* syntax sugar */
152  return flag<T>::is_flag_any_set (where_to_check, what_to_check);
153 }
154 
155 template<typename T>
156 inline bool flag<T>::is_flag_all_set (const T &where_to_check, const T &what_to_check)
157 {
158  return (where_to_check & what_to_check) == what_to_check;
159 }
160 
161 template<typename T>
162 inline bool flag<T>::is_flag_any_set (const T &where_to_check, const T &what_to_check)
163 {
164  return (where_to_check & what_to_check) != 0;
165 }
166 
167 #endif /* _BASE_FLAG_HPP_ */
168 
static void clear_flag(T &where_to_clear, const T &what_to_clear)
Definition: base_flag.hpp:142
bool is_all_set(const T &flags)
Definition: base_flag.hpp:115
static bool is_flag_any_set(const T &where_to_check, const T &what_to_check)
Definition: base_flag.hpp:162
bool is_any_set(const T &flags)
Definition: base_flag.hpp:121
static void set_flag(T &where_to_set, const T &what_to_set)
Definition: base_flag.hpp:135
static bool is_flag_all_set(const T &where_to_check, const T &what_to_check)
Definition: base_flag.hpp:156
flag & clear(const T &flags)
Definition: base_flag.hpp:100
T m_flags
Definition: base_flag.hpp:51
flag & operator=(const flag< T > &other)
Definition: base_flag.hpp:76
static bool is_flag_set(const T &where_to_check, const T &what_to_check)
Definition: base_flag.hpp:149
flag & set(const T &flags)
Definition: base_flag.hpp:92
flag()
Definition: base_flag.hpp:56
bool is_set(const T &flags)
Definition: base_flag.hpp:108
T get_flags(void)
Definition: base_flag.hpp:128