diff --git a/alib2std/src/extensions/functional.hpp b/alib2std/src/extensions/functional.hpp index 195719b7d3b18a4c3dca55de1c7ec124bc8b4338..992f227f80d5168b91186a1f6010371e03bdc2f4 100644 --- a/alib2std/src/extensions/functional.hpp +++ b/alib2std/src/extensions/functional.hpp @@ -26,8 +26,36 @@ #include <functional> +#include "type_traits.hpp" +#include "typeindex.h" + namespace ext { +template < typename _Tp = void > +struct less; + +template < typename _Tp > +struct less : public std::binary_function < _Tp, _Tp, bool > { + bool operator ( ) ( const _Tp & __x, const _Tp & __y ) const { + return __x < __y; + } +}; + +template < > +struct less < void > { + template < typename _Tp, typename _Up, typename std::enable_if < supports < std::less < > ( _Tp, _Up ) >::value >::type * = nullptr > + auto operator ( ) ( _Tp && __t, _Up && __u ) const noexcept ( noexcept ( std::forward < _Tp > ( __t ) < std::forward < _Up > ( __u ) ) ) -> decltype ( std::forward < _Tp > ( __t ) < std::forward < _Up > ( __u ) ) { + return std::forward < _Tp > ( __t ) < std::forward < _Up > ( __u ); + } + + template < typename _Tp, typename _Up, typename std::enable_if < ! supports < std::less < > ( _Tp, _Up ) >::value >::type * = nullptr > + bool operator ( ) ( _Tp &&, _Up && ) const noexcept { + return ext::type_index ( typeid ( _Tp ) ) < ext::type_index ( typeid ( _Up ) ); + } + + typedef std::__is_transparent is_transparent; +}; + /** * \brief * Class extending the reference wrapper class from the standard library. Original reason is to allow its use with standard stream aggregation class. diff --git a/alib2std/test-src/extensions/FunctionalTest.cpp b/alib2std/test-src/extensions/FunctionalTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abcdd76ed380183050356b682de458ecd15f36cc --- /dev/null +++ b/alib2std/test-src/extensions/FunctionalTest.cpp @@ -0,0 +1,18 @@ +#include "FunctionalTest.h" +#include <alib/functional> + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FunctionalTest, "bits" ); +CPPUNIT_TEST_SUITE_REGISTRATION( FunctionalTest ); + +void FunctionalTest::setUp() { +} + +void FunctionalTest::tearDown() { +} + +void FunctionalTest::testLess() { + ext::less < > test; + CPPUNIT_ASSERT ( test ( 1, 2 ) == true ); + CPPUNIT_ASSERT ( test ( 1, 2. ) == true ); + CPPUNIT_EXCLUSIVE_OR( test ( std::string ( "aa" ), 1 ), test ( 1, std::string ( "aa" ) ) ); +} diff --git a/alib2std/test-src/extensions/FunctionalTest.h b/alib2std/test-src/extensions/FunctionalTest.h new file mode 100644 index 0000000000000000000000000000000000000000..8f859a7e490bdf44d8e8091a6f40da1268c9dfa2 --- /dev/null +++ b/alib2std/test-src/extensions/FunctionalTest.h @@ -0,0 +1,19 @@ +#ifndef FUNCTIONAL_TEST_H_ +#define FUNCTIONAL_TEST_H_ + +#include <cppunit/extensions/HelperMacros.h> + +class FunctionalTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE( FunctionalTest ); + CPPUNIT_TEST( testLess ); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp(); + void tearDown(); + + void testLess(); +}; + +#endif // FUNCTIONAL_TEST_H_