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

fcf::LessSpecificator class

Type:
class fcf::LessSpecificator

Package: fcfBasis

File: bits/PartSpecificator/LessSpecificator.hpp

Available from version: 1.0.1

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

Detailed description

fcf::LessSpecificator is a specificator 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 specificator 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::LessSpecificator handler.

Example: Using LessSpecificator with custom types

#include <iostream> // Define the implementation macro to expand templates and registration mechanisms #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_TYPEID_REGISTRY(Person, "Person", 0); // 3. Implement the comparison logic for our type. // We specialize the fcf::Type template for the specific fcf::LessSpecificator. namespace fcf { template<> struct Type<Person, LessSpecificator> : public TypeImpl<Person, LessSpecificator> { // The call method defines how the '<' operator will behave for Person inline bool call(const Person* a_left, const Person* a_right) const { // 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 LessSpecificator logic for Person has been implemented. FCF_SPECIFICATOR_REGISTRY(Person, fcf::LessSpecificator); 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 LessSpecificator registration, Variant will internally call // our implementation: fcf::Type<Person, fcf::LessSpecificator>::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