Types - T_Optional access
Changed so it's relatively similar to the interface of pointers.
This commit is contained in:
parent
82fc8406b3
commit
46fa84d967
5 changed files with 48 additions and 21 deletions
|
@ -42,11 +42,11 @@ template<
|
||||||
constexpr bool operator!( ) const noexcept;
|
constexpr bool operator!( ) const noexcept;
|
||||||
explicit constexpr operator Storage( ) const noexcept;
|
explicit constexpr operator Storage( ) const noexcept;
|
||||||
|
|
||||||
constexpr bool operator ==( const T_Flags other ) const noexcept;
|
constexpr bool operator ==( T_Flags other ) const noexcept;
|
||||||
constexpr bool operator !=( const T_Flags other ) const noexcept;
|
constexpr bool operator !=( T_Flags other ) const noexcept;
|
||||||
|
|
||||||
constexpr bool isSet( const T_Flags value ) const noexcept;
|
constexpr bool isSet( T_Flags value ) const noexcept;
|
||||||
constexpr bool isClear( const T_Flags value ) const noexcept;
|
constexpr bool isClear( T_Flags value ) const noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -393,13 +393,16 @@ class T_Optional
|
||||||
void clear( ) noexcept;
|
void clear( ) noexcept;
|
||||||
|
|
||||||
// Is there a value?
|
// Is there a value?
|
||||||
constexpr bool present( ) const noexcept;
|
constexpr operator bool( ) const noexcept;
|
||||||
|
constexpr bool operator!( ) const noexcept;
|
||||||
// Pointer to value or nullptr if no value
|
// Pointer to value or nullptr if no value
|
||||||
constexpr Type const* target( ) const noexcept;
|
constexpr Type const* target( ) const noexcept;
|
||||||
constexpr Type* target( ) noexcept;
|
constexpr Type* target( ) noexcept;
|
||||||
// Reference to value or std::bad_cast if no value
|
// Access value or std::bad_cast if no value
|
||||||
operator Type const&( ) const;
|
Type const& operator*( ) const;
|
||||||
operator Type& ( );
|
Type& operator*( );
|
||||||
|
Type const* operator->( ) const;
|
||||||
|
Type* operator->( );
|
||||||
};
|
};
|
||||||
|
|
||||||
template< typename Type >
|
template< typename Type >
|
||||||
|
|
|
@ -103,12 +103,12 @@ inline size_t T_SRDLocation::byte( ) const noexcept
|
||||||
|
|
||||||
inline bool T_SRDLocation::isChained( ) const noexcept
|
inline bool T_SRDLocation::isChained( ) const noexcept
|
||||||
{
|
{
|
||||||
return chaining_.present( );
|
return chaining_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T_SRDLocationChaining const& T_SRDLocation::chaining( ) const noexcept
|
inline T_SRDLocationChaining const& T_SRDLocation::chaining( ) const noexcept
|
||||||
{
|
{
|
||||||
return chaining_;
|
return *chaining_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -864,11 +864,17 @@ inline void T_Optional< T >::clear( ) noexcept
|
||||||
/*----------------------------------------------------------------------------*/
|
/*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
template< typename T >
|
template< typename T >
|
||||||
inline constexpr bool T_Optional< T >::present( ) const noexcept
|
inline constexpr T_Optional< T >::operator bool( ) const noexcept
|
||||||
{
|
{
|
||||||
return present_;
|
return present_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template< typename T >
|
||||||
|
inline constexpr bool T_Optional< T >::operator!( ) const noexcept
|
||||||
|
{
|
||||||
|
return !present_;
|
||||||
|
}
|
||||||
|
|
||||||
template< typename T >
|
template< typename T >
|
||||||
inline constexpr T const* T_Optional< T >::target( ) const noexcept
|
inline constexpr T const* T_Optional< T >::target( ) const noexcept
|
||||||
{
|
{
|
||||||
|
@ -882,7 +888,7 @@ inline constexpr T* T_Optional< T >::target( ) noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
template< typename T >
|
template< typename T >
|
||||||
inline T_Optional< T >::operator T const&( ) const
|
inline T const& T_Optional< T >::operator*( ) const
|
||||||
{
|
{
|
||||||
if ( present_ ) {
|
if ( present_ ) {
|
||||||
return *target( );
|
return *target( );
|
||||||
|
@ -891,7 +897,7 @@ inline T_Optional< T >::operator T const&( ) const
|
||||||
}
|
}
|
||||||
|
|
||||||
template< typename T >
|
template< typename T >
|
||||||
inline T_Optional< T >::operator T& ( )
|
inline T& T_Optional< T >::operator*( )
|
||||||
{
|
{
|
||||||
if ( present_ ) {
|
if ( present_ ) {
|
||||||
return *target( );
|
return *target( );
|
||||||
|
@ -899,6 +905,24 @@ inline T_Optional< T >::operator T& ( )
|
||||||
throw std::bad_cast( );
|
throw std::bad_cast( );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template< typename T >
|
||||||
|
inline T const* T_Optional< T >::operator->( ) const
|
||||||
|
{
|
||||||
|
if ( present_ ) {
|
||||||
|
return target( );
|
||||||
|
}
|
||||||
|
throw std::bad_cast( );
|
||||||
|
}
|
||||||
|
|
||||||
|
template< typename T >
|
||||||
|
inline T* T_Optional< T >::operator->( )
|
||||||
|
{
|
||||||
|
if ( present_ ) {
|
||||||
|
return target( );
|
||||||
|
}
|
||||||
|
throw std::bad_cast( );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*= T_Union ==================================================================*/
|
/*= T_Union ==================================================================*/
|
||||||
|
|
||||||
|
|
|
@ -1430,7 +1430,7 @@ T_String T_String::trim( ) const noexcept
|
||||||
while ( !it.atEnd( ) ) {
|
while ( !it.atEnd( ) ) {
|
||||||
T_Character c( it );
|
T_Character c( it );
|
||||||
if ( !c.isWhitespace( ) ) {
|
if ( !c.isWhitespace( ) ) {
|
||||||
if ( !firstNws.present( ) ) {
|
if ( !firstNws ) {
|
||||||
firstNws = it.index( );
|
firstNws = it.index( );
|
||||||
}
|
}
|
||||||
lastNws = it.index( );
|
lastNws = it.index( );
|
||||||
|
@ -1438,10 +1438,10 @@ T_String T_String::trim( ) const noexcept
|
||||||
it.next( );
|
it.next( );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !firstNws.present( ) ) {
|
if ( !firstNws ) {
|
||||||
return T_String( );
|
return T_String( );
|
||||||
}
|
}
|
||||||
return range( firstNws , lastNws );
|
return range( *firstNws , lastNws );
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------------*/
|
/*----------------------------------------------------------------------------*/
|
||||||
|
|
|
@ -92,15 +92,15 @@ struct T_Counter_
|
||||||
|
|
||||||
#define M_EMPTY_( O ) \
|
#define M_EMPTY_( O ) \
|
||||||
do { \
|
do { \
|
||||||
CPPUNIT_ASSERT( ! ( O ).present( ) ); \
|
CPPUNIT_ASSERT( ! ( O ) ); \
|
||||||
CPPUNIT_ASSERT( ( O ).target( ) == nullptr ); \
|
CPPUNIT_ASSERT( ( O ).target( ) == nullptr ); \
|
||||||
} while ( 0 )
|
} while ( 0 )
|
||||||
|
|
||||||
#define M_VALUE_( O , V ) \
|
#define M_VALUE_( O , V ) \
|
||||||
do { \
|
do { \
|
||||||
CPPUNIT_ASSERT( ( O ).present( ) ); \
|
CPPUNIT_ASSERT( ( O ) ); \
|
||||||
CPPUNIT_ASSERT( ( O ).target( ) != nullptr ); \
|
CPPUNIT_ASSERT( ( O ).target( ) != nullptr ); \
|
||||||
CPPUNIT_ASSERT_EQUAL( int( V ) , ( O ).target( )->value ); \
|
CPPUNIT_ASSERT_EQUAL( int( V ) , ( O )->value ); \
|
||||||
} while ( 0 )
|
} while ( 0 )
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -271,7 +271,7 @@ void OptionalTest::testClear( )
|
||||||
void OptionalTest::testCast( )
|
void OptionalTest::testCast( )
|
||||||
{
|
{
|
||||||
const T_Optional< T_Counter_ > s{ Construct< T_Counter_ >( ) , 12 };
|
const T_Optional< T_Counter_ > s{ Construct< T_Counter_ >( ) , 12 };
|
||||||
T_Counter_ cnt( s );
|
T_Counter_ cnt( *s );
|
||||||
CPPUNIT_ASSERT_EQUAL( 12 , cnt.value );
|
CPPUNIT_ASSERT_EQUAL( 12 , cnt.value );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,7 +279,7 @@ void OptionalTest::testCastException( )
|
||||||
{
|
{
|
||||||
const T_Optional< T_Counter_ > s;
|
const T_Optional< T_Counter_ > s;
|
||||||
try {
|
try {
|
||||||
T_Counter_ cnt( s );
|
T_Counter_ cnt( *s );
|
||||||
CPPUNIT_FAIL( "std::bad_cast not thrown" );
|
CPPUNIT_FAIL( "std::bad_cast not thrown" );
|
||||||
} catch ( std::bad_cast const& ) {
|
} catch ( std::bad_cast const& ) {
|
||||||
// OK
|
// OK
|
||||||
|
|
Loading…
Reference in a new issue