|
BDE 4.14.0 Production release
|
Enumerate the types supported for command-line-option values.
enum and utilitiesThis component provides a struct, balcl::OptionType, that is a namespace for:
typedef for each of the types allowed for command-line-option values.The enumerator values are used to control type-dependent operations where the type is not known until runtime.
The names of related symbols are similar to each other. For example, the supported option-value type bsl::vector<bdlt::Datetime> has:
The null pointer symbols are typically used with the constructor of balcl::TypeInfo. They provide a clearly named way to select the constructor to create an object having the desired type attribute. See balcl_typeinfo and balcl_commandline .
In addition to the conventional enumeration methods, this component also provides some utility functions that categorize the represented types. For example, given an enumerator representing a scalar type, the balcl::OptionType::toArrayType method returns the enumerator representing an array of that scalar type.
This section illustrates intended use of this component.
The following snippets of code provide a simple illustration of balcl::OptionType usage.
First, we create a variable value of type balcl::OptionType::Enum and initialize it to the value balcl::OptionType::e_STRING:
Next, we store a pointer to its ASCII representation in a variable asciiValue of type const char *:
Finally, we print the value to bsl::cout:
This statement produces the following output on stdout:
In a software system devoted to assembling option values of various types, the code is often governed in terms of the enumerated values (balcl::OptionType::Enum) corresponding to the various types. In particular, in order to assemble an option value of one of the array types (e.g., balcl::OptionType::e_STRING_ARRAY), one must first construct the constitute elements.
Suppose we have a class, MyMultitypeValue, that can, at runtime, be set to contain a value of one of the types named by balcl::OptionType. We may want to initialize a MyMultitypeValue object from an input stream using a utility function MyMultitypeValueUtil::parse:
If type is not one of the array types, as determined by the balcl::OptionType::isArrayType method, one calls MyMultitypeValueUtil::parseScalar:
Otherwise, we have an array type. In this case, we must call parseScalar repeatedly and build a vector of those scalar values. The scalar type can be calculated from the given array type by the balcl::OptionType::fromArrayType method: