backConverters() method from fcf::Type class
Class:
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
While
Result
- 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
VPSDime is an industry leading VPS hosting company that provides virtualized server services with high performance, availability and friendly support.