nlib
Convert.h
Go to the documentation of this file.
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
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
bool IsOk() const noexcept
: Checks whether the object is valid.
Definition: Convert.h:47
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:126
Class to use to pass a UTF-8, UTF-16, or UTF-32 string as UTF-8.
Definition: Convert.h:12
ToUtf8Obj(const char *str) noexcept
Sets the UTF-8 string.
Definition: Convert.h:17
Class used to pass a UTF-8, UTF-16, or UTF-32 string as a wide character (UTF-16/UTF-32) string...
Definition: Convert.h:35
uint32_t nlib_utf32_t
Uses typedef to define as char32_t if that can be used. If not, it uses typedef to define as uint32_t...
Definition: Platform.h:2161
uint16_t nlib_utf16_t
Uses typedef to define as char16_t if that can be used. If not, it uses typedef to define as uint16_t...
Definition: Platform.h:2160
const char * c_str() noexcept
Explicitly returns a C string.
Definition: Convert.h:26
ToWideObj(const wchar_t *wstr) noexcept
Sets a wide character string.
Definition: Convert.h:38
const wchar_t * c_str() noexcept
Explicitly returns a C string.
Definition: Convert.h:46
A file that contains the configuration information for each development environment.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
bool IsOk() const noexcept
: Checks whether the object is valid.
Definition: Convert.h:27