|
BDE 4.39.x Production Release
|
Provide a compile-time switch meta-function.
Provide a compile-time switch meta-function.
switch meta-function (variable number of types)switch meta-function (among two types)switch meta-function (among three types)switch meta-function (among four types)switch meta-function (among five types)switch meta-function (among six types)switch meta-function (among seven types)switch meta-function (among eight types)switch meta-function (among nine types)This component provides a compile-time switch meta-function. Its main class, bslmf::Switch, parameterized by an integral t_SWITCH_SELECTOR and a variable number N of types, t_T0 up to T{N - 1}, contains a single type named Type, which is the result of the meta-function and is an alias to T{t_SWITCH_SELECTOR} or to bslmf::Nil if t_SWITCH_SELECTOR is outside the range [ 0 .. N - 1 ]. The analogy between the following "meta-code" and its valid C++ version using bslmf::Switch may serve as a useful mental picture to understand and memorize the usage of this component.
Note the use of the keyword typename, necessary only if one or more of the t_SWITCH_SELECTOR or t_T0 up to T{N - 1} is dependent on a template parameter of the local context (i.e., that of the block using bslmf::Switch). In particular, it should be omitted if the bslmf::Switch is not used within a class or function template, as in the usage example below.
For most situations, the number N of template type arguments is known and the bslmf::SwitchN meta-functions, which take exactly the indicated number of arguments, should be preferred. Their usage leads to shorter mangled symbol names in object files (e.g., no extra defaulted template type arguments are included in the name), and shorter compilation times, as well.
Assume an external server API for storing and retrieving data:
In our application, we need some very small (1, 2, and 4-byte), special-purpose string types, so we create the following ShortString class template:
We would like to store our short strings in the data server, but the data server only handles char, short and int types. Since our strings fit into these simple types, we can transform ShortString into these integral types when calling store and retrieve, using bslmf::Switch to choose which integral type to use for each ShortString type:
In our main program, we first assert our basic assumptions, then we store and retrieve strings using our ShortString template.
| #define bslmf_Switch bslmf::Switch |
This alias is defined for backward compatibility.
| #define bslmf_Switch2 bslmf::Switch2 |
This alias is defined for backward compatibility.
| #define bslmf_Switch3 bslmf::Switch3 |
This alias is defined for backward compatibility.
| #define bslmf_Switch4 bslmf::Switch4 |
This alias is defined for backward compatibility.
| #define bslmf_Switch5 bslmf::Switch5 |
This alias is defined for backward compatibility.
| #define bslmf_Switch6 bslmf::Switch6 |
This alias is defined for backward compatibility.
| #define bslmf_Switch7 bslmf::Switch7 |
This alias is defined for backward compatibility.
| #define bslmf_Switch8 bslmf::Switch8 |
This alias is defined for backward compatibility.
| #define bslmf_Switch9 bslmf::Switch9 |
This alias is defined for backward compatibility.