Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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