1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2017 Realm Inc.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ////////////////////////////////////////////////////////////////////////////
19 #ifndef REALM_OS_UTIL_TAGGED_BOOL_HPP
20 #define REALM_OS_UTIL_TAGGED_BOOL_HPP
22 #include <type_traits>
26 // A type factory which defines a type which is implicitly convertable to and
27 // from `bool`, but not to other TaggedBool types
30 // using IsIndexed = util::TaggedBool<class IsIndexedTag>;
31 // using IsPrimary = util::TaggedBool<class IsPrimaryTag>;
32 // void foo(IsIndexed is_indexed, IsPrimary is_primary);
34 // foo(IsIndexed{true}, IsPrimary{false}); // compiles
35 // foo(IsPrimary{true}, IsIndexed{false}); // doesn't compile
36 template <typename Tag>
38 // Allow explicit construction from anything convertible to bool
39 constexpr explicit TaggedBool(bool v) : m_value(v) { }
41 // Allow implicit construction from *just* bool and not things convertible
42 // to bool (such as other types of tagged bools)
43 template <typename Bool, typename = typename std::enable_if<std::is_same<Bool, bool>::value>::type>
44 constexpr TaggedBool(Bool v) : m_value(v) {}
46 constexpr TaggedBool(TaggedBool const& v) : m_value(v.m_value) {}
48 constexpr operator bool() const { return m_value; }
49 constexpr TaggedBool operator!() const { return TaggedBool{!m_value}; }
51 friend constexpr bool operator==(TaggedBool l, TaggedBool r) { return l.m_value == r.m_value; }
52 friend constexpr bool operator!=(TaggedBool l, TaggedBool r) { return l.m_value != r.m_value; }
60 #endif // REALM_OS_UTIL_TAGGED_BOOL_HPP