Ice 3.9
C++ API Reference
Loading...
Searching...
No Matches
StringConverter.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_STRING_CONVERTER_H
4#define ICE_STRING_CONVERTER_H
5
6#include "Config.h"
7#include "LocalException.h"
8
9#include <cstdint>
10#include <memory>
11#include <string>
12#include <vector>
13
14namespace Ice
15{
16 /// Provides bytes to toUTF8.
17 /// @headerfile Ice/Ice.h
18 class ICE_API UTF8Buffer
19 {
20 public:
21 /// Obtains more bytes.
22 /// @param howMany The number of bytes requested.
23 /// @param firstUnused A pointer to the first unused byte.
24 /// @return A pointer to the start of the newly reserved space (the first unused byte).
25 /// @throws std::bad_alloc Thrown when the buffer cannot be resized.
26 virtual std::byte* getMoreBytes(size_t howMany, std::byte* firstUnused) = 0;
27
28 virtual ~UTF8Buffer();
29 };
30
31 /// A StringConverter converts narrow or wide-strings to and from UTF-8 byte sequences.
32 /// It's used by the communicator during marshaling (toUTF8) and unmarshaling (fromUTF8).
33 /// It report errors by throwing IllegalConversionException or an exception thrown
34 /// by UTF8Buffer.
35 /// @headerfile Ice/Ice.h
36 template<typename charT> class BasicStringConverter
37 {
38 public:
39 /// Returns a pointer to byte after the last written byte (which may be
40 /// past the last byte returned by getMoreBytes).
41 virtual std::byte* toUTF8(const charT* sourceStart, const charT* sourceEnd, UTF8Buffer& buf) const = 0;
42
43 /// Unmarshals a UTF-8 sequence into a basic_string.
44 virtual void
45 fromUTF8(const std::byte* sourceStart, const std::byte* sourceEnd, std::basic_string<charT>& target) const = 0;
46
47 virtual ~BasicStringConverter() = default;
48 };
49
50#if defined(__clang__)
51 // Explicit template instantiation so that dynamic_cast of derived exported
52 // classes works well with clang, see ICE-7473.
53 template class __attribute__((visibility("default"))) BasicStringConverter<char>;
54 template class __attribute__((visibility("default"))) BasicStringConverter<wchar_t>;
55#endif
56
57 /// A narrow string converter.
59
60 /// A shared pointer to a StringConverter.
61 using StringConverterPtr = std::shared_ptr<StringConverter>;
62
63 /// A wide string converter.
65
66 /// A shared pointer to a WstringConverter.
67 using WstringConverterPtr = std::shared_ptr<WstringConverter>;
68
69 /// Creates a WstringConverter that converts to and from UTF-16 or UTF-32 depending on sizeof(wchar_t).
70 /// @return The string converter.
72
73 /// Retrieves the per-process narrow string converter.
74 /// @return The string converter.
76
77 /// Sets the per-process narrow string converter.
78 /// @param c The string converter.
80
81 /// Retrieves the per-process wide string converter.
82 /// @return The string converter.
84
85 /// Sets the per process wide string converter.
86 /// @param c The string converter.
88
89 /// Converts a wide string to a narrow string.
90 /// @param str The wide string to convert.
91 /// @param nc The narrow string converter. If null, the result's encoding is UTF-8.
92 /// @param wc The wide string converter. If null, the encoding of @p str is UTF-16 or UTF-32 depending on the size
93 /// of wchar_t.
94 /// @return A narrow string.
95 ICE_API std::string wstringToString(
96 const std::wstring& str,
97 const StringConverterPtr& nc = nullptr,
98 const WstringConverterPtr& wc = nullptr);
99
100 /// Converts a narrow string to a wide string.
101 /// @param str The narrow string to convert.
102 /// @param nc The narrow string converter. If null, the encoding of @p str is UTF-8.
103 /// @param wc The wide string converter. If null, the result's encoding is UTF-16 or UTF-32 depending on the size of
104 /// wchar_t.
105 /// @return A wide string.
106 ICE_API std::wstring stringToWstring(
107 const std::string& str,
108 const StringConverterPtr& nc = nullptr,
109 const WstringConverterPtr& wc = nullptr);
110
111 /// Converts the given string from the native narrow string encoding to
112 /// UTF-8 using the given converter.
113 /// @param str The native narrow string.
114 /// @param nc The narrow string converter. If null, the native string is returned.
115 /// @return A UTF-8 string.
116 ICE_API std::string nativeToUTF8(std::string_view str, const StringConverterPtr& nc);
117
118 /// Converts the given string from UTF-8 to the native narrow string
119 /// encoding using the given converter.
120 /// @param str The UTF-8 string.
121 /// @param nc The narrow string converter. If null, the UTF-8 string is returned.
122 /// @return A native narrow string.
123 ICE_API std::string UTF8ToNative(std::string_view str, const StringConverterPtr& nc);
124
125#if defined(_WIN32) || defined(ICE_DOXYGEN)
126 /// Creates a StringConverter that converts to and from narrow chars
127 /// in the given code page, using MultiByteToWideChar and WideCharToMultiByte.
128 /// @param page The code page.
129 /// @return The string converter.
130 /// @remark Windows only.
132#endif
133
134 /// This exception indicates the failure of a string conversion.
135 /// @headerfile Ice/Ice.h
136 class ICE_API IllegalConversionException final : public LocalException
137 {
138 public:
139 /// Constructs an IllegalConversionException.
140 /// @param file The file where this exception is constructed. This C string is not copied.
141 /// @param line The line where this exception is constructed.
142 /// @param message The error message adopted by this exception and returned by what().
143 IllegalConversionException(const char* file, int line, std::string message)
144 : LocalException(file, line, std::move(message))
145 {
146 }
147
148 [[nodiscard]] const char* ice_id() const noexcept final;
149 };
150}
151
152namespace IceInternal
153{
154 //
155 // Convert from UTF-8 to UTF-16/32
156 //
157 ICE_API std::vector<unsigned short> toUTF16(const std::vector<std::uint8_t>&);
158 ICE_API std::vector<unsigned int> toUTF32(const std::vector<std::uint8_t>&);
159
160 //
161 // Convert from UTF-32 to UTF-8
162 //
163 ICE_API std::vector<std::uint8_t> fromUTF32(const std::vector<unsigned int>&);
164}
165
166#endif
virtual std::byte * toUTF8(const charT *sourceStart, const charT *sourceEnd, UTF8Buffer &buf) const =0
Returns a pointer to byte after the last written byte (which may be past the last byte returned by ge...
virtual void fromUTF8(const std::byte *sourceStart, const std::byte *sourceEnd, std::basic_string< charT > &target) const =0
Unmarshals a UTF-8 sequence into a basic_string.
A StringConverter converts narrow or wide-strings to and from UTF-8 byte sequences.
IllegalConversionException(const char *file, int line, std::string message)
Constructs an IllegalConversionException.
const char * ice_id() const noexcept final
Returns the type ID of this exception.
LocalException(const char *file, int line, std::string message)
Constructs a local exception.
virtual std::byte * getMoreBytes(size_t howMany, std::byte *firstUnused)=0
Obtains more bytes.
Provides bytes to toUTF8.
WstringConverterPtr createUnicodeWstringConverter()
Creates a WstringConverter that converts to and from UTF-16 or UTF-32 depending on sizeof(wchar_t).
void setProcessWstringConverter(const WstringConverterPtr &c)
Sets the per process wide string converter.
std::shared_ptr< StringConverter > StringConverterPtr
A shared pointer to a StringConverter.
std::string nativeToUTF8(std::string_view str, const StringConverterPtr &nc)
Converts the given string from the native narrow string encoding to UTF-8 using the given converter.
BasicStringConverter< char > StringConverter
A narrow string converter.
StringConverterPtr getProcessStringConverter()
Retrieves the per-process narrow string converter.
StringConverterPtr createWindowsStringConverter(unsigned int page)
Creates a StringConverter that converts to and from narrow chars in the given code page,...
std::wstring stringToWstring(const std::string &str, const StringConverterPtr &nc=nullptr, const WstringConverterPtr &wc=nullptr)
Converts a narrow string to a wide string.
BasicStringConverter< wchar_t > WstringConverter
A wide string converter.
std::string UTF8ToNative(std::string_view str, const StringConverterPtr &nc)
Converts the given string from UTF-8 to the native narrow string encoding using the given converter.
void setProcessStringConverter(const StringConverterPtr &c)
Sets the per-process narrow string converter.
WstringConverterPtr getProcessWstringConverter()
Retrieves the per-process wide string converter.
std::shared_ptr< WstringConverter > WstringConverterPtr
A shared pointer to a WstringConverter.
std::string wstringToString(const std::wstring &str, const StringConverterPtr &nc=nullptr, const WstringConverterPtr &wc=nullptr)
Converts a wide string to a narrow string.
The Ice RPC framework.
Definition SampleEvent.h:60