Type:
template <ConverterLevel Mode, typename TDestination, typename TSource, typename TSpecifier = DefaultConvertMode>
class fcf::Converter
Package: fcfBasis
File:
bits/PartConvert/Converter.hpp
( link_to_the_text_position_in_the_file )
Available from version: 1.0.1
A template-based conversion engine providing multiple strategies for transforming data between different types at compile-time.
The fcf::Converter class is a highly optimized, template-driven engine designed to handle type conversions using various strategies defined by the fcf::ConverterLevel.
By selecting a specific mode, developers can choose between high-performance direct casting, safe string parsing, or generalized conversion logic.
Available fcf::ConverterLevel modes:
- fcf::CL_ALL: Performs a standard C++ assignment or cast. Useful for types with built-in conversion operators.
- fcf::CL_DIRECT_SOURCE: Specializes in converting from a source type to a destination (e.g., parsing a string into a numeric type).
- fcf::CL_DIRECT_DESTINATION: Specializes in converting from a source to a destination (e.g., converting a number to a string).
- fcf::CL_DIRECT: Provides highly optimized, specialized conversion paths for common primitive types, ensuring maximum performance and error handling.
Template arguments
Mode
- The conversion strategy to be used (CL_ALL, CL_DIRECT, etc.)
TDestination
- The target type to convert to
TSource
- The source type to convert from
TSpecifier
= fcf::DefaultConvertMode
- Customization specifier for the conversion process
Example: Exploring Conversion Strategies
This example demonstrates how to use different fcf::ConverterLevel modes to perform various type conversions, from simple casting to specialized string parsing.
#define FCF_TEST_IMPLEMENTATION
#include <fcfBasis/bits/PartConvert/PartConvert.hpp>
#include <fcfTest/test.hpp>
#include <string>
FCF_TEST_DEFINE("ConverterDemo", "Conversion", "Strategies") {
fcf::Error error(false);
fcf::ConvertOptions options;
// // 1. CL_DIRECT: Specialized string to integer conversion
// std::string strInt = "123";
// int intVal = 0;
// fcf::Converter<fcf::CL_DIRECT, int, std::string>::operator()(intVal, strInt, &options, error);
//
// // 2. CL_DIRECT_DESTINATION: Specialized integer to string conversion
// int intVal2 = 456;
// std::string strVal2;
// fcf::Converter<fcf::CL_DIRECT_DESTINATION, std::string, int>::operator()(strVal2, intVal2, &options, error);
//
// // 3. CL_ALL: Simple casting/assignment (double to int)
// double dblVal = 3.14;
// int castedInt = 0;
// fcf::Converter<fcf::CL_ALL, int, double>::operator()(castedInt, dblVal, &options, error);
//
// // Output results using fcf::NTest::log
// fcf::NTest::log() << "Direct (string -> int): " << intVal << std::endl;
// fcf::NTest::log() << "DirectDest (int -> string): " << strVal2 << std::endl;
// fcf::NTest::log() << "All (double -> int): " << castedInt << std::endl;
//
// // Verification
// FCF_TEST(intVal == 123);
// FCF_TEST(strVal2 == "456");
// FCF_TEST(castedInt == 3);
}
int main(int argc, char** argv) {
bool error = false;
fcf::NTest::cmdRun(argc, argv, fcf::NTest::CRM_RUN, &error);
return error ? 1 : 0;
}
Output:
Performing the test: "ConverterDemo" -> "Conversion" -> "Strategies" ...
[SUCCESS] Test completed successfully (0.000`000`547 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`000`547 sec