Class: fcf::Type
Package: fcfBasis
File:
bits/PartType/TypeDecl.hpp
(
link_to_the_declaration_in_the_file
)
Implementation file:
bits/PartType/Type.hpp
(
link_to_the_implementation_in_the_file
)
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<CL_DIRECT, MyCustomStruct, std::string> {
public:
void operator()(MyCustomStruct& a_destination, const std::string& a_source, ConvertOptions& /*a_options*/, Error& /*a_error*/) {
// 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
fcf::TypeIndex 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";
fcf::Error error(true);
fcf::ConvertOptions co;
convFunc(&obj, &sourceData, co, error);
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