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