Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

[architecture] Fix implicit conversion of Flags #230

Merged
merged 3 commits into from
Mar 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/unittest/harness.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ namespace unittest
}

#define TEST_ASSERT_TRUE(expr) \
TEST_RETURN__(::unittest::checkExpression((expr), __LINE__))
TEST_RETURN__(::unittest::checkExpression(static_cast<bool>(expr), __LINE__))

#define TEST_ASSERT_FALSE(expr) \
TEST_RETURN__(::unittest::checkExpression(!static_cast<bool>(expr), __LINE__))
Expand Down
55 changes: 19 additions & 36 deletions src/xpcc/architecture/interface/register.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,16 @@ struct Register
: value(0) {}

/// @{
// The safe-bool idiom is not required here, since we are not
// dealing with complex objects, but with one integral value.
// So there is not need for it.
/* explicit */

/// Returns `true` if `value` is non-zero
constexpr operator bool() const
/**
* Returns `true` if `value` is non-zero
*
* The compiler will allow implicit conversions to bool in the following contexts:
* - conditions of if, while, for, do-while statements
* - logical operators (&&, ||)
* - negation (operator !)
* - static_assert
*/
explicit constexpr operator bool() const
{ return bool(value); }

/// Returns `true` if `value` is zero
Expand All @@ -252,28 +255,6 @@ struct Register
/// This class is meant to be subclassed
constexpr Register(UnderlyingType value)
: value(value) {}

#ifndef __DOXYGEN__
public:
// do NOT cast to anything else
/// all other conversion operators are deleted and must be explicitly implemented
constexpr operator char16_t() const = delete;
constexpr operator char32_t() const = delete;
constexpr operator wchar_t() const = delete;
constexpr operator signed char() const = delete;
constexpr operator unsigned char() const = delete;
constexpr operator signed short int() const = delete;
constexpr operator unsigned short int() const = delete;
constexpr operator signed int() const = delete;
constexpr operator unsigned int() const = delete;
constexpr operator signed long int() const = delete;
constexpr operator unsigned long int() const = delete;
constexpr operator signed long long int() const = delete;
constexpr operator unsigned long long int() const = delete;
constexpr operator float() const = delete;
constexpr operator double() const = delete;
constexpr operator long double() const = delete;
#endif
};

/// @ingroup register
Expand Down Expand Up @@ -547,10 +528,10 @@ struct Flags : public ::xpcc::FlagsOperators<Enum, T>

/// Returns `true` if bit is set
constexpr bool all(Enum const &flag) const
{ return (*this & flag); }
{ return bool(*this & flag); }
/// Returns `true` if bit is set
constexpr bool any(Enum const &flag) const
{ return (*this & flag); }
{ return bool(*this & flag); }
/// Returns `true` if bit is **not** set
constexpr bool none(Enum const &flag) const
{ return !(*this & flag); }
Expand All @@ -563,7 +544,7 @@ struct Flags : public ::xpcc::FlagsOperators<Enum, T>
{ return (*this & o) == o; }
/// Returns `true` if **any** of the passed bits are set
constexpr bool any(Flags const &o) const
{ return *this & o; }
{ return bool(*this & o); }
/// Returns `true` if **none** of the passed bits are set
constexpr bool none(Flags const &o) const
{ return (*this & o).value == 0; }
Expand Down Expand Up @@ -598,9 +579,10 @@ struct FlagsGroup<T, Ts...> : public FlagsGroup<Ts...>
// enum class
constexpr FlagsGroup(typename T::EnumType value)
: FlagsGroup<Ts...>(typename T::UnderlyingType(value)) {}
// Flags class
constexpr FlagsGroup(T value)
/// Flags operators and Flags constructor
constexpr FlagsGroup(::xpcc::FlagsOperators<typename T::EnumType, typename T::UnderlyingType> value)
: FlagsGroup<Ts...>(value.value) {}

} xpcc_packed;
/// @endcond

Expand Down Expand Up @@ -656,9 +638,10 @@ struct FlagsGroup<T> : public Register<typename T::UnderlyingType>
/// enum type constructor
constexpr FlagsGroup(typename T::EnumType value)
: Register<typename T::UnderlyingType>(typename T::UnderlyingType(value)) {}
/// Flags type constructor
constexpr FlagsGroup(T value)
/// Flags operators and Flags constructor
constexpr FlagsGroup(::xpcc::FlagsOperators<typename T::EnumType, typename T::UnderlyingType> value)
: Register<typename T::UnderlyingType>(value.value) {}

} xpcc_packed;

/**
Expand Down
5 changes: 5 additions & 0 deletions src/xpcc/architecture/interface/test/register_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ RegisterTest::testAssignments()
a0 = translateCommonArgument(c3);
TEST_ASSERT_EQUALS(a0, 0x42);

// flags operators to common struct
v0 = Test::A;
c0 = v0 | Test::C;
TEST_ASSERT_EQUALS(c0.value, 0b101);

// to register

// these are not possible!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ xpcc::stm32::Adc{{ id }}::setPrescaler(const Prescaler pre)
bool
xpcc::stm32::Adc{{ id }}::isReady()
{
return (getInterruptFlags() & InterruptFlag::Ready);
return static_cast<bool>(getInterruptFlags() & InterruptFlag::Ready);
}

void
Expand Down Expand Up @@ -162,7 +162,7 @@ xpcc::stm32::Adc{{ id }}::startConversion(void)
bool
xpcc::stm32::Adc{{ id }}::isConversionFinished(void)
{
return (getInterruptFlags() & InterruptFlag::EndOfSampling);
return static_cast<bool>(getInterruptFlags() & InterruptFlag::EndOfSampling);
}

// ----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ xpcc::stm32::SpiHal{{ id }}::setMasterSelection(MasterSelection masterSelection)
inline bool
xpcc::stm32::SpiHal{{ id }}::isReceiveRegisterNotEmpty()
{
return getInterruptFlags() & InterruptFlag::RxBufferNotEmpty;
return static_cast<bool>(getInterruptFlags() & InterruptFlag::RxBufferNotEmpty);
}

inline bool
xpcc::stm32::SpiHal{{ id }}::isTransmitRegisterEmpty()
{
return getInterruptFlags() & InterruptFlag::TxBufferEmpty;
return static_cast<bool>(getInterruptFlags() & InterruptFlag::TxBufferEmpty);
}

void inline
Expand Down
2 changes: 1 addition & 1 deletion src/xpcc/driver/temperature/tmp102_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ xpcc::Tmp102<I2cMaster>::readComparatorMode(bool &result)
if (RF_CALL( this->runTransaction() ))
{
reinterpret_cast<Config1_t&>(this->config_msb) = Config1_t(this->buffer[0]) & ~Resolution_t::mask();
result = Config2_t(this->buffer[1]) & Config2::Alert;
result = static_cast<bool>(Config2_t(this->buffer[1]) & Config2::Alert);
config_lsb = Config2_t(this->buffer[1]) & ~Config2::Alert;
RF_RETURN(true);
}
Expand Down