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