2016-07-20 21:10:36 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2006-2013 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLE_API_SAFE_BOOL_H_
|
|
|
|
#define BLE_API_SAFE_BOOL_H_
|
|
|
|
|
2017-11-07 22:25:45 +00:00
|
|
|
/* Safe bool idiom, see: http://www.artima.com/cppsource/safebool.html */
|
2016-07-20 21:10:36 +00:00
|
|
|
|
2017-10-24 15:53:25 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @addtogroup ble
|
|
|
|
* @{
|
|
|
|
* @addtogroup common
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private namespace used to host details of the SafeBool implementation.
|
|
|
|
*/
|
2016-07-20 21:10:36 +00:00
|
|
|
namespace SafeBool_ {
|
|
|
|
/**
|
2017-10-24 15:53:25 +00:00
|
|
|
* Base class of all SafeBool instances.
|
|
|
|
*
|
2017-11-07 22:25:45 +00:00
|
|
|
* This nontemplate base class exists to reduce the number of instantiation of
|
2017-10-24 15:53:25 +00:00
|
|
|
* the trueTag function.
|
2016-07-20 21:10:36 +00:00
|
|
|
*/
|
|
|
|
class base {
|
|
|
|
template<typename>
|
|
|
|
friend class SafeBool;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
2017-11-07 22:25:45 +00:00
|
|
|
* The bool type is a pointer to method that can be used in boolean context.
|
2016-07-20 21:10:36 +00:00
|
|
|
*/
|
|
|
|
typedef void (base::*BoolType_t)() const;
|
|
|
|
|
|
|
|
/**
|
2017-11-07 22:25:45 +00:00
|
|
|
* Nonimplemented call, use to disallow conversion between unrelated types.
|
2016-07-20 21:10:36 +00:00
|
|
|
*/
|
|
|
|
void invalidTag() const;
|
|
|
|
|
|
|
|
/**
|
2017-11-07 22:25:45 +00:00
|
|
|
* Special member function that indicates a true value.
|
2016-07-20 21:10:36 +00:00
|
|
|
*/
|
|
|
|
void trueTag() const {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-24 15:53:25 +00:00
|
|
|
* Safe conversion of objects in boolean context.
|
|
|
|
*
|
2017-10-30 15:37:59 +00:00
|
|
|
* Classes wanting evaluation of their instances in boolean context must derive
|
|
|
|
* publicly from this class rather than implementing the easy to misuse
|
2017-10-24 15:53:25 +00:00
|
|
|
* operator bool().
|
|
|
|
*
|
|
|
|
* Descendant classes must implement the function bool toBool() const to enable
|
|
|
|
* the safe conversion in boolean context.
|
2016-07-20 21:10:36 +00:00
|
|
|
*
|
|
|
|
* @tparam T Type of the derived class
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
*
|
|
|
|
* class A : public SafeBool<A> {
|
|
|
|
* public:
|
|
|
|
*
|
|
|
|
* // boolean conversion
|
2017-10-24 15:53:25 +00:00
|
|
|
* bool toBool() const {
|
2016-07-20 21:10:36 +00:00
|
|
|
*
|
|
|
|
* }
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* class B : public SafeBool<B> {
|
|
|
|
* public:
|
|
|
|
*
|
|
|
|
* // boolean conversion
|
|
|
|
* bool toBool() const {
|
|
|
|
*
|
|
|
|
* }
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* A a;
|
|
|
|
* B b;
|
|
|
|
*
|
|
|
|
* // will compile
|
|
|
|
* if(a) {
|
|
|
|
*
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // compilation error
|
|
|
|
* if(a == b) {
|
|
|
|
*
|
|
|
|
* }
|
|
|
|
* @endcode
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class SafeBool : public SafeBool_::base {
|
|
|
|
public:
|
|
|
|
/**
|
2017-10-24 15:53:25 +00:00
|
|
|
* Bool operator implementation, derived class must provide a bool
|
|
|
|
* toBool() const function.
|
2016-07-20 21:10:36 +00:00
|
|
|
*/
|
2017-10-24 15:53:25 +00:00
|
|
|
operator BoolType_t() const
|
|
|
|
{
|
2016-07-20 21:10:36 +00:00
|
|
|
return (static_cast<const T*>(this))->toBool()
|
|
|
|
? &SafeBool<T>::trueTag : 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Avoid conversion to bool between different classes.
|
2017-10-24 15:53:25 +00:00
|
|
|
*
|
2017-12-13 16:14:16 +00:00
|
|
|
* @attention Will generate a compile time error if instantiated.
|
2016-07-20 21:10:36 +00:00
|
|
|
*/
|
|
|
|
template <typename T, typename U>
|
2017-10-24 15:53:25 +00:00
|
|
|
void operator==(const SafeBool<T>& lhs,const SafeBool<U>& rhs)
|
|
|
|
{
|
2016-07-20 21:10:36 +00:00
|
|
|
lhs.invalidTag();
|
|
|
|
// return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Avoid conversion to bool between different classes.
|
2017-10-24 15:53:25 +00:00
|
|
|
*
|
2017-12-13 16:14:16 +00:00
|
|
|
* @attention Will generate a compile time error if instantiated.
|
2016-07-20 21:10:36 +00:00
|
|
|
*/
|
|
|
|
template <typename T,typename U>
|
2017-10-24 15:53:25 +00:00
|
|
|
void operator!=(const SafeBool<T>& lhs,const SafeBool<U>& rhs)
|
|
|
|
{
|
2016-07-20 21:10:36 +00:00
|
|
|
lhs.invalidTag();
|
|
|
|
// return false;
|
|
|
|
}
|
|
|
|
|
2017-10-24 15:53:25 +00:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-07-20 21:10:36 +00:00
|
|
|
#endif /* BLE_API_SAFE_BOOL_H_ */
|