FCF 2.0 development in progress...
> > > >
[News] [C++ Libraries API] [C++ Downloads] [Donate to the project] [Contacts]

fcf::LessSpecifier class

Type:
class fcf::LessSpecifier

Package: fcfBasis

File: bits/PartSpecifier/LessSpecifier.hpp

Available from version: 1.0.1

A specifier used to perform 'less than' (<) comparisons between objects.

fcf::LessSpecifier is a specifier that defines the logic for the less-than operator. It allows the framework to perform runtime comparisons between objects of any registered type, provided that a specialization for this specifier has been implemented and registered for that type.

When used with fcf::Variant, it enables the use of the fcf::Variant::operator< operator for dynamic comparison.

Typedef
typedef CallType - Function type for the fcf::LessSpecifier handler.

Example: Using LessSpecifier with custom types

#include <iostream> // Define an implementation macro to include the implementation section in header files #define FCF_BASIS_IMPLEMENTATION #include <fcfBasis/basis.hpp> // 1. Create a custom type that we want to enable for comparison struct Person { int age; std::string name; }; // 2. Register the type in the fcf system. // This is required so that fcf::Type<Person> can access its metadata. FCF_TYPE_REGISTRATION(Person, "Person", 0); // 3. Implement the comparison logic for our type. // We specialize the fcf::Type template for the specific fcf::LessSpecifier. namespace fcf { template<> struct Type<Person, LessSpecifier> : public TypeImpl<Person, LessSpecifier> { // The call method defines how the '<' operator will behave for Person inline bool operator()(const Person* a_left, const Person* a_right) { // In this example, we compare people by age return a_left->age < a_right->age; } }; } // 4. Bind the specifier implementation to our type. // Without this macro, the system will not know that the LessSpecifier logic for Person has been implemented. FCF_SPECIFIER_REGISTRATION(Person, fcf::LessSpecifier); int main() { // Create two objects of our type Person p1{25, "Alice"}; Person p2{30, "Bob"}; // 5. Wrap the objects in fcf::Variant. // Variant allows performing operations (like comparison) at runtime, // using the registered specifiers. fcf::Variant v1(p1); fcf::Variant v2(p2); // 6. Use the standard '<' operator. // Thanks to the LessSpecifier registration, Variant will internally call // our implementation: fcf::Type<Person, fcf::LessSpecifier>::call. if (v1 < v2) { std::cout << "Alice is younger than Bob" << std::endl; } else { std::cout << "Alice is not younger than Bob" << std::endl; } return 0; }

Output:

Alice is younger than Bob