Types - T_Optional access

Changed so it's relatively similar to the interface of pointers.
This commit is contained in:
Emmanuel BENOîT 2017-11-07 17:40:02 +01:00
parent 82fc8406b3
commit 46fa84d967
5 changed files with 48 additions and 21 deletions

View file

@ -42,11 +42,11 @@ template<
constexpr bool operator!( ) const noexcept;
explicit constexpr operator Storage( ) const noexcept;
constexpr bool operator ==( const T_Flags other ) const noexcept;
constexpr bool operator !=( const T_Flags other ) const noexcept;
constexpr bool operator ==( T_Flags other ) const noexcept;
constexpr bool operator !=( T_Flags other ) const noexcept;
constexpr bool isSet( const T_Flags value ) const noexcept;
constexpr bool isClear( const T_Flags value ) const noexcept;
constexpr bool isSet( T_Flags value ) const noexcept;
constexpr bool isClear( T_Flags value ) const noexcept;
};
@ -393,13 +393,16 @@ class T_Optional
void clear( ) noexcept;
// 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
constexpr Type const* target( ) const noexcept;
constexpr Type* target( ) noexcept;
// Reference to value or std::bad_cast if no value
operator Type const&( ) const;
operator Type& ( );
// Access value or std::bad_cast if no value
Type const& operator*( ) const;
Type& operator*( );
Type const* operator->( ) const;
Type* operator->( );
};
template< typename Type >

View file

@ -103,12 +103,12 @@ inline size_t T_SRDLocation::byte( ) const noexcept
inline bool T_SRDLocation::isChained( ) const noexcept
{
return chaining_.present( );
return chaining_;
}
inline T_SRDLocationChaining const& T_SRDLocation::chaining( ) const noexcept
{
return chaining_;
return *chaining_;
}

View file

@ -864,11 +864,17 @@ inline void T_Optional< T >::clear( ) noexcept
/*----------------------------------------------------------------------------*/
template< typename T >
inline constexpr bool T_Optional< T >::present( ) const noexcept
inline constexpr T_Optional< T >::operator bool( ) const noexcept
{
return present_;
}
template< typename T >
inline constexpr bool T_Optional< T >::operator!( ) const noexcept
{
return !present_;
}
template< typename T >
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 >
inline T_Optional< T >::operator T const&( ) const
inline T const& T_Optional< T >::operator*( ) const
{
if ( present_ ) {
return *target( );
@ -891,7 +897,7 @@ inline T_Optional< T >::operator T const&( ) const
}
template< typename T >
inline T_Optional< T >::operator T& ( )
inline T& T_Optional< T >::operator*( )
{
if ( present_ ) {
return *target( );
@ -899,6 +905,24 @@ inline T_Optional< T >::operator T& ( )
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 ==================================================================*/

View file

@ -1430,7 +1430,7 @@ T_String T_String::trim( ) const noexcept
while ( !it.atEnd( ) ) {
T_Character c( it );
if ( !c.isWhitespace( ) ) {
if ( !firstNws.present( ) ) {
if ( !firstNws ) {
firstNws = it.index( );
}
lastNws = it.index( );
@ -1438,10 +1438,10 @@ T_String T_String::trim( ) const noexcept
it.next( );
}
if ( !firstNws.present( ) ) {
if ( !firstNws ) {
return T_String( );
}
return range( firstNws , lastNws );
return range( *firstNws , lastNws );
}
/*----------------------------------------------------------------------------*/

View file

@ -92,15 +92,15 @@ struct T_Counter_
#define M_EMPTY_( O ) \
do { \
CPPUNIT_ASSERT( ! ( O ).present( ) ); \
CPPUNIT_ASSERT( ! ( O ) ); \
CPPUNIT_ASSERT( ( O ).target( ) == nullptr ); \
} while ( 0 )
#define M_VALUE_( O , V ) \
do { \
CPPUNIT_ASSERT( ( O ).present( ) ); \
CPPUNIT_ASSERT( ( O ) ); \
CPPUNIT_ASSERT( ( O ).target( ) != nullptr ); \
CPPUNIT_ASSERT_EQUAL( int( V ) , ( O ).target( )->value ); \
CPPUNIT_ASSERT_EQUAL( int( V ) , ( O )->value ); \
} while ( 0 )
} // namespace
@ -271,7 +271,7 @@ void OptionalTest::testClear( )
void OptionalTest::testCast( )
{
const T_Optional< T_Counter_ > s{ Construct< T_Counter_ >( ) , 12 };
T_Counter_ cnt( s );
T_Counter_ cnt( *s );
CPPUNIT_ASSERT_EQUAL( 12 , cnt.value );
}
@ -279,7 +279,7 @@ void OptionalTest::testCastException( )
{
const T_Optional< T_Counter_ > s;
try {
T_Counter_ cnt( s );
T_Counter_ cnt( *s );
CPPUNIT_FAIL( "std::bad_cast not thrown" );
} catch ( std::bad_cast const& ) {
// OK