Implicitly Converting from Unicode
Each
RWUFromUnicodeConversionContext instance contains a unique
RWUFromUnicodeConverter instance used for all relevant implicit conversions.
For example, if
str is an
RWUString instance, you could write out a Shift-JIS representation of
str like this:
RWUFromUnicodeConversionContext toShiftJisContext("Shift-JIS");
std::cout << str << std::endl;
The stream insertion operator for
RWUString writes the sequence of bytes that are produced when the contents of
str are converted into the encoding specified by the currently active
RWUFromUnicodeConversionContext.
When called with no arguments, the
RWUString::toBytes() method also serializes the contents of an
RWUString using the currently active
RWUFromUnicodeConversionContext.
As outlined in
The Conversion Context Stacks,
RWUFromUnicodeConversionContext manages internal, per-thread stacks of
RWUFromUnicodeConversionContext instances. The static
RWUFromUnicodeConversionContext::getContext() method returns a reference to the
RWUFromUnicodeConversionContext currently in effect.
Within any local scope, you can create an
RWUFromUnicodeConversionContext. For example, this code outputs the contents of
str first in the ISO-8859-1 encoding, then in Shift-JIS:
RWUFromUnicodeConversionContext toShiftJisContext("Shift-JIS");
{
RWUFromUnicodeConversionContext
toIso_8859_1Context("ISO-8859-1");
std::cout << str << std::endl; // ISO-8859-1
}
std::cout << str << std::endl; // Shift-JIS
At the closing curly brace, the nested
RWUFromUnicodeConversionContext instance goes out of scope and is destroyed. Destroying the instance automatically pops it off the current thread’s from-Unicode stack.
Do not create unnamed, temporary instances of
RWUFromUnicodeConversionContext. 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:
RWUFromUnicodeConversionContext("ISO-8859-1"); // Wrong!
std::cout << str << std::endl;
NOTE: Do not create unnamed, temporary instances of RWUFromUnicodeConversionContext.
The
getConverter() method returns a reference to the
RWUFromUnicodeConverter instance held by an
RWUFromUnicodeConversionContext object. For example, you might want to set the conversion error-handling behavior of the underlying converter (
Conversion Errors):
RWUFromUnicodeConversionContext toShiftJisContext("Shift-JIS");
toShiftJisContext.getConverter().setErrorResponse(
RWUFromUnicodeConverter::EscapeNativeHexadecimal);
std::cout << str << std::endl;