converters() method from fcf::Type class
Class:
Package: fcfBasis
File: bits/PartType/TypeDefinition.hpp
Available from version: 1.0.1
Retrieves the map of registered converters for the given type.
The method returns a constant reference to a map where the key is the destination type index and the value is a
Result
- A constant reference to the map of converters from this type to other types.
Example: Inspecting available 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 MyCustomStruct to std::string
namespace fcf {
template <>
class Converter<3, std::string, MyCustomStruct> {
public:
void operator()(std::string& a_destination, const MyCustomStruct& a_source, ConvertOptions* /*a_options*/) {
a_destination = "ID: " + std::to_string(a_source.id) + ", Name: " + a_source.name;
}
};
}
// 4. Register the converter in the system
FCF_CONVERTER_REGISTRATION (std::string, MyCustomStruct);
int main() {
// 5. Create an instance of the custom type
MyCustomStruct obj{123, "Alice"};
// 6. Access the type descriptor
fcf::Type <MyCustomStruct> typeDescriptor;
// 7. Retrieve the map of converters
const auto& convertersMap = typeDescriptor.converters () ;
std::cout << "=== Available Conversions ===" << std::endl;
std::cout << "Number of registered converters: " << convertersMap.size() << std::endl;
// 8. Check if conversion to std::string is available
unsigned int stringTypeIndex = fcf::Type <std::string >().index();
if (convertersMap.count(stringTypeIndex)) {
std::cout << "[OK] Conversion to std::string is supported." << std::endl;
// Demonstrate the conversion using the retrieved function
fcf::ConvertFunction convFunc = convertersMap.at(stringTypeIndex);
std::string result;
convFunc(&result, &obj, nullptr);
std::cout << "Converted value: " << result << std::endl;
} else {
std::cout << "[ERROR] Conversion to std::string is NOT supported." << std::endl;
}
return 0;
}
Output:
=== Available Conversions ===
Number of registered converters: 1
[OK] Conversion to std::string is supported.
Converted value: ID: 123, Name: Alice
VPSDime is an industry leading VPS hosting company that provides virtualized server services with high performance, availability and friendly support.