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

FCF_CONVERTER_REGISTRATION macro

FCF_CONVERTER_REGISTRATION (TYPE a_destinationType, TYPE a_sourceType)

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 fcf::TypeInfo::converters and fcf::TypeInfo::backConverters properties. This is necessary for runtime type conversions (for example, in fcf::Variant).

Registration is performed only if the FCF_BASIS_IMPLEMENTATION macro is declared.

When you use this macro, the framework:

  1. Identifies the conversion path from the source type to the destination type.
  2. Adds the converter to the fcf::TypeInfo::converters map of the source type.
  3. Adds the converter to the fcf::TypeInfo::backConverters map of the destination type.
  4. Ensures that the conversion is available for both explicit and implicit runtime transformations.

Arguments

TYPE a_destinationType
- The target type that the data will be converted into.

TYPE a_sourceType
- 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