FCF_CONVERTER_REGISTRATION macro
Package: fcfBasis
File: macro.hpp
Available from version: 1.0.1
Registers a converter between two types in the global registry, enabling runtime type transformations.
Registers a converter for dynamic calls. Adds a reference to the conversion function to the
Registration is performed only if the
When you use this macro, the framework:
- Identifies the conversion path from the source type to the destination type.
- Adds the converter to the
fcf::TypeInfo::converters map of the source type. - Adds the converter to the
fcf::TypeInfo::backConverters map of the destination type. - Ensures that the conversion is available for both explicit and implicit runtime transformations.
Arguments
- The target type that the data will be converted into.
- The original type of the source data.
Example: Registering a custom converter for runtime transformation
#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 UserProfile {
int id;
std::string username;
};
// 2. Register the custom type in the fcf system
FCF_TYPE_REGISTRATION (UserProfile, "UserProfile", 100);
// 3. Implement the converter logic.
// We specialize the fcf::Converter template for the pair (UserProfile, std::string).
namespace fcf {
template <>
class Converter <3, UserProfile, std::string> {
public:
void operator()(UserProfile& a_destination, const std::string& a_source, ConvertOptions* /*a_options*/) {
// Parsing logic: "ID:Name"
size_t delimiterPos = a_source.find(':');
if (delimiterPos != std::string::npos) {
a_destination.id = std::stoi(a_source.substr(0, delimiterPos));
a_destination.username = a_source.substr(delimiterPos + 1);
} else {
throw std::runtime_error("Invalid format");
}
}
};
}
// 4. Register the converter in the global registry.
// This makes the conversion from std::string to UserProfile available at runtime.
FCF_CONVERTER_REGISTRATION (UserProfile, std::string);
int main() {
// 5. Prepare source data as a string
std::string rawData = "101:CyberHekko178";
UserProfile profile;
// 6. Perform the conversion using fcf::convert
// The framework uses the fcf::Converter type directly for conversion (UserProfile, std::string).
fcf::convert (profile, rawData);
// 7. Verify the result
std::cout << "=== Conversion Result ===" << std::endl;
std::cout << "ID: " << profile.id << std::endl;
std::cout << "Username: " << profile.username << std::endl;
// 8. Demonstrate conversion via fcf::Variant
// Runtime data type conversion is used.
fcf::Variant v = std::string("202:AdminUser");
fcf::Variant vProfile = v.cast <UserProfile>();
std::cout << "\nVariant conversion result:" << std::endl;
std::cout << "ID: " << vProfile.get <UserProfile>().id << std::endl;
std::cout << "Username: " << vProfile.get <UserProfile>().username << std::endl;
return 0;
}
Output:
=== Conversion Result ===
ID: 101
Username: CyberHekko178
Variant conversion result:
ID: 202
Username: AdminUser
VPSDime is an industry leading VPS hosting company that provides virtualized server services with high performance, availability and friendly support.