Class: fcf::Type
Package: fcfBasis
File: bits/PartType/TypeDefinition.hpp
Available from version: 1.0.1
Retrieves a universal function pointer for a specifier, enabling dynamic runtime dispatch.
The method returns a fcf::UniversalCall function pointer. This pointer is designed to be used with the framework's dynamic calling mechanism, allowing you to invoke a specific operation (like comparison, addition, etc.) on a type without knowing its exact compile-time type.
If no specifier handler function is defined for the specified specifier, calling the fcf::Type::specifierUniversalCall method without parameters will throw a fcf::SpecifierNotFoundException exception.
If the function instance is called with the a_dstError parameter accepting a pointer to an error object, then no exception will be thrown and the function will return 0 if there is no handler specifier.
Template arguments
TSpecifier
- The type of the specifier to retrieve (e.g., fcf::LessSpecifier, fcf::AddSpecifier).
Arguments
fcf::Exception* a_dstError
- The argument is a pointer to the object of fcf::Exception, whose fields will be filled in case of error. This argument may be zero.
Result
fcf::UniversalCall
- A universal function pointer for a specifier. If called with the a_dstError argument, the function returns 0 on error.
Thrown exceptions
fcf::SpecifierNotFoundException
- Thrown if the specified TSpecifier has not been registered for this type via FCF_SPECIFIER_REGISTRATION.
Example: Dynamic dispatch using UniversalCall
#include <iostream>
#include <string>
// Define an implementation macro to include the implementation section in header files
#define FCF_BASIS_IMPLEMENTATION
#include <fcfBasis/basis.hpp>
// 1. Define a custom structure for demonstration
struct MyCustomStruct {
int id;
std::string name;
};
// 2. Register the type in the fcf system
FCF_TYPE_REGISTRATION(MyCustomStruct, "MyCustomStruct", 0);
// 3. Implement the comparison logic (LessSpecifier)
namespace fcf {
template<> struct Type<MyCustomStruct, LessSpecifier> : public TypeImpl<MyCustomStruct, LessSpecifier> {
inline bool operator()(const MyCustomStruct* a_left, const MyCustomStruct* a_right) {
return a_left->id < a_right->id;
}
};
}
// 4. Bind the specifier to the type
FCF_SPECIFIER_REGISTRATION(MyCustomStruct, fcf::LessSpecifier);
int main() {
// 5. Create instances of the type
MyCustomStruct obj1{10, "Alice"};
MyCustomStruct obj2{20, "Bob"};
// 6. Get the universal call for LessSpecifier
// This allows us to perform comparison without knowing the type at compile time
fcf::UniversalCall lessCall = fcf::Type<MyCustomStruct>().specifierUniversalCall<fcf::LessSpecifier>();
if (lessCall) {
// 7. Execute the call dynamically
// The first argument is the object to act upon, the second is an array of arguments (in this case, one more object), and the third is the count.
// We wrap the second object in a fcf::Variant to satisfy the UniversalCall interface.
fcf::Variant arg2(obj2, fcf::Variant::REFERENCE);
fcf::Variant result = lessCall(&obj1, &arg2, 1);
if (result.cast<bool>()) {
std::cout << "Dynamic comparison result: obj1 < obj2 is TRUE" << std::endl;
} else {
std::cout << "Dynamic comparison result: obj1 < obj2 is FALSE" << std::endl;
}
} else {
std::cout << "Specifier not found!" << std::endl;
}
return 0;
}
Output:
Dynamic comparison result: obj1 < obj2 is TRUE