Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

4.5 Modifying a Standard Facet's Behavior

At times you may need to replace a facet object in a locale by another kind of facet object. In the following example, let us derive from one of the standard facet classes, numpunct, and create a locale object in which the standard numpunct facet object is replaced by an instance of our new, derived facet class.

Here is the problem we want to solve. When you print boolean values, you can choose between the numeric representation of the values "true" and "false", or their alphanumeric representation.

//1A variable of type bool is defined. Its initial value is the boolean value of the logical expression (argc > 1), so the variable any_arguments contains the information, whether the program was called with or without arguments.
//2The format flag ios_base:: boolalpha is set in the predefined output stream cout. The effect is that the string representation of boolean values is printed, instead of their numerical representation 0 or 1, which is the default representation.
//3Here either the string "true" or the string "false" are printed.

Of course, the string representation depends on the language. Hence, the alphanumeric representation of boolean values is provided by a locale. It is the numpunct facet of a locale that describes the cultural conventions for numerical formatting. It contains services that return the string representations of the boolean values true and false.

This is the interface of facet numpunct:

Now let us replace this facet. To make it more exciting, let's use not only a different language, but also different words for true and false, such as Yes! and No!. For just using another language, we would not need a new facet; we would simply use the right native locale, and it would contain the right facet.

//1The new facet is a class template that takes the character type as a template parameter.
//2The new facet is derived from the numpunct_byname<charT> facet.

The byname facets read the respective locale information from the external representation of a C locale. The name provided to construct a byname facet is the name of a locale, as you would use it in a call to setlocale().
//3A constructor is provided that takes a locale name and the new values we want to display for the alpha versions of true and false. The fourth parameter, refs, controls the facet's lifetime, as described in an earlier section.
//4The virtual member functions do_truename() and do_falsename() are reimplemented. They are called by the public member functions truename() and falsename(). See the Class Reference for further details.

Now let's create a German locale in which the numpunct<char> facet has been replaced by an object of our new derived facet type, as shown in Figure 11:

Figure 11 -- Replacing the numpunct<char> facet object

The code looks like this:

//1A locale object is constructed with an instance of the new facet class. The locale object has all facet objects from a German locale object, except that the new facet object change_bool_names substitutes for the numpunct facet object.
//2The new facet object takes all information from a German numpunct facet object, and replaces the default native names for true and false with the provided strings "Ja."("Yes.") and "Nein."("No.").
//3The standard output stream cout is imbued with the newly created locale.
//4The expression (argc > 1) yields a boolean value, which indicates whether the program was called with arguments. This boolean value's alphanumeric representation is printed to the standard output stream. The output might be:

Argument vorhanden? Ja.

Previous fileTop of DocumentContentsIndexNext file

OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.