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

fcf::convertFromRuntime() function

template <typename TDestination>
void fcf::convertFromRuntime(TDestination* a_destinationData, const void* a_source, fcf::TypeIndex a_sourceTypeIndex, fcf::ConvertOptions& a_options, fcf::Error& a_error)
template <typename TDestination>
void fcf::convertFromRuntime(TDestination* a_destinationData, const void* a_source, fcf::TypeIndex a_sourceTypeIndex, fcf::ConvertOptions& a_options, fcf::Error&& a_error = fcf::Error{true})
template <typename TDestination>
void fcf::convertFromRuntime(TDestination* a_destinationData, const void* a_source, fcf::TypeIndex a_sourceTypeIndex, fcf::Error& a_error)
template <typename TDestination>
void fcf::convertFromRuntime(TDestination* a_destinationData, const void* a_source, fcf::TypeIndex a_sourceTypeIndex, fcf::Error&& a_error = fcf::Error{true})

Package: fcfBasis

File: bits/PartConvert/convertRuntime.hpp ( link_to_the_declaration_in_the_file )

Available from version: 1.0.1

Performs type conversion at runtime using type indices, populating a destination object from a raw source pointer.

The convertFromRuntime function is designed for scenarios where the source type is only known at runtime via a fcf::TypeIndex. It allows for dynamic type transformation without knowing the source type at compile time.

The function performs the following operations:

  • Type Lookup: It retrieves the appropriate conversion function using the provided a_sourceTypeIndex and the target TDestination type.
  • Pointer-based Access: It works with raw pointers, making it suitable for low-level data processing or when dealing with generic buffers.
  • Error Handling: It supports both explicit fcf::Error reference passing and rvalue (move) error passing for flexible error management.

Template arguments
TDestination - The type of the destination object to be populated.
Arguments

TDestination* a_destinationData
- Pointer to the memory of the destination object.

const void* a_source
- Pointer to the source data.

fcf::TypeIndex a_sourceTypeIndex
- The runtime type index of the source data.

fcf::ConvertOptions& a_options
- Optional configuration for the conversion process.

fcf::Error& a_error
- Object used to report conversion errors.

Example: Performing runtime conversion from int to string

An example showing how to convert an integer to a string when the source type is only known at runtime via its fcf::TypeIndex.

#define FCF_BASIS_IMPLEMENTATION #include <fcfBasis/basis.hpp> #include <iostream> #include <string> int main() { // 1. Source data of type int int sourceValue = 12345; // 2. Destination object to be populated std::string destination; // 3. Obtain the fcf::TypeIndex for the source type at runtime fcf::TypeIndex sourceIndex = fcf::Type<int>().index(); // 4. Error object for reporting status fcf::Error error; // 5. Execute runtime conversion // We pass the address of the destination and the address of the source fcf::convertFromRuntime(&destination, &sourceValue, sourceIndex, error); if (!error) { // Conversion successful std::cout << "Success! Converted value: " << destination << std::endl; } else { // Conversion failed std::cout << "Error during conversion: " << error.error << std::endl; } return 0; }

Output:

Success! Converted value: 12345

The convertFromRuntime function is essential for building generic systems that handle data streams where types are determined dynamically.