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