Implicitly Converting to Unicode
Each
RWUToUnicodeConversionContext instance contains a unique
RWUToUnicodeConverter instance used for all relevant implicit conversions. For example, this code sets the to-Unicode conversion context to ASCII, then uses implicit conversion to construct an
RWUString:
RWUToUnicodeConversionContext fromAsciiContext("US-ASCII");
RWUString str = "hello";
The code above is a convenient alternative to the following explicit conversion, especially when you are creating many strings with the same encoding conversion:
RWUToUnicodeConverter fromAscii("US-ASCII");
RWUString str("hello", fromAscii);
As outlined in
The Conversion Context Stacks,
RWUToUnicodeConversionContext manages internal, per-thread stacks of
RWUToUnicodeConversionContext instances. The static method
RWUToUnicodeConversionContext::getContext() returns a reference to the
RWUToUnicodeConversionContext currently in effect.
Within any local scope, you can create an
RWUToUnicodeConversionContext. For example, this code creates two conversion contexts, one nested inside the other:
RWUToUnicodeConversionContext fromAsciiContext("US-ASCII");
RWUString str1 = "hello";
{
RWUToUnicodeConversionContext
fromIso_8859_1Context("ISO-8859-1");
RWUString str2 = "goodbye";
}
At the closing curly brace, the nested
RWUToUnicodeConversionContext instance goes out of scope and is destroyed. Destroying the instance automatically pops it off the current thread’s to-Unicode stack.
Do not create unnamed, temporary instances of
RWUToUnicodeConversionContext. The destructors for such objects pop the context off the context stack immediately. For example, although this code would compile, it may not have the intended effect:
RWUToUnicodeConversionContext("US-ASCII"); // Wrong!
RWUString str("hello");
NOTE: Do not create unnamed, temporary instances of RWUToUnicodeConversionContext.
The
getConverter() method returns a reference to the
RWUToUnicodeConverter instance held by an
RWUToUnicodeConversionContext object. For example, you might want to set the conversion error-handling behavior of the underlying converter (
Conversion Errors), as follows:
RWUToUnicodeConversionContext fromAsciiContext("US-ASCII");
fromAsciiContext.getConverter().setErrorResponse(
RWUToUnicodeConverter::Skip);
RWUString str("hello");