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 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 fcf::ConvertFunction. This map contains all the registered transformation logic that allows converting data FROM the current type TO various other types.
Result
const fcf::TypeInfo::ConvertersType&
- 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<CL_DIRECT, std::string, MyCustomStruct> {
public:
void operator()(std::string& a_destination, const MyCustomStruct& a_source, ConvertOptions& /*a_options*/, Error& /*a_error*/) {
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. Retrieve the map of converters
const auto& convertersMap = fcf::Type<MyCustomStruct>().converters();
std::cout << "=== Available Conversions ===" << std::endl;
std::cout << "Number of registered converters: " << convertersMap.size() << std::endl;
// 7. Check if conversion to std::string is available
fcf::TypeIndex 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;
fcf::Error error(true);
fcf::ConvertOptions co;
convFunc(&result, &obj, co, error);
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