Mistake on this page?
Report an issue in GitHub or email us
common/SafeBool.h
Go to the documentation of this file.
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2020 ARM Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef BLE_API_SAFE_BOOL_H_
20 #define BLE_API_SAFE_BOOL_H_
21 
22 /* Safe bool idiom, see: http://www.artima.com/cppsource/safebool.html */
23 
24 /**
25  * @file
26  * @addtogroup ble
27  * @{
28  * @addtogroup common
29  * @{
30  */
31 
32 /**
33  * Private namespace used to host details of the SafeBool implementation.
34  */
35 namespace SafeBool_ {
36 /**
37  * Base class of all SafeBool instances.
38  *
39  * This nontemplate base class exists to reduce the number of instantiation of
40  * the trueTag function.
41  */
42 class base {
43  template<typename>
44  friend class SafeBool;
45 
46 protected:
47  /**
48  * The bool type is a pointer to method that can be used in boolean context.
49  */
50  typedef void (base::*BoolType_t)() const;
51 
52  /**
53  * Nonimplemented call, use to disallow conversion between unrelated types.
54  */
55  void invalidTag() const;
56 
57  /**
58  * Special member function that indicates a true value.
59  */
60  void trueTag() const {}
61 };
62 
63 
64 }
65 
66 /**
67  * Safe conversion of objects in boolean context.
68  *
69  * Classes wanting evaluation of their instances in boolean context must derive
70  * publicly from this class rather than implementing the easy to misuse
71  * operator bool().
72  *
73  * Descendant classes must implement the function bool toBool() const to enable
74  * the safe conversion in boolean context.
75  *
76  * @tparam T Type of the derived class
77  *
78  * @code
79  *
80  * class A : public SafeBool<A> {
81  * public:
82  *
83  * // boolean conversion
84  * bool toBool() const {
85  *
86  * }
87  * };
88  *
89  * class B : public SafeBool<B> {
90  * public:
91  *
92  * // boolean conversion
93  * bool toBool() const {
94  *
95  * }
96  * };
97  *
98  * A a;
99  * B b;
100  *
101  * // will compile
102  * if(a) {
103  *
104  * }
105  *
106  * // compilation error
107  * if(a == b) {
108  *
109  * }
110  * @endcode
111  */
112 template <typename T>
113 class SafeBool : public SafeBool_::base {
114 public:
115  /**
116  * Bool operator implementation, derived class must provide a bool
117  * toBool() const function.
118  */
119  operator BoolType_t() const
120  {
121  return (static_cast<const T*>(this))->toBool()
122  ? &SafeBool<T>::trueTag : 0;
123  }
124 };
125 
126 /**
127  * Avoid conversion to bool between different classes.
128  *
129  * @attention Will generate a compile time error if instantiated.
130  */
131 template <typename T, typename U>
132 void operator==(const SafeBool<T>& lhs,const SafeBool<U>& rhs)
133 {
134  lhs.invalidTag();
135  // return false;
136 }
137 
138 /**
139  * Avoid conversion to bool between different classes.
140  *
141  * @attention Will generate a compile time error if instantiated.
142  */
143 template <typename T,typename U>
144 void operator!=(const SafeBool<T>& lhs,const SafeBool<U>& rhs)
145 {
146  lhs.invalidTag();
147  // return false;
148 }
149 
150 /**
151  * @}
152  * @}
153  */
154 
155 
156 #endif /* BLE_API_SAFE_BOOL_H_ */
void(base::* BoolType_t)() const
The bool type is a pointer to method that can be used in boolean context.
void invalidTag() const
Nonimplemented call, use to disallow conversion between unrelated types.
Base class of all SafeBool instances.
Private namespace used to host details of the SafeBool implementation.
void operator!=(const SafeBool< T > &lhs, const SafeBool< U > &rhs)
Avoid conversion to bool between different classes.
void operator==(const SafeBool< T > &lhs, const SafeBool< U > &rhs)
Avoid conversion to bool between different classes.
Safe conversion of objects in boolean context.
void trueTag() const
Special member function that indicates a true value.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.