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