19
19
20
20
/* Safe bool idiom, see : http://www.artima.com/cppsource/safebool.html */
21
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
+ */
22
33
namespace SafeBool_ {
23
34
/* *
24
- * @brief Base class for all intances of SafeBool.
25
- * This base class reduces instantiation of trueTag function.
35
+ * Base class of all SafeBool instances.
36
+ *
37
+ * This non template base class exists to reduces the number of instantiation of
38
+ * the trueTag function.
26
39
*/
27
40
class base {
28
41
template <typename >
@@ -40,7 +53,7 @@ class base {
40
53
void invalidTag () const ;
41
54
42
55
/* *
43
- * Member function which indicate true value.
56
+ * Special member function which indicate a true value.
44
57
*/
45
58
void trueTag () const {}
46
59
};
@@ -49,9 +62,14 @@ class base {
49
62
}
50
63
51
64
/* *
52
- * @brief template class SafeBool use CRTP to made boolean conversion easy and correct.
53
- * Derived class should implement the function bool toBool() const to make this work. Inheritance
54
- * should be public.
65
+ * Safe conversion of objects in boolean context.
66
+ *
67
+ * Classes wanting to evaluation of their instances in boolean context must
68
+ * derive 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.
55
73
*
56
74
* @tparam T Type of the derived class
57
75
*
@@ -61,7 +79,7 @@ class base {
61
79
* public:
62
80
*
63
81
* // boolean conversion
64
- * bool toBool() {
82
+ * bool toBool() const {
65
83
*
66
84
* }
67
85
* };
@@ -87,38 +105,50 @@ class base {
87
105
* if(a == b) {
88
106
*
89
107
* }
90
- *
91
- *
92
108
* @endcode
93
109
*/
94
110
template <typename T>
95
111
class SafeBool : public SafeBool_ ::base {
96
112
public:
97
113
/* *
98
- * Bool operator implementation, derived class has to provide bool toBool() const function.
114
+ * Bool operator implementation, derived class must provide a bool
115
+ * toBool() const function.
99
116
*/
100
- operator BoolType_t () const {
117
+ operator BoolType_t () const
118
+ {
101
119
return (static_cast <const T*>(this ))->toBool ()
102
120
? &SafeBool<T>::trueTag : 0 ;
103
121
}
104
122
};
105
123
106
124
/* *
107
125
* Avoid conversion to bool between different classes.
126
+ *
127
+ * @important Will generate a compile time error if instantiated.
108
128
*/
109
129
template <typename T, typename U>
110
- void operator ==(const SafeBool<T>& lhs,const SafeBool<U>& rhs) {
130
+ void operator ==(const SafeBool<T>& lhs,const SafeBool<U>& rhs)
131
+ {
111
132
lhs.invalidTag ();
112
133
// return false;
113
134
}
114
135
115
136
/* *
116
137
* Avoid conversion to bool between different classes.
138
+ *
139
+ * @important Will generate a compile time error if instantiated.
117
140
*/
118
141
template <typename T,typename U>
119
- void operator !=(const SafeBool<T>& lhs,const SafeBool<U>& rhs) {
142
+ void operator !=(const SafeBool<T>& lhs,const SafeBool<U>& rhs)
143
+ {
120
144
lhs.invalidTag ();
121
145
// return false;
122
146
}
123
147
148
+ /* *
149
+ * @}
150
+ * @}
151
+ */
152
+
153
+
124
154
#endif /* BLE_API_SAFE_BOOL_H_ */
0 commit comments