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