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