fcf::EConverterLevel enum
Type: fcf::EConverterLevel
Package: fcfBasis
File: bits/PartConvert/EConverterLevel.hpp
Available from version: 1.0.1
Defines the priority and specificity of the conversion strategies used by the conversion engine.
The EConverterLevel enumeration specifies the depth and specialization level of the conversion strategy used by the Converter class. It allows the framework to select an available implementation or allows users to provide their own specializations.
The levels are organized by specificity. A distinction is made between levels with and without the _LOW suffix:
- Levels with _LOW: These refer to the default converters provided by the framework.
- Levels without _LOW: These are used to override the framework's default converters with custom specialized logic. These levels are functionally analogous to their _LOW counterparts.
The hierarchy of specificity (from most general to most specific) is:
- CL_ALL_LOW / CL_ALL: General conversion level.
- CL_DIRECT_SOURCE_LOW / CL_DIRECT_SOURCE: Specialization focusing on the source type.
- CL_DIRECT_DESTINATION_LOW / CL_DIRECT_DESTINATION: Specialization focusing on the destination type.
- CL_DIRECT_LOW / CL_DIRECT: Full specialization of both source and destination types.
When performing a conversion via the convert function, the system automatically searches for the most appropriate converter by iterating through these levels in descending order (from CL_DIRECT down to CL_ALL_LOW).
Enum items
CL_ALL_LOW
=
0
- The most general conversion level, providing a basic fallback using a standard cast between types.
CL_ALL
=
1
- General conversion level used when no specialized converters are available for the source or destination types.
CL_DIRECT_SOURCE_LOW
=
2
- Provides conversion from a string source to a destination type using standard stream parsing.
CL_DIRECT_SOURCE
=
3
- Specialized conversion level focusing on the source type, used to override default framework converters.
CL_DIRECT_DESTINATION
=
5
- Specialized conversion level focusing on the destination type, used to override default framework converters.
CL_DIRECT_LOW
=
6
- Full specialization for a specific source and destination type pair using the framework's default implementation.
CL_DIRECT
=
7
- Full specialization of both source and destination types, used to override default framework converters.
Example: Demonstrating Conversion Fallback Mechanism
This example shows how the framework automatically selects the most specialized converter available, falling back to more general levels if a specific one is not defined.
#define FCF_BASIS_IMPLEMENTATION
#include <fcfBasis/basis.hpp>
#include <iostream>
#include <string>
// A simple target structure to demonstrate conversion
struct Target {
int value;
};
namespace fcf {
// 1. Highly specialized converter (CL_DIRECT)
// This handles conversion from std::string to Target specifically.
template <>
class Converter<CL_DIRECT, Target, std::string> {
public:
void operator()( Target& a_destination, const std::string& a_source, ConvertOptions&, Error& a_error) {
try {
a_destination.value = std::stoi(a_source);
} catch (...) {
a_error.raise<fcf::Exception>(__FILE__, __LINE__, "Failed to parse string");
}
}
};
// 2. General fallback converter (CL_ALL_LOW)
// This handles a basic static cast from double to Target.
template <typename TSource>
class Converter<CL_ALL_LOW, Target, TSource> {
public:
void operator()( Target& a_destination, const TSource& a_source, ConvertOptions&, Error&) {
a_destination.value = static_cast<int>(a_source);
}
};
}
int main() {
fcf::Error error;
Target t1, t2;
// Case 1: The system finds the most specific converter (CL_DIRECT)
// because we are providing a std::string as the source.
fcf::convert(t1, std::string("42"), error);
std::cout << "Specialized conversion (string to Target): " << t1.value << std::endl;
// Case 2: The system falls back to a lower level (CL_ALL_LOW)
// because no specialized converter exists for the double type.
fcf::convert(t2, 3.14, error);
std::cout << "Fallback conversion (double to Target): " << t2.value << std::endl;
return 0;
}
Output:
Specialized conversion (string to Target): 42
Fallback conversion (double to Target): 3