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:
It generates the following output:
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.
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.
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) |
Signature of the invokable function.
self | The box that the function was invoked on. |
rhs | The box to compare. |
Definition at line 255 of file functions.inl.
|
static |
Templated implementation for function FIsLess , usable with boxable types which have operator<
implemented.
false
is returned, if only self is nulled, true
and if only rhs is nulled, then false
.TComparable | The mapped type that can be compared using operator< . |
self | The box that the function was invoked on. |
rhs | The boxed value to compare. |
true
if self equals rhs , false
otherwise.