Imt.Base C++ API V4.1.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Flags.h
Go to the documentation of this file.
1// (c) IMT - Information Management Technology AG, CH-9470 Buchs, www.imt.ch.
2//
3// ActiveParts (AP) and the corresponding Data Flow Framework (DFF) is invented and designed by Jakob Daescher.
4// ANY USE OF THIS CODE CONSTITUTES ACCEPTANCE OF THE TERMS OF THE COPYRIGHT NOTICE.
5// ===================================================================================================
6// COPYRIGHT NOTICE
7// ===================================================================================================
8// Copyright (C) 2005-2075, IMT Information Management Technology AG, 9470 Buchs, Switzerland
9// All rights reserved.
10// This code is proprietary software of IMT Information Management Technology AG (hereinafter: "IMT").
11// Proprietary software is computer software licensed under exclusive legal right of IMT.
12//
13// The licensee is given the irrevocable, perpetual, worldwide, non-exclusive right and license to use,
14// execute and reproduce the software in binary form within the licensed products.
15//
16// Redistribution and use in source forms, without modification, are permitted provided that the following conditions are met:
17// (1) Copying of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
18// (2) Copying of source code is only allowed for regulatory documentation and archiving purposes
19// (3) Redistributions in binary form must reproduce the above copyright notice,
20// this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
21//
22// IMT provide no reassurances that the source code provided does not infringe
23// any patent, copyright, or any other intellectual property rights of third parties.
24// IMT disclaim any liability to any recipient for claims brought against
25// recipient by any third party for infringement of that parties intellectual property rights.
26//
27// THIS SOFTWARE IS PROVIDED BY IMT AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
28// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29// IN NO EVENT SHALL IMT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCURE-MENT OF SUBSTITUTE GOODS OR SERVICES;
31// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34// ===================================================================================================
35
36#ifndef IMT_BASE_CORE_UTIL_FLAGS_H
37#define IMT_BASE_CORE_UTIL_FLAGS_H
38
41#include <type_traits>
42
43namespace imt {
44namespace base {
45namespace core {
46namespace util {
47
73template<typename EnumType, typename LimitsType, typename BaseType>
74class Flags final {
75
76public:
77
82 Flags() noexcept;
83
89 explicit Flags(EnumType const flag) noexcept;
90
96 explicit Flags(BaseType const flags) noexcept;
97
102 void set(EnumType const flag);
103
108 void clear(EnumType const flag);
109
115 bool isSet(EnumType const flag) const;
116
121 BaseType const& toInteger() const;
122
129 Flags combine(Flags const& other) const;
130
137 Flags intersect(Flags const& other) const;
138
139private:
140
141 BaseType toBase(EnumType const flag) const;
142
143 BaseType m_flags;
144};
145
146// implementation must be here because of linker
147
148template<typename EnumType, typename LimitsType, typename BaseType>
150 Flags {0} {
151 // the base type must be capable of holding all the flag bits
152 static_assert((sizeof(BaseType) * 8U) >= LimitsType::MAX, "Base type size to small");
153 // The enum must start at 0
154 static_assert(LimitsType::MIN == 0, "Enum does not start at 0");
155 // The enum max value must be correct
156 static_assert(LimitsType::MAX == (LimitsType::COUNT - 1), "Enum not sequentialy numbered");
157}
158
159template<typename EnumType, typename LimitsType, typename BaseType>
160Flags<EnumType, LimitsType, BaseType>::Flags(EnumType const flag) noexcept :
161 Flags {toBase(flag)} {
162}
163
164template<typename EnumType, typename LimitsType, typename BaseType>
165Flags<EnumType, LimitsType, BaseType>::Flags(BaseType const flags) noexcept :
166 m_flags(flags) {
167}
168
169template<typename EnumType, typename LimitsType, typename BaseType>
171 this->m_flags |= toBase(flag);
172}
173
174template<typename EnumType, typename LimitsType, typename BaseType>
176 this->m_flags &= ~(toBase(flag));
177}
178
179template<typename EnumType, typename LimitsType, typename BaseType>
180bool Flags<EnumType, LimitsType, BaseType>::isSet(EnumType const flag) const {
181 BaseType const mask {toBase(flag)};
182 return (this->m_flags & mask) != 0;
183}
184
185template<typename EnumType, typename LimitsType, typename BaseType>
187 return this->m_flags;
188}
189
190template<typename EnumType, typename LimitsType, typename BaseType>
192 return Flags(this->m_flags | other.m_flags);
193}
194
195template<typename EnumType, typename LimitsType, typename BaseType>
197 return Flags(this->m_flags & other.m_flags);
198}
199
200template<typename EnumType, typename LimitsType, typename BaseType>
201BaseType Flags<EnumType, LimitsType, BaseType>::toBase(EnumType const flag) const {
202 return static_cast<BaseType>(1U << static_cast<size_t>(flag));
203}
204
211template<typename EnumType, typename LimitsType, typename BaseType>
213 return left.toInteger() == right.toInteger();
214}
215
222template<typename EnumType, typename LimitsType, typename BaseType>
224 return !(left == right);
225}
226
227} // namespace util
228} // namespace core
229} // namespace base
230} // namespace imt
231
232#endif // IMT_BASE_CORE_UTIL_FLAGS_H
A template to create a type safe flags type from an enum.
Definition Flags.h:74
Flags intersect(Flags const &other) const
Intersects the flags with another instance.
Definition Flags.h:196
void clear(EnumType const flag)
Clears the given flag.
Definition Flags.h:175
Flags() noexcept
Default constructor.
Definition Flags.h:149
BaseType const & toInteger() const
Returns the base type.
Definition Flags.h:186
Flags combine(Flags const &other) const
Combines the flags with another instance.
Definition Flags.h:191
bool isSet(EnumType const flag) const
Checks whether the given flag is set.
Definition Flags.h:180
void set(EnumType const flag)
Sets the given flag.
Definition Flags.h:170
bool operator==(DateTimeStamp const left, DateTimeStamp const right) noexcept
Equals operator overload.
bool operator!=(DateTimeStamp const left, DateTimeStamp const right) noexcept
Not equals operator overload.
This is a application specific file which is used to configure Imt.Base.Core.Math.