added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / util / tagged_bool.hpp
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2017 Realm Inc.
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 #ifndef REALM_OS_UTIL_TAGGED_BOOL_HPP
20 #define REALM_OS_UTIL_TAGGED_BOOL_HPP
21
22 #include <type_traits>
23
24 namespace realm {
25 namespace util {
26 // A type factory which defines a type which is implicitly convertable to and
27 // from `bool`, but not to other TaggedBool types
28 //
29 // Usage:
30 // using IsIndexed = util::TaggedBool<class IsIndexedTag>;
31 // using IsPrimary = util::TaggedBool<class IsPrimaryTag>;
32 // void foo(IsIndexed is_indexed, IsPrimary is_primary);
33 //
34 // foo(IsIndexed{true}, IsPrimary{false}); // compiles
35 // foo(IsPrimary{true}, IsIndexed{false}); // doesn't compile
36 template <typename Tag>
37 struct TaggedBool {
38     // Allow explicit construction from anything convertible to bool
39     constexpr explicit TaggedBool(bool v) : m_value(v) { }
40
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) {}
45
46     constexpr TaggedBool(TaggedBool const& v) : m_value(v.m_value) {}
47
48     constexpr operator bool() const { return m_value; }
49     constexpr TaggedBool operator!() const { return TaggedBool{!m_value}; }
50
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; }
53
54 private:
55     bool m_value;
56 };
57
58 }
59 }
60 #endif // REALM_OS_UTIL_TAGGED_BOOL_HPP