Module: Internationalization Module Group: Character Encoding Scheme Conversion
Does Not Inherit
#include <rw/i18n/RWUToUnicodeConversionContext.h>
RWUToUnicodeConversionContext specifies the default character encoding scheme to use for subsequent implicit conversions from narrow character strings to UTF-16 encoded Unicode strings.
RWUToUnicodeConversionContext uses the resource acquisition is initialization idiom to manage an internal, per-thread stack of RWUToUnicodeConversionContext instances, each containing an RWUToUnicodeConverter object.
The top-level element of the stack defines the conversion required to translate implicitly a narrow character string into an equivalent UTF-16 encoded Unicode string.
Each RWUToUnicodeConversionContext instance contains an RWUToUnicodeConverter instance used for all relevant implicit conversions.
The RWUToUnicodeConversionContext constructor pushes a reference to the new instance onto the context stack. The RWUToUnicodeConversionContext destructor pops the instance off the context stack.
Instances of this class can only be constructed with static or automatic storage duration; dynamic allocation is not allowed. Do not create unnamed, temporary instances. The destructors for such objects pop the context off the context stack prematurely.
#include <rw/i18n/RWUToUnicodeConversionContext.h> #include <rw/i18n/RWUFromUnicodeConverter.h> #include <rw/i18n/RWUString.h> #include <iostream> int main() { // Indicate that source strings should be implicitly // converted from ASCII RWUToUnicodeConversionContext fromAscii("ASCII"); // NOTE! This won't work: RWUToUnicodeConversionContext("ASCII"); // Use implicit conversion to construct `str' RWUString str("ASCII"); { // Create a nested conversion context RWUToUnicodeConversionContext fromIso_8859_1("ISO-8859-1"); // Use implicit conversion to initialize `str2' RWUString str2 = " and ISO-8859-1 source: "; str += str2; // Nested context goes out of scope } // Write a Shift-JIS encoded representation of // `str' to std::cout RWUFromUnicodeConverter toShiftJis("Shift-JIS"); std::cout << str.toBytes(toShiftJis) << "in Shift-JIS" << std::endl; // Original context goes out of scope return 0; } Results: ======== ASCII and ISO-8859-1 source: in Shift-JIS
RWUToUnicodeConverter, RWUFromUnicodeConversionContext, RWUConversionContext
static RWUToUnicodeConversionContext& getContext();
Returns a reference to the current RWUToUnicodeConversionContext.
Note: Using this method to access a user-defined or root context after the program has exited main() may produce unpredictable or erroneous behavior. Do not use conversion contexts during the destruction of static-duration objects.
RWUToUnicodeConversionContext(const char* encoding = RWUConverterBase::getDefaultEncodingName());
Constructs an RWUToUnicodeConversionContext for the specified encoding. The to-Unicode conversion context specifies the default implicit conversion to use when automatically converting a sequence of bytes, encoded accorded to the specified encoding, into a UTF-16 representation. The encoding name is encoded in ASCII and must specify one of the names or aliases recognized by the Internationalization Module (see RWUAvailableEncodingList and RWUEncodingAliasList).
The conversion context remains in effect until another conversion context is specified, or until the conversion context instance goes out of scope and is destroyed.
Throws RWUException to indicate that a converter could not be constructed for the specified encoding. The exception carries one of the following status codes:
RWUMemoryAllocationError
Indicates that the memory required by the converter could not be allocated.
RWUFileAccessError
Indicates that the requested converter could not be found.
~RWUToUnicodeConversionContext();
Destructor. Destroys self and restores the conversion context that was in effect prior to the construction of this instance.
RWUToUnicodeConverter& getConverter();
Returns a reference to self's internal RWUToUnicodeConverter instance for use in converting standard C++ and Rogue Wave strings in various encodings into UTF-16 encoded RWUStrings.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.