Loading...
Searching...
No Matches
flags.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
42#pragma once
43
44namespace ledger { namespace flags {
45
46template <typename T = boost::uint_least8_t, typename U = T>
48{
49public:
50 typedef T flags_t;
51
52protected:
54
55public:
63 TRACE_CTOR(supports_flags, "const flags_t&");
64 }
68
71 return *this;
72 }
73
74 flags_t flags() const {
75 return _flags;
76 }
77 bool has_flags(const flags_t arg) const {
78 return _flags & arg;
79 }
80
81 void set_flags(const flags_t arg) {
82 _flags = arg;
83 }
84 void clear_flags() {
85 _flags = static_cast<T>(0);
86 }
87 void add_flags(const flags_t arg) {
88 _flags = static_cast<T>(static_cast<U>(_flags) | static_cast<U>(arg));
89 }
90 void drop_flags(const flags_t arg) {
91 _flags = static_cast<T>(static_cast<U>(_flags) & static_cast<U>(~arg));
92 }
93};
94
95template <typename T = boost::uint_least8_t, typename U = T>
96class basic_t : public supports_flags<T, U>
97{
98public:
100 TRACE_CTOR(basic_t, "");
101 }
102 basic_t(const T& bits) {
103 TRACE_CTOR(basic_t, "const T&");
105 }
106 basic_t(const U& bits) {
107 TRACE_CTOR(basic_t, "const U&");
108 supports_flags<T, U>::set_flags(static_cast<T>(bits));
109 }
112 }
113
115 : supports_flags<T, U>(other) {
116 TRACE_CTOR(basic_t, "copy");
117 }
119 set_flags(other.flags());
120 return *this;
121 }
122 basic_t& operator=(const T& bits) {
124 return *this;
125 }
126
127 operator T() const {
129 }
130 operator U() const {
132 }
133
134 basic_t plus_flags(const T& arg) const {
135 basic_t temp(*this);
136 temp.add_flags(arg);
137 return temp;
138 }
139 basic_t minus_flags(const T& arg) const {
140 basic_t temp(*this);
141 temp.drop_flags(arg);
142 return temp;
143 }
144};
145
146template <typename T = boost::uint_least8_t>
147class delegates_flags : public boost::noncopyable
148{
149public:
150 typedef T flags_t;
151
152protected:
154
155public:
160 TRACE_CTOR(delegates_flags, "const supports_flags<T>&");
161 }
165
166 flags_t flags() const {
167 return _flags.flags();
168 }
169 bool has_flags(const flags_t arg) const {
170 return _flags.has_flags(arg);
171 }
172
173 void set_flags(const flags_t arg) {
174 _flags.set_flags(arg);
175 }
176 void clear_flags() {
177 _flags.clear_flags();
178 }
179 void add_flags(const flags_t arg) {
180 _flags.add_flags(arg);
181 }
182 void drop_flags(const flags_t arg) {
183 _flags.drop_flags(arg);
184 }
185};
186
187} } // namespace ledger::flags
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
T & downcast(U &object)
Definition utils.h:468
void drop_flags(const flags_t arg)
Definition flags.h:90
void add_flags(const flags_t arg)
Definition flags.h:87
supports_flags(const supports_flags &arg)
Definition flags.h:59
supports_flags(const flags_t &arg)
Definition flags.h:62
void set_flags(const flags_t arg)
Definition flags.h:81
supports_flags & operator=(const supports_flags &other)
Definition flags.h:69
bool has_flags(const flags_t arg) const
Definition flags.h:77
flags_t flags() const
Definition flags.h:74
basic_t minus_flags(const T &arg) const
Definition flags.h:139
basic_t(const basic_t &other)
Definition flags.h:114
basic_t & operator=(const basic_t &other)
Definition flags.h:118
basic_t plus_flags(const T &arg) const
Definition flags.h:134
basic_t(const T &bits)
Definition flags.h:102
basic_t & operator=(const T &bits)
Definition flags.h:122
basic_t(const U &bits)
Definition flags.h:106
delegates_flags(supports_flags< T > &arg)
Definition flags.h:159
void add_flags(const flags_t arg)
Definition flags.h:179
bool has_flags(const flags_t arg) const
Definition flags.h:169
flags_t flags() const
Definition flags.h:166
supports_flags< T > & _flags
Definition flags.h:153
void drop_flags(const flags_t arg)
Definition flags.h:182
void set_flags(const flags_t arg)
Definition flags.h:173