Ice 3.8
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 beginning of the buffer.
25 /// @throws std::bad_alloc Thrown when too many bytes are requested.
26 /// @throws MarshalException Thrown when too many bytes are requested.
27 virtual std::byte* getMoreBytes(size_t howMany, std::byte* firstUnused) = 0;
28
29 virtual ~UTF8Buffer();
30 };
31
32 /// A StringConverter converts narrow or wide-strings to and from UTF-8 byte sequences.
33 /// It's used by the communicator during marshaling (toUTF8) and unmarshaling (fromUTF8).
34 /// It report errors by throwing IllegalConversionException or an exception thrown
35 /// by UTF8Buffer.
36 /// @headerfile Ice/Ice.h
37 template<typename charT> class BasicStringConverter
38 {
39 public:
40 /// Returns a pointer to byte after the last written byte (which may be
41 /// past the last byte returned by getMoreBytes).
42 virtual std::byte* toUTF8(const charT* sourceStart, const charT* sourceEnd, UTF8Buffer& buf) const = 0;
43
44 /// Unmarshals a UTF-8 sequence into a basic_string.
45 virtual void
46 fromUTF8(const std::byte* sourceStart, const std::byte* sourceEnd, std::basic_string<charT>& target) const = 0;
47
48 virtual ~BasicStringConverter() = default;
49 };
50
51#if defined(__clang__)
52 // Explicit template instantiation so that dynamic_cast of derived exported
53 // classes works well with clang, see ICE-7473.
54 template class __attribute__((visibility("default"))) BasicStringConverter<char>;
55 template class __attribute__((visibility("default"))) BasicStringConverter<wchar_t>;
56#endif
57
58 /// A narrow string converter.
60
61 /// A shared pointer to a StringConverter.
62 using StringConverterPtr = std::shared_ptr<StringConverter>;
63
64 /// A wide string converter.
66
67 /// A shared pointer to a WstringConverter.
68 using WstringConverterPtr = std::shared_ptr<WstringConverter>;
69
70 /// Creates a WstringConverter that converts to and from UTF-16 or UTF-32 depending on sizeof(wchar_t).
71 /// @return The string converter.
73
74 /// Retrieves the per-process narrow string converter.
75 /// @return The string converter.
77
78 /// Sets the per-process narrow string converter.
79 /// @param c The string converter.
81
82 /// Retrieves the per-process wide string converter.
83 /// @return The string converter.
85
86 /// Sets the per process wide string converter.
87 /// @param c The string converter.
89
90 /// Converts the given wide string to a narrow string.
91 /// @param str A wide string.
92 /// @param nc The narrow string converter. If null, the result's narrow string encoding is UTF-8.
93 /// @param wc The wide string converter. If null, the input's wstring encoding is UTF-16 or UTF-32
94 /// depending on the size of wchar_t.
95 /// @return A narrow string.
96 ICE_API std::string wstringToString(
97 const std::wstring& str,
98 const StringConverterPtr& nc = nullptr,
99 const WstringConverterPtr& wc = nullptr);
100
101 /// Converts the given narrow string to a wide string.
102 /// @param str A narrow string.
103 /// @param nc The narrow string converter. If null, the result's narrow string encoding is UTF-8.
104 /// @param wc The wide string converter. If null, the input's wstring encoding is UTF-16 or UTF-32
105 /// depending on the size of wchar_t.
106 /// @return A wide string.
107 ICE_API std::wstring stringToWstring(
108 const std::string& str,
109 const StringConverterPtr& nc = nullptr,
110 const WstringConverterPtr& wc = nullptr);
111
112 /// Converts the given string from the native narrow string encoding to
113 /// UTF-8 using the given converter.
114 /// @param str The native narrow string.
115 /// @param nc The narrow string converter. If null, the native string is returned.
116 /// @return A UTF-8 string.
117 ICE_API std::string nativeToUTF8(std::string_view str, const StringConverterPtr& nc);
118
119 /// Converts the given string from UTF-8 to the native narrow string
120 /// encoding using the given converter.
121 /// @param str The UTF-8 string.
122 /// @param nc The narrow string converter. If null, the UTF-8 string is returned.
123 /// @return A native narrow string.
124 ICE_API std::string UTF8ToNative(std::string_view str, const StringConverterPtr& nc);
125
126#if defined(_WIN32) || defined(ICE_DOXYGEN)
127 /// Creates a StringConverter that converts to and from narrow chars
128 /// in the given code page, using MultiByteToWideChar and WideCharToMultiByte.
129 /// @param page The code page.
130 /// @return The string converter.
131 /// @remark Windows only.
133#endif
134
135 /// This exception indicates the failure of a string conversion.
136 /// @headerfile Ice/Ice.h
137 class ICE_API IllegalConversionException final : public LocalException
138 {
139 public:
140 /// Constructs an IllegalConversionException.
141 /// @param file The file where this exception is constructed. This C string is not copied.
142 /// @param line The line where this exception is constructed.
143 /// @param message The error message adopted by this exception and returned by what().
144 IllegalConversionException(const char* file, int line, std::string message)
145 : LocalException(file, line, std::move(message))
146 {
147 }
148
149 [[nodiscard]] const char* ice_id() const noexcept final;
150 };
151}
152
153namespace IceInternal
154{
155 //
156 // Convert from UTF-8 to UTF-16/32
157 //
158 ICE_API std::vector<unsigned short> toUTF16(const std::vector<std::uint8_t>&);
159 ICE_API std::vector<unsigned int> toUTF32(const std::vector<std::uint8_t>&);
160
161 //
162 // Convert from UTF-32 to UTF-8
163 //
164 ICE_API std::vector<std::uint8_t> fromUTF32(const std::vector<unsigned int>&);
165}
166
167#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 the given 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 the given wide string to a narrow string.
The Ice RPC framework.
Definition SampleEvent.h:59