Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of LinkNode-Test by
SafeBool.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef BLE_API_SAFE_BOOL_H_ 00018 #define BLE_API_SAFE_BOOL_H_ 00019 00020 //safe bool idiom, see : http://www.artima.com/cppsource/safebool.html 00021 00022 namespace SafeBool_ { 00023 /** 00024 * @brief Base class for all intances of SafeBool, 00025 * This base class reduce instantiation of trueTag function 00026 */ 00027 class base { 00028 template<typename> 00029 friend class SafeBool; 00030 00031 protected: 00032 //the bool type is a pointer to method which can be used in boolean context 00033 typedef void (base::*BoolType_t)() const; 00034 00035 // non implemented call, use to disallow conversion between unrelated types 00036 void invalidTag() const; 00037 00038 // member function which indicate true value 00039 void trueTag() const {} 00040 }; 00041 00042 00043 } 00044 00045 /** 00046 * @brief template class SafeBool use CRTP to made boolean conversion easy and correct. 00047 * Derived class should implement the function bool toBool() const to make this work. Inheritance 00048 * should be public. 00049 * 00050 * @tparam T Type of the derived class 00051 * 00052 * \code 00053 * 00054 * class A : public SafeBool<A> { 00055 * public: 00056 * 00057 * // boolean conversion 00058 * bool toBool() { 00059 * 00060 * } 00061 * }; 00062 * 00063 * class B : public SafeBool<B> { 00064 * public: 00065 * 00066 * // boolean conversion 00067 * bool toBool() const { 00068 * 00069 * } 00070 * }; 00071 * 00072 * A a; 00073 * B b; 00074 * 00075 * // will compile 00076 * if(a) { 00077 * 00078 * } 00079 * 00080 * // compilation error 00081 * if(a == b) { 00082 * 00083 * } 00084 * 00085 * 00086 * \endcode 00087 */ 00088 template <typename T> 00089 class SafeBool : public SafeBool_::base { 00090 public: 00091 /** 00092 * bool operator implementation, derived class has to provide bool toBool() const function. 00093 */ 00094 operator BoolType_t() const { 00095 return (static_cast<const T*>(this))->toBool() 00096 ? &SafeBool<T>::trueTag : 0; 00097 } 00098 }; 00099 00100 //Avoid conversion to bool between different classes 00101 template <typename T, typename U> 00102 void operator==(const SafeBool<T>& lhs,const SafeBool<U>& rhs) { 00103 lhs.invalidTag(); 00104 // return false; 00105 } 00106 00107 //Avoid conversion to bool between different classes 00108 template <typename T,typename U> 00109 void operator!=(const SafeBool<T>& lhs,const SafeBool<U>& rhs) { 00110 lhs.invalidTag(); 00111 // return false; 00112 } 00113 00114 #endif /* BLE_API_SAFE_BOOL_H_ */
Generated on Tue Jul 12 2022 16:00:22 by
