ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
FIsLess Struct Reference

Description:


This function provides a relational comparison of two boxes.

A default implementation is registered that compares the types. If they are equal, the first uinteger values in the placeholders are compared. Specifics for array types are not implemented with that default version.

If the types are not the same, the result of the comparison of the run-time type information object is returned. For this, method Box::TypeID is invoked on both boxes and to allow operator< on those, std::type_index is applied. This leads to a "nested" sort order, with the type information being the outer order and the boxed data being the inner.
To keep this overall order intact, type-specific implementations should use the following implementation scheme:

 if( rhs.IsType<AComparableType1>() )
     return MyCompare( self.Unbox<MyType>, rhs.Unbox<AComparableType1>();

 if( rhs.IsType<AComparableType2>() )
     return MyCompare( self.Unbox<MyType>, rhs.Unbox<AComparableType2>();

 if( ...
     return ...

 return std::type_index( self.TypeID() ) < std::type_index( rhs.TypeID() );

With this scheme in place, for example std::sort will work properly on containers of boxes of mixed types. The following sample demonstrates this. It uses a specialization of std::less<T> for type Box. This is found in a compatibility header:

With that, the following code compiles fine:

std::vector<alib::Box> myVec= { 2, A_CHAR('b'), 3.0, "BBB", A_WCHAR('a'), -6, 1.0,
"AAA", A_WCHAR('d'), 4, "CCC", A_CHAR('c'),5.0, 0 };
std::sort( myVec.begin(), myVec.end(), std::less<Box>() );
for( auto& box : myVec )
cout << box << endl;

It generates the following output:

AAA
BBB
CCC
-6
0
1.0
2
3.0
4
5.0
a
b
c
d

As can be seen from the result, a proper "outer" sorting is in place. Nevertheless, floating-point and integral values follow the inner sorting. This is because specific implementations of the functions are registered for all arithmetic types, which allow comparisons of mixed types. The same is true for the different C++ character types.

Note
It is a matter of the compiler (and can not be determined by the user code) how the types are sorted (outer sorting).
Furthermore, the default implementation that simply compares the first uinteger of the placeholder is unlikely to produce "reasonable" results.

Type-specific implementations are given and registered for fundamental types. Integrals of different sizes and floating point values will be compared by using Box::UnboxSignedIntegral , Box::UnboxUnsignedIntegral and Box::UnboxFloatingPoint and appropriate casting.

If module ALib Strings is included in the distribution, an implementation for arrays of nchar , wchar and xchar is given.

For custom types, with ComparableTypes, a templated implementation is suggested: Rather than implementing a specific box-function, the custom type should implement operator< and register an instantiation of the templated function.

See also
Method Box::operator< , which calls this function.

Definition at line 246 of file functions.inl.

Public Type Index:

using Signature = bool (*) ( const Box& self, const Box& rhs)
 

Public Static Method Index:

template<typename TComparable >
static bool ComparableTypes (const Box &self, const Box &rhs)
 

Type Definition Details:

◆ Signature

using Signature = bool (*) ( const Box& self, const Box& rhs)

Signature of the invokable function.

Parameters
selfThe box that the function was invoked on.
rhsThe box to compare.
Returns
Return value and type is implementation specific.

Definition at line 255 of file functions.inl.

Method Details:

◆ ComparableTypes()

template<typename TComparable >
static bool ComparableTypes ( const Box & self,
const Box & rhs )
static

Templated implementation for function FIsLess , usable with boxable types which have operator< implemented.

Note
This method interally is provide twice, once for types boxed as pointers and one for types boxed as values, and selected using TMP.
If a type is boxed as pointer, then TComparable has to be given as such pointer type. For comparison, the unboxed pointers will be dereferenced. If both are nulled, false is returned, if only self is nulled, true and if only rhs is nulled, then false.
See also
Macro ALIB_BOXING_DEFINE_FISLESS_FOR_COMPARABLE_TYPE.
Template Parameters
TComparableThe mapped type that can be compared using operator<.
Parameters
selfThe box that the function was invoked on.
rhsThe boxed value to compare.
Returns
true if self equals rhs , false otherwise.

The documentation for this struct was generated from the following file: