diff --git a/include/ebcl/Types.hh b/include/ebcl/Types.hh index 5781117..afd828f 100644 --- a/include/ebcl/Types.hh +++ b/include/ebcl/Types.hh @@ -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 > diff --git a/include/ebcl/inline/SRDData.hh b/include/ebcl/inline/SRDData.hh index b5eadab..cc384b8 100644 --- a/include/ebcl/inline/SRDData.hh +++ b/include/ebcl/inline/SRDData.hh @@ -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_; } diff --git a/include/ebcl/inline/Types.hh b/include/ebcl/inline/Types.hh index d9375b7..d3717bc 100644 --- a/include/ebcl/inline/Types.hh +++ b/include/ebcl/inline/Types.hh @@ -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 ==================================================================*/ diff --git a/src/Strings.cc b/src/Strings.cc index def171e..f18c998 100644 --- a/src/Strings.cc +++ b/src/Strings.cc @@ -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 ); } /*----------------------------------------------------------------------------*/ diff --git a/tests/optional.cc b/tests/optional.cc index 07f1e2d..e9d66e7 100644 --- a/tests/optional.cc +++ b/tests/optional.cc @@ -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