Lib
QOLを高める
util.h
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include <memory>
8 #include <array>
9 #include <string>
10 #include <windows.h>
11 #include <Unknwn.h>
12 
13 namespace yappy {
15 namespace util {
16 
24 class noncopyable {
25 public:
26  noncopyable() = default;
27  noncopyable(const noncopyable &) = delete;
28  noncopyable &operator=(const noncopyable &) = delete;
29 };
30 
31 
35 using IdString = std::array<char, 16>;
36 
39 template <size_t N>
40 inline void createFixedString(std::array<char, N> *out, const char *src) {
41  size_t len = std::strlen(src);
42  if (len >= N) {
43  error::throwTrace<std::invalid_argument>(
44  std::string("String size is too long: ") + src);
45  }
46  // copy non-'\0' part
47  std::memcpy(out->data(), src, len);
48  // fills the remainder with '\0'
49  std::memset(out->data() + len, '\0', N - len);
50 }
51 
52 
54 struct HandleDeleter {
55  using pointer = HANDLE;
56  void operator()(HANDLE h)
57  {
58  if (h != INVALID_HANDLE_VALUE) {
59  ::CloseHandle(h);
60  }
61  }
62 };
64 using HandlePtr = std::unique_ptr<HANDLE, HandleDeleter>;
65 
67 struct heapDeleter {
68  using pointer = HANDLE;
69  void operator()(HANDLE h)
70  {
71  if (h != INVALID_HANDLE_VALUE) {
72  ::HeapDestroy(h);
73  }
74  }
75 };
77 using HeapPtr = std::unique_ptr<HANDLE, heapDeleter>;
78 
80 struct ComDeleter {
81  void operator()(IUnknown *p)
82  {
83  p->Release();
84  }
85 };
87 template<class T>
88 using ComPtr = std::unique_ptr<T, ComDeleter>;
89 
91 struct FileDeleter {
92  void operator()(FILE *fp)
93  {
94  ::fclose(fp);
95  }
96 };
98 using FilePtr = std::unique_ptr<FILE, FileDeleter>;
99 
100 
105 inline std::unique_ptr<char[]> wc2utf8(const wchar_t *in)
106 {
107  int len = ::WideCharToMultiByte(CP_UTF8, 0, in, -1, nullptr, 0, nullptr, nullptr);
108  std::unique_ptr<char[]> pBuf(new char[len]);
109  ::WideCharToMultiByte(CP_UTF8, 0, in, -1, pBuf.get(), len, nullptr, nullptr);
110  return pBuf;
111 }
112 
117 inline std::unique_ptr<wchar_t[]> utf82wc(const char *in)
118 {
119  int len = ::MultiByteToWideChar(CP_UTF8, 0, in, -1, nullptr, 0);
120  std::unique_ptr<wchar_t[]> pBuf(new wchar_t[len]);
121  ::MultiByteToWideChar(CP_UTF8, 0, in, -1, pBuf.get(), len);
122  return pBuf;
123 }
124 
127 class CoInitialize : private noncopyable {
128 public:
130  CoInitialize() { ::CoInitializeEx(nullptr, COINIT_MULTITHREADED); }
132  ~CoInitialize() { ::CoUninitialize(); }
133 };
134 
135 } // namespace util
136 } // namespace yappy
137 
138 namespace std {
139 
142 template <>
143 struct hash<yappy::util::IdString> {
144  size_t operator()(const yappy::util::IdString &key) const
145  {
146  size_t hashCode = 0;
147  const char *p = key.data();
148  for (size_t i = 0; i < key.size(); i++) {
149  hashCode = hashCode * 31 + p[i];
150  }
151  return hashCode;
152  }
153 };
154 
155 } // namespace std
std::unique_ptr< char[]> wc2utf8(const wchar_t *in)
Wide char to UTF-8.
Definition: util.h:105
Noncopyable class.
Definition: util.h:24
void createFixedString(std::array< char, N > *out, const char *src)
Create yappy::util::IdString from C-style string.
Definition: util.h:40
std::array< char, 16 > IdString
Fixed size char array for string ID.
Definition: util.h:35
std::unique_ptr< FILE, FileDeleter > FilePtr
unique_ptr of FILE with FileDeleter.
Definition: util.h:98
size_t operator()(const yappy::util::IdString &key) const
Definition: util.h:144
Deleter: auto flose().
Definition: util.h:91
STL namespace.
Deleter: auto IUnknown::Release().
Definition: util.h:80
void operator()(FILE *fp)
Definition: util.h:92
std::unique_ptr< HANDLE, heapDeleter > HeapPtr
unique_ptr of HANDLE with heapDeleter.
Definition: util.h:77
Definition: config.cpp:6
noncopyable & operator=(const noncopyable &)=delete
Deleter: auto CloseHandle().
Definition: util.h:54
Auto CoInitializeEx() and CoUninitialize() class.
Definition: util.h:127
std::unique_ptr< HANDLE, HandleDeleter > HandlePtr
unique_ptr of HANDLE with HandleDeleter.
Definition: util.h:64
std::unique_ptr< wchar_t[]> utf82wc(const char *in)
UTF-8 to wide char.
Definition: util.h:117
CoInitialize()
CoInitializeEx(nullptr, COINIT_MULTITHREADED)
Definition: util.h:130
void operator()(HANDLE h)
Definition: util.h:69
Deleter: auto HeapDestroy().
Definition: util.h:67
~CoInitialize()
CoUninitialize()
Definition: util.h:132
void operator()(IUnknown *p)
Definition: util.h:81
std::unique_ptr< T, ComDeleter > ComPtr
unique_ptr of IUnknown with ComDeleter.
Definition: util.h:88
void operator()(HANDLE h)
Definition: util.h:56