BDE 4.14.0 Production release
Loading...
Searching...
No Matches
balcl_optiontype

Detailed Description

Outline

Purpose

Enumerate the types supported for command-line-option values.

Classes

See also
balcl_optionvalue, balcl_commandline

Description

This component provides a struct, balcl::OptionType, that is a namespace for:

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:

Symbol Description
------------------------------------ --------------------------------
balcl::OptionType::e_DATETIME_ARRAY enumerator
balcl::OptionType::k_DATETIME_ARRAY statically-initialized null pointer
Definition bslstl_vector.h:1025
Definition balcl_commandline.h:1364

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.

Enumerators

Enumerator Type Alias Type
---------------- ------------- -------------------------------
e_VOID n/a void
e_BOOL Bool bool
e_CHAR Char char
e_INT Int int
e_INT64 Int64 bsls::Types::Int64
e_DOUBLE Double double
e_STRING String bsl::string
e_DATETIME Datetime bdlt::Datetime
e_DATE Date bdlt::Date
e_TIME Time bdlt::Time
e_CHAR_ARRAY CharArray bsl::vector<char>
e_INT_ARRAY IntArray bsl::vector<int>
e_INT64_ARRAY Int64Array bsl::vector<bsls::Types::Int64>
e_DOUBLE_ARRAY DoubleArray bsl::vector<double>
e_STRING_ARRAY StringArray bsl::vector<bsl::string>
e_DATETIME_ARRAY DatetimeArray bsl::vector<bdlt::Datetime>
e_DATE_ARRAY DateArray bsl::vector<bdlt::Date>
e_TIME_ARRAY TimeArray bsl::vector<bdlt::Time>
Definition bdlt_date.h:294
Definition bdlt_datetime.h:331
Definition bdlt_time.h:196
Definition bslstl_string.h:1281
long long Int64
Definition bsls_types.h:132

Usage

This section illustrates intended use of this component.

Example 1: Basic Syntax

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:

Enum
Definition balcl_optiontype.h:248
@ e_STRING
Definition balcl_optiontype.h:256

Next, we store a pointer to its ASCII representation in a variable asciiValue of type const char *:

const char *asciiValue = balcl::OptionType::toAscii(value);
assert(0 == bsl::strcmp(asciiValue, "STRING"));
static const char * toAscii(OptionType::Enum value)

Finally, we print the value to bsl::cout:

bsl::cout << value << bsl::endl;

This statement produces the following output on stdout:

STRING

Example 2: Utility Methods

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:

int MyMultitypeValueUtil::parse(MyMultitypeValue *result,
bsl::ostream& input,
{
BSLS_ASSERT(result);
result->setType(type);
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804

If type is not one of the array types, as determined by the balcl::OptionType::isArrayType method, one calls MyMultitypeValueUtil::parseScalar:

return MyMultitypeValueUtil::parseScalar(result, input, type);
// RETURN
} else {
static bool isArrayType(Enum type)
Definition balcl_optiontype.h:409

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:

MyMultitypeValue element(scalarType);
int rc;
while (0 == (rc = MyMultitypeValueUtil::parseScalar(&element,
input,
scalarType))) {
result->append(element);
}
return rc; // RETURN
}
}
static Enum fromArrayType(Enum type)
Definition balcl_optiontype.h:394