High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
ble/SafeBool.h@1155:e28c7aac64ab, 2016-04-06 (annotated)
- Committer:
- vcoubard
- Date:
- Wed Apr 06 19:14:34 2016 +0100
- Revision:
- 1155:e28c7aac64ab
- Parent:
- 1150:45226f13ae5c
- Child:
- 1156:e1ea38b576c6
Synchronized with git rev e20c8c58
Author: Vincent Coubard
Merge pull request #163 from ARMmbed/develop
merge version 2.5.0 into master
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 | 1155:e28c7aac64ab | 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 | 1155:e28c7aac64ab | 24 | * @brief Base class for all intances of SafeBool, |
vcoubard | 1155:e28c7aac64ab | 25 | * This base class reduce 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 | 1155:e28c7aac64ab | 32 | //the bool type is a pointer to method which can be used in boolean context |
vcoubard | 1155:e28c7aac64ab | 33 | typedef void (base::*BoolType_t)() const; |
vcoubard | 1148:0340d5bbbeba | 34 | |
vcoubard | 1155:e28c7aac64ab | 35 | // non implemented call, use to disallow conversion between unrelated types |
vcoubard | 1155:e28c7aac64ab | 36 | void invalidTag() const; |
vcoubard | 1148:0340d5bbbeba | 37 | |
vcoubard | 1155:e28c7aac64ab | 38 | // member function which indicate true value |
vcoubard | 1155:e28c7aac64ab | 39 | void trueTag() const {} |
vcoubard | 1148:0340d5bbbeba | 40 | }; |
vcoubard | 1148:0340d5bbbeba | 41 | |
vcoubard | 1148:0340d5bbbeba | 42 | |
vcoubard | 1148:0340d5bbbeba | 43 | } |
vcoubard | 1148:0340d5bbbeba | 44 | |
vcoubard | 1148:0340d5bbbeba | 45 | /** |
vcoubard | 1148:0340d5bbbeba | 46 | * @brief template class SafeBool use CRTP to made boolean conversion easy and correct. |
vcoubard | 1155:e28c7aac64ab | 47 | * Derived class should implement the function bool toBool() const to make this work. Inheritance |
vcoubard | 1148:0340d5bbbeba | 48 | * should be public. |
vcoubard | 1148:0340d5bbbeba | 49 | * |
vcoubard | 1148:0340d5bbbeba | 50 | * @tparam T Type of the derived class |
vcoubard | 1155:e28c7aac64ab | 51 | * |
vcoubard | 1155:e28c7aac64ab | 52 | * \code |
vcoubard | 1155:e28c7aac64ab | 53 | * |
vcoubard | 1155:e28c7aac64ab | 54 | * class A : public SafeBool<A> { |
vcoubard | 1148:0340d5bbbeba | 55 | * public: |
vcoubard | 1155:e28c7aac64ab | 56 | * |
vcoubard | 1148:0340d5bbbeba | 57 | * // boolean conversion |
vcoubard | 1155:e28c7aac64ab | 58 | * bool toBool() { |
vcoubard | 1155:e28c7aac64ab | 59 | * |
vcoubard | 1155:e28c7aac64ab | 60 | * } |
vcoubard | 1148:0340d5bbbeba | 61 | * }; |
vcoubard | 1155:e28c7aac64ab | 62 | * |
vcoubard | 1155:e28c7aac64ab | 63 | * class B : public SafeBool<B> { |
vcoubard | 1148:0340d5bbbeba | 64 | * public: |
vcoubard | 1155:e28c7aac64ab | 65 | * |
vcoubard | 1148:0340d5bbbeba | 66 | * // boolean conversion |
vcoubard | 1155:e28c7aac64ab | 67 | * bool toBool() const { |
vcoubard | 1155:e28c7aac64ab | 68 | * |
vcoubard | 1155:e28c7aac64ab | 69 | * } |
vcoubard | 1148:0340d5bbbeba | 70 | * }; |
vcoubard | 1155:e28c7aac64ab | 71 | * |
vcoubard | 1148:0340d5bbbeba | 72 | * A a; |
vcoubard | 1148:0340d5bbbeba | 73 | * B b; |
vcoubard | 1155:e28c7aac64ab | 74 | * |
vcoubard | 1155:e28c7aac64ab | 75 | * // will compile |
vcoubard | 1155:e28c7aac64ab | 76 | * if(a) { |
vcoubard | 1155:e28c7aac64ab | 77 | * |
vcoubard | 1148:0340d5bbbeba | 78 | * } |
vcoubard | 1155:e28c7aac64ab | 79 | * |
vcoubard | 1155:e28c7aac64ab | 80 | * // compilation error |
vcoubard | 1155:e28c7aac64ab | 81 | * if(a == b) { |
vcoubard | 1155:e28c7aac64ab | 82 | * |
vcoubard | 1148:0340d5bbbeba | 83 | * } |
vcoubard | 1155:e28c7aac64ab | 84 | * |
vcoubard | 1155:e28c7aac64ab | 85 | * |
vcoubard | 1155:e28c7aac64ab | 86 | * \endcode |
vcoubard | 1148:0340d5bbbeba | 87 | */ |
vcoubard | 1155:e28c7aac64ab | 88 | template <typename T> |
vcoubard | 1148:0340d5bbbeba | 89 | class SafeBool : public SafeBool_::base { |
vcoubard | 1148:0340d5bbbeba | 90 | public: |
vcoubard | 1155:e28c7aac64ab | 91 | /** |
vcoubard | 1155:e28c7aac64ab | 92 | * bool operator implementation, derived class has to provide bool toBool() const function. |
vcoubard | 1155:e28c7aac64ab | 93 | */ |
vcoubard | 1155:e28c7aac64ab | 94 | operator BoolType_t() const { |
vcoubard | 1155:e28c7aac64ab | 95 | return (static_cast<const T*>(this))->toBool() |
vcoubard | 1155:e28c7aac64ab | 96 | ? &SafeBool<T>::trueTag : 0; |
vcoubard | 1155:e28c7aac64ab | 97 | } |
vcoubard | 1148:0340d5bbbeba | 98 | }; |
vcoubard | 1148:0340d5bbbeba | 99 | |
vcoubard | 1155:e28c7aac64ab | 100 | //Avoid conversion to bool between different classes |
vcoubard | 1148:0340d5bbbeba | 101 | template <typename T, typename U> |
vcoubard | 1148:0340d5bbbeba | 102 | void operator==(const SafeBool<T>& lhs,const SafeBool<U>& rhs) { |
vcoubard | 1148:0340d5bbbeba | 103 | lhs.invalidTag(); |
vcoubard | 1155:e28c7aac64ab | 104 | // return false; |
vcoubard | 1148:0340d5bbbeba | 105 | } |
vcoubard | 1148:0340d5bbbeba | 106 | |
vcoubard | 1155:e28c7aac64ab | 107 | //Avoid conversion to bool between different classes |
vcoubard | 1148:0340d5bbbeba | 108 | template <typename T,typename U> |
vcoubard | 1148:0340d5bbbeba | 109 | void operator!=(const SafeBool<T>& lhs,const SafeBool<U>& rhs) { |
vcoubard | 1155:e28c7aac64ab | 110 | lhs.invalidTag(); |
vcoubard | 1155:e28c7aac64ab | 111 | // return false; |
vcoubard | 1148:0340d5bbbeba | 112 | } |
vcoubard | 1148:0340d5bbbeba | 113 | |
rgrover1 | 948:1bb402105289 | 114 | #endif /* BLE_API_SAFE_BOOL_H_ */ |