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  buf_ = str;
20  is_internal_alloc_ = false;
21  }
23  operator const char*() NLIB_NOEXCEPT { return buf_ ? buf_ : ""; }
24  const char* c_str() NLIB_NOEXCEPT { return buf_ ? buf_ : ""; }
25  NLIB_SAFE_BOOL(ToUtf8Obj, buf_ != NULL)
26 
27  private:
28  const char* buf_;
29  bool is_internal_alloc_;
31 };
32 
34  public:
35  explicit ToWideObj(const char* str) NLIB_NOEXCEPT;
36  explicit ToWideObj(const wchar_t* wstr) NLIB_NOEXCEPT {
37  buf_ = wstr;
38  is_internal_alloc_ = false;
39  }
41  operator const wchar_t*() NLIB_NOEXCEPT { return buf_ ? buf_ : L""; }
42  const wchar_t* c_str() NLIB_NOEXCEPT { return buf_ ? buf_ : L""; }
43  NLIB_SAFE_BOOL(ToWideObj, buf_ != NULL)
44 
45  private:
46  const wchar_t* buf_;
47  bool is_internal_alloc_;
49 };
50 
51 } // namespace unicode
52 NLIB_NAMESPACE_END
53 
54 #endif // INCLUDE_NN_NLIB_UNICODE_CONVERT_H_
#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:145
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:160
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
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
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:33
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: Config.h:539
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: Config.h:538
const char * c_str() noexcept
Explicitly returns a C string.
Definition: Convert.h:24
ToWideObj(const wchar_t *wstr) noexcept
Sets a wide character string.
Definition: Convert.h:36
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
const wchar_t * c_str() noexcept
Explicitly returns a wide string.
Definition: Convert.h:42
A file that contains the configuration information for each development environment.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211