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