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

FCF_CONVERTER_REGISTRATION_FORCE macro

FCF_CONVERTER_REGISTRATION_FORCE (TYPE a_destinationType, TYPE a_sourceType)

Package: fcfBasis

File: macro.hpp

Available from version: 1.0.1

Forces registration of a converter between two types in the global registry.

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).

Unlike FCF_CONVERTER_REGISTRATION, registration is always performed, regardless of FCF_BASIS_IMPLEMENTATION

This macro is used to explicitly register a conversion function between a source type and a target type. It is particularly useful when you need to ensure that the converter is created and added to the global registry during application initialization, regardless of whether the FCF_BASIS_IMPLEMENTATION macro is declared.

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 a user profile

#include <iostream> #include <string> #include <fcfBasis/basis.hpp> // 1. Define a custom structure for demonstration struct UserProfile { int id; std::string username; }; // 2. Register the type in the fcf system. // This is required so that the framework can handle this type during conversions. FCF_TYPE_REGISTRATION(UserProfile, "UserProfile", 0); // 3. Implement a custom converter from std::string to UserProfile. // The converter logic will be used when fcf::convert is called with these types. namespace fcf { template <> class Converter<3, UserProfile, std::string> { public: void operator()(UserProfile& a_destination, const std::string& a_source, ConvertOptions* /*a_options*/) { 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("Format error"); } } }; } // 4. Force the registration of the converter in the global registry. // This macro ensures that the converter is visible to the framework's runtime conversion engine. FCF_CONVERTER_REGISTRATION_FORCE(UserProfile, std::string); int main() { // 5. Prepare source data fcf::Variant rawData = "101:CyberHekko178"; UserProfile profile; // 6. Perform the conversion using fcf::Variant // The framework will find the registered converter for (UserProfile, std::string) profile = rawData.cast<UserProfile>(); // 7. Verify the result std::cout << "=== Conversion Result ===" << std::endl; std::cout << "ID: " << profile.id << std::endl; std::cout << "Username: " << profile.username << std::endl; return 0; }

impl.cpp file:

// Define an implementation macro to include the implementation section in header files. #define FCF_BASIS_IMPLEMENTATION #include <fcfBasis/basis.hpp>

CMakeLists.txt file:

add_executable(app main.cpp impl.cpp) target_include_directories(app PRIVATE ../../..)

Output:

=== Conversion Result === ID: 101 Username: CyberHekko178