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

backConverters() method from fcf::Type class

const fcf::TypeInfo::ConvertersType& backConverters()

Class: fcf::Type

Package: fcfBasis

File: bits/PartType/TypeDefinition.hpp

Available from version: 1.0.1

Retrieves the map of registered back-converters for the given type.

The method returns a constant reference to a map where the key is the source type index and the value is a fcf::ConvertFunction. This map contains all the registered transformation logic that allows converting data FROM various other types TO the current type.

While fcf::Type::converters describes how to transform the current type into something else, fcf::Type::backConverters describes how other types can be transformed into the current one.

Result
const fcf::TypeInfo::ConvertersType&
- A constant reference to the map of back-converters into this type.

Example: Inspecting incoming conversions

#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 a converter FROM std::string TO MyCustomStruct namespace fcf { template <> class Converter<3, MyCustomStruct, std::string> { public: void operator()(MyCustomStruct& a_destination, const std::string& a_source, ConvertOptions* /*a_options*/) { // Simple parsing logic for demonstration size_t delimiterPos = a_source.find(':'); if (delimiterPos != std::string::npos) { a_destination.id = std::stoi(a_source.substr(0, delimiterPos)); a_destination.name = a_source.substr(delimiterPos + 1); } } }; } // 4. Register the converter in the system FCF_CONVERTER_REGISTRATION(MyCustomStruct, std::string); int main() { // 5. Create an instance of the custom type MyCustomStruct obj{0, ""}; // 6. Access the type descriptor fcf::Type<MyCustomStruct> typeDescriptor; // 7. Retrieve the map of back-converters // This map tells us which types can be converted INTO MyCustomStruct const auto& backConvertersMap = typeDescriptor.backConverters(); std::cout << "=== Incoming Conversions ===" << std::endl; std::cout << "Number of registered back-converters: " << backConvertersMap.size() << std::endl; // 8. Check if conversion FROM std::string is supported unsigned int stringTypeIndex = fcf::Type<std::string>().index(); if (backConvertersMap.count(stringTypeIndex)) { std::cout << "[OK] Conversion FROM std::string TO MyCustomStruct is supported." << std::endl; // Demonstrate the conversion using the retrieved function fcf::ConvertFunction convFunc = backConvertersMap.at(stringTypeIndex); std::string sourceData = "42:Alice"; convFunc(&obj, &sourceData, nullptr); std::cout << "Converted object -> ID: " << obj.id << ", Name: " << obj.name << std::endl; } else { std::cout << "[ERROR] Conversion FROM std::string is NOT supported." << std::endl; } return 0; }

Output:

=== Incoming Conversions === Number of registered back-converters: 1 [OK] Conversion FROM std::string TO MyCustomStruct is supported. Converted object -> ID: 42, Name: Alice