Pinned to some recent date
features/FEATURE_BLE/ble/SafeBool.h@0:fb7af294d5d9, 2016-11-17 (annotated)
- Committer:
- Simon Cooksey
- Date:
- Thu Nov 17 16:43:53 2016 +0000
- Revision:
- 0:fb7af294d5d9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Simon Cooksey |
0:fb7af294d5d9 | 1 | /* mbed Microcontroller Library |
Simon Cooksey |
0:fb7af294d5d9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Simon Cooksey |
0:fb7af294d5d9 | 3 | * |
Simon Cooksey |
0:fb7af294d5d9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Simon Cooksey |
0:fb7af294d5d9 | 5 | * you may not use this file except in compliance with the License. |
Simon Cooksey |
0:fb7af294d5d9 | 6 | * You may obtain a copy of the License at |
Simon Cooksey |
0:fb7af294d5d9 | 7 | * |
Simon Cooksey |
0:fb7af294d5d9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Simon Cooksey |
0:fb7af294d5d9 | 9 | * |
Simon Cooksey |
0:fb7af294d5d9 | 10 | * Unless required by applicable law or agreed to in writing, software |
Simon Cooksey |
0:fb7af294d5d9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Simon Cooksey |
0:fb7af294d5d9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Simon Cooksey |
0:fb7af294d5d9 | 13 | * See the License for the specific language governing permissions and |
Simon Cooksey |
0:fb7af294d5d9 | 14 | * limitations under the License. |
Simon Cooksey |
0:fb7af294d5d9 | 15 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 16 | |
Simon Cooksey |
0:fb7af294d5d9 | 17 | #ifndef BLE_API_SAFE_BOOL_H_ |
Simon Cooksey |
0:fb7af294d5d9 | 18 | #define BLE_API_SAFE_BOOL_H_ |
Simon Cooksey |
0:fb7af294d5d9 | 19 | |
Simon Cooksey |
0:fb7af294d5d9 | 20 | /* Safe bool idiom, see : http://www.artima.com/cppsource/safebool.html */ |
Simon Cooksey |
0:fb7af294d5d9 | 21 | |
Simon Cooksey |
0:fb7af294d5d9 | 22 | namespace SafeBool_ { |
Simon Cooksey |
0:fb7af294d5d9 | 23 | /** |
Simon Cooksey |
0:fb7af294d5d9 | 24 | * @brief Base class for all intances of SafeBool. |
Simon Cooksey |
0:fb7af294d5d9 | 25 | * This base class reduces instantiation of trueTag function. |
Simon Cooksey |
0:fb7af294d5d9 | 26 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 27 | class base { |
Simon Cooksey |
0:fb7af294d5d9 | 28 | template<typename> |
Simon Cooksey |
0:fb7af294d5d9 | 29 | friend class SafeBool; |
Simon Cooksey |
0:fb7af294d5d9 | 30 | |
Simon Cooksey |
0:fb7af294d5d9 | 31 | protected: |
Simon Cooksey |
0:fb7af294d5d9 | 32 | /** |
Simon Cooksey |
0:fb7af294d5d9 | 33 | * The bool type is a pointer to method which can be used in boolean context. |
Simon Cooksey |
0:fb7af294d5d9 | 34 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 35 | typedef void (base::*BoolType_t)() const; |
Simon Cooksey |
0:fb7af294d5d9 | 36 | |
Simon Cooksey |
0:fb7af294d5d9 | 37 | /** |
Simon Cooksey |
0:fb7af294d5d9 | 38 | * Non implemented call, use to disallow conversion between unrelated types. |
Simon Cooksey |
0:fb7af294d5d9 | 39 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 40 | void invalidTag() const; |
Simon Cooksey |
0:fb7af294d5d9 | 41 | |
Simon Cooksey |
0:fb7af294d5d9 | 42 | /** |
Simon Cooksey |
0:fb7af294d5d9 | 43 | * Member function which indicate true value. |
Simon Cooksey |
0:fb7af294d5d9 | 44 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 45 | void trueTag() const {} |
Simon Cooksey |
0:fb7af294d5d9 | 46 | }; |
Simon Cooksey |
0:fb7af294d5d9 | 47 | |
Simon Cooksey |
0:fb7af294d5d9 | 48 | |
Simon Cooksey |
0:fb7af294d5d9 | 49 | } |
Simon Cooksey |
0:fb7af294d5d9 | 50 | |
Simon Cooksey |
0:fb7af294d5d9 | 51 | /** |
Simon Cooksey |
0:fb7af294d5d9 | 52 | * @brief template class SafeBool use CRTP to made boolean conversion easy and correct. |
Simon Cooksey |
0:fb7af294d5d9 | 53 | * Derived class should implement the function bool toBool() const to make this work. Inheritance |
Simon Cooksey |
0:fb7af294d5d9 | 54 | * should be public. |
Simon Cooksey |
0:fb7af294d5d9 | 55 | * |
Simon Cooksey |
0:fb7af294d5d9 | 56 | * @tparam T Type of the derived class |
Simon Cooksey |
0:fb7af294d5d9 | 57 | * |
Simon Cooksey |
0:fb7af294d5d9 | 58 | * @code |
Simon Cooksey |
0:fb7af294d5d9 | 59 | * |
Simon Cooksey |
0:fb7af294d5d9 | 60 | * class A : public SafeBool<A> { |
Simon Cooksey |
0:fb7af294d5d9 | 61 | * public: |
Simon Cooksey |
0:fb7af294d5d9 | 62 | * |
Simon Cooksey |
0:fb7af294d5d9 | 63 | * // boolean conversion |
Simon Cooksey |
0:fb7af294d5d9 | 64 | * bool toBool() { |
Simon Cooksey |
0:fb7af294d5d9 | 65 | * |
Simon Cooksey |
0:fb7af294d5d9 | 66 | * } |
Simon Cooksey |
0:fb7af294d5d9 | 67 | * }; |
Simon Cooksey |
0:fb7af294d5d9 | 68 | * |
Simon Cooksey |
0:fb7af294d5d9 | 69 | * class B : public SafeBool<B> { |
Simon Cooksey |
0:fb7af294d5d9 | 70 | * public: |
Simon Cooksey |
0:fb7af294d5d9 | 71 | * |
Simon Cooksey |
0:fb7af294d5d9 | 72 | * // boolean conversion |
Simon Cooksey |
0:fb7af294d5d9 | 73 | * bool toBool() const { |
Simon Cooksey |
0:fb7af294d5d9 | 74 | * |
Simon Cooksey |
0:fb7af294d5d9 | 75 | * } |
Simon Cooksey |
0:fb7af294d5d9 | 76 | * }; |
Simon Cooksey |
0:fb7af294d5d9 | 77 | * |
Simon Cooksey |
0:fb7af294d5d9 | 78 | * A a; |
Simon Cooksey |
0:fb7af294d5d9 | 79 | * B b; |
Simon Cooksey |
0:fb7af294d5d9 | 80 | * |
Simon Cooksey |
0:fb7af294d5d9 | 81 | * // will compile |
Simon Cooksey |
0:fb7af294d5d9 | 82 | * if(a) { |
Simon Cooksey |
0:fb7af294d5d9 | 83 | * |
Simon Cooksey |
0:fb7af294d5d9 | 84 | * } |
Simon Cooksey |
0:fb7af294d5d9 | 85 | * |
Simon Cooksey |
0:fb7af294d5d9 | 86 | * // compilation error |
Simon Cooksey |
0:fb7af294d5d9 | 87 | * if(a == b) { |
Simon Cooksey |
0:fb7af294d5d9 | 88 | * |
Simon Cooksey |
0:fb7af294d5d9 | 89 | * } |
Simon Cooksey |
0:fb7af294d5d9 | 90 | * |
Simon Cooksey |
0:fb7af294d5d9 | 91 | * |
Simon Cooksey |
0:fb7af294d5d9 | 92 | * @endcode |
Simon Cooksey |
0:fb7af294d5d9 | 93 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 94 | template <typename T> |
Simon Cooksey |
0:fb7af294d5d9 | 95 | class SafeBool : public SafeBool_::base { |
Simon Cooksey |
0:fb7af294d5d9 | 96 | public: |
Simon Cooksey |
0:fb7af294d5d9 | 97 | /** |
Simon Cooksey |
0:fb7af294d5d9 | 98 | * Bool operator implementation, derived class has to provide bool toBool() const function. |
Simon Cooksey |
0:fb7af294d5d9 | 99 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 100 | operator BoolType_t() const { |
Simon Cooksey |
0:fb7af294d5d9 | 101 | return (static_cast<const T*>(this))->toBool() |
Simon Cooksey |
0:fb7af294d5d9 | 102 | ? &SafeBool<T>::trueTag : 0; |
Simon Cooksey |
0:fb7af294d5d9 | 103 | } |
Simon Cooksey |
0:fb7af294d5d9 | 104 | }; |
Simon Cooksey |
0:fb7af294d5d9 | 105 | |
Simon Cooksey |
0:fb7af294d5d9 | 106 | /** |
Simon Cooksey |
0:fb7af294d5d9 | 107 | * Avoid conversion to bool between different classes. |
Simon Cooksey |
0:fb7af294d5d9 | 108 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 109 | template <typename T, typename U> |
Simon Cooksey |
0:fb7af294d5d9 | 110 | void operator==(const SafeBool<T>& lhs,const SafeBool<U>& rhs) { |
Simon Cooksey |
0:fb7af294d5d9 | 111 | lhs.invalidTag(); |
Simon Cooksey |
0:fb7af294d5d9 | 112 | // return false; |
Simon Cooksey |
0:fb7af294d5d9 | 113 | } |
Simon Cooksey |
0:fb7af294d5d9 | 114 | |
Simon Cooksey |
0:fb7af294d5d9 | 115 | /** |
Simon Cooksey |
0:fb7af294d5d9 | 116 | * Avoid conversion to bool between different classes. |
Simon Cooksey |
0:fb7af294d5d9 | 117 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 118 | template <typename T,typename U> |
Simon Cooksey |
0:fb7af294d5d9 | 119 | void operator!=(const SafeBool<T>& lhs,const SafeBool<U>& rhs) { |
Simon Cooksey |
0:fb7af294d5d9 | 120 | lhs.invalidTag(); |
Simon Cooksey |
0:fb7af294d5d9 | 121 | // return false; |
Simon Cooksey |
0:fb7af294d5d9 | 122 | } |
Simon Cooksey |
0:fb7af294d5d9 | 123 | |
Simon Cooksey |
0:fb7af294d5d9 | 124 | #endif /* BLE_API_SAFE_BOOL_H_ */ |