nlib
Convert.h
[詳解]
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_UNICODE_CONVERT_H_
4 #define INCLUDE_NN_NLIB_UNICODE_CONVERT_H_
5 
6 #include "nn/nlib/Config.h"
7 
8 // functions below may be deprecated in the future
9 NLIB_NAMESPACE_BEGIN
10 namespace unicode {
11 
13  public:
14  explicit ToUtf8Obj(const wchar_t* wstr) NLIB_NOEXCEPT;
15  explicit ToUtf8Obj(const nlib_utf16_t* utf16str) NLIB_NOEXCEPT;
16  explicit ToUtf8Obj(const nlib_utf32_t* utf32str) NLIB_NOEXCEPT;
17  explicit ToUtf8Obj(const char* str) NLIB_NOEXCEPT {
18  // does not work if the encoding is not UTF-8.
19  m_Buf = str;
20  m_Alloc = false;
21  }
23  if (m_Alloc) delete[] m_Buf;
24  }
25  operator const char*() NLIB_NOEXCEPT { return m_Buf ? m_Buf : ""; }
26  const char* c_str() NLIB_NOEXCEPT { return m_Buf ? m_Buf : ""; }
27  bool IsOk() const NLIB_NOEXCEPT { return m_Buf != NULL; }
28 
29  private:
30  const char* m_Buf;
31  bool m_Alloc;
33 };
34 
36  public:
37  explicit ToWideObj(const char* str) NLIB_NOEXCEPT;
38  explicit ToWideObj(const wchar_t* wstr) NLIB_NOEXCEPT {
39  m_Buf = wstr;
40  m_Alloc = false;
41  }
43  if (m_Alloc) delete[] m_Buf;
44  }
45  operator const wchar_t*() NLIB_NOEXCEPT { return m_Buf ? m_Buf : L""; }
46  const wchar_t* c_str() NLIB_NOEXCEPT { return m_Buf ? m_Buf : L""; }
47  bool IsOk() const NLIB_NOEXCEPT { return m_Buf != NULL; }
48 
49  private:
50  const wchar_t* m_Buf;
51  bool m_Alloc;
53 };
54 
55 } // namespace unicode
56 NLIB_NAMESPACE_END
57 
58 #endif // INCLUDE_NN_NLIB_UNICODE_CONVERT_H_
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Platform.h:2151
bool IsOk() const noexcept
オブジェクトが有効かどうかを調べます。
Definition: Convert.h:47
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:126
UTF-8/UTF-16/UTF-32の文字列をUTF-8として渡すためのクラスです。
Definition: Convert.h:12
ToUtf8Obj(const char *str) noexcept
UTF-8文字列を設定します。
Definition: Convert.h:17
UTF-8/UTF-16/UTF-32の文字列をワイド文字列(UTF-16/UTF-32)として渡すためのクラスです。 ...
Definition: Convert.h:35
uint32_t nlib_utf32_t
char32_tが利用できる場合はchar32_tに、そうでない場合はuint32_tにtypedefされます。 ...
Definition: Platform.h:2161
uint16_t nlib_utf16_t
char16_tが利用できる場合はchar16_tに、そうでない場合はuint16_tにtypedefされます。 ...
Definition: Platform.h:2160
const char * c_str() noexcept
明示的にC文字列を返します。
Definition: Convert.h:26
ToWideObj(const wchar_t *wstr) noexcept
ワイド文字列を設定します。
Definition: Convert.h:38
const wchar_t * c_str() noexcept
明示的にC文字列を返します。
Definition: Convert.h:46
開発環境別の設定が書かれるファイルです。
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:51
bool IsOk() const noexcept
オブジェクトが有効かどうかを調べます。
Definition: Convert.h:27