forked from somma/_MyLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_test_boost.cpp
More file actions
241 lines (207 loc) · 6.02 KB
/
_test_boost.cpp
File metadata and controls
241 lines (207 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**----------------------------------------------------------------------------
* _test_boost.cpp
*-----------------------------------------------------------------------------
*
*-----------------------------------------------------------------------------
* All rights reserved by Noh,Yonghwan (fixbrain@gmail.com, unsorted@msn.com)
*-----------------------------------------------------------------------------
* 2014:1:25 14:30 created
**---------------------------------------------------------------------------*/
#include "stdafx.h"
/**
* @brief boost::lexical_cast
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool boost_lexical_cast()
{
// 숫자 -> 문자
//
std::string s = boost::lexical_cast<std::string>(1024);
std::cout << boost::lexical_cast<std::string>(1024) << std::endl;
std::wcout << boost::lexical_cast<std::wstring>(1024) << std::endl;
// 문자열 -> 숫자
//
std::cout << boost::lexical_cast<float>("3.14") << std::endl;
// 타입 변환 오류
//
try
{
std::cout << boost::lexical_cast<int>("abcd") << std::endl;
}
catch (boost::bad_lexical_cast)
{
std::cout << "bad lexical cast" << std::endl;
}
return true;
}
/**
* @brief shared_ptr 로 void * 처리하기 (HANDLE 과 동일함)
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
void my_free(_In_ void* memory)
{
if (NULL != memory) return;
log_msg "trying to free void pointer at 0x%p", memory log_end
free(memory);
}
bool boost_shared_ptr_void()
{
boost::shared_ptr< boost::remove_pointer<void*>::type > void_ptr( malloc(128), my_free );
if (NULL == void_ptr.get()) return false;
log_msg "void pointer allocated at 0x%p", void_ptr.get() log_end
return true;
}
/**
* @brief boost::shared_ptr 로 HANDLE 처리하기 #1 (이렇게 사용하면 안됨!!)
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool boost_shared_ptr_handle_01()
{
boost::shared_ptr< boost::remove_pointer<HANDLE>::type > file_handle(
open_file_to_read(L"c:\\windows\\system32\\drivers\\etc\\hosts"),
CloseHandle
);
if (INVALID_HANDLE_VALUE == file_handle.get())
{
return false;
}
DWORD bytes_read=0;
unsigned char buffer[128]={0};
if (!ReadFile(file_handle.get(), buffer, 128, &bytes_read, NULL)) return false;
log_msg "ReadFile, buffer = %S", buffer log_end
file_handle.reset(); //!
//> CloseHandle() 이 호출되었을테니... ReadFile() 은 실패해야 하고,
//> GetLastError() == ERROR_INVALID_HANDLE 이어야 함
if (TRUE == ReadFile(file_handle.get(), buffer, 128, &bytes_read, NULL)) return false;
if (ERROR_INVALID_HANDLE != GetLastError()) return false;
return true;
}
/**
* @brief boost::shared_ptr 로 HANDLE 처리하기 #2
* @brief custom destructor 이용하기
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
void MyCloseHandle(_In_ HANDLE file_handle)
{
//> invalid 한 file_handle 이 넘어올 수도 있으므로 꼭 체크해주어야 함
if (INVALID_HANDLE_VALUE == file_handle || NULL == file_handle) return;
log_msg
"file handle = 0x%08x is closing...",
file_handle
log_end
CloseHandle(file_handle);
}
bool boost_shared_ptr_handle_02()
{
typedef boost::shared_ptr< boost::remove_pointer<HANDLE>::type > shared_handle;
shared_handle file_handle (
open_file_to_read(L"c:\\windows\\system32\\drivers\\etc\\hosts"),
MyCloseHandle
);
if (INVALID_HANDLE_VALUE == file_handle.get()) return false;
DWORD bytes_read=0;
unsigned char buffer[128]={0};
if (!ReadFile(file_handle.get(), buffer, 128, &bytes_read, NULL)) return false;
log_msg "ReadFile, buffer = %S", buffer log_end
file_handle.reset(); //!
// MyCloseHandle() 이 호출되었을테니...
//> ReadFile() 은 실패해야 하고, ERROR_INVALID_HANDLE 이어야 함
if (TRUE == ReadFile(file_handle.get(), buffer, 128, &bytes_read, NULL)) return false;
if (ERROR_INVALID_HANDLE != GetLastError()) return false;
return true;
}
/**
* @brief boost::shared_ptr 로 HANDLE 처리하기 #3
* @brief file_hanele 이 NULL 인 경우에 대한 처리
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool boost_shared_ptr_handle_03()
{
typedef boost::shared_ptr< boost::remove_pointer<HANDLE>::type > shared_handle;
shared_handle file_handle (
open_file_to_read(L"c:\\windows\\system32\\drivers\\etc\\hosts\\file_doesnot_exists"),
MyCloseHandle
);
if (INVALID_HANDLE_VALUE == file_handle.get())
{
file_handle.reset();
return true;
}
return false; // never reach here
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
bool boost_tuple()
{
boost::tuple<bool, bool, std::string> _tuple;
// tuple 에 값 설정하기 (I)
bool &is_timeout = boost::tuples::get<0>(_tuple);
bool &io_status = boost::tuples::get<1>(_tuple);
std::string &str = boost::tuples::get<2>(_tuple);
is_timeout = true;
io_status = false;
str = "this is boost::tuple test";
// tuple 로부터 값 읽기
std::cout << "is_timeout=" << boost::tuples::get<0>(_tuple) << std::endl
<< "io_status=" << boost::tuples::get<1>(_tuple) << std::endl
<< "str =" << boost::tuples::get<2>(_tuple) << std::endl;
// tuple 에 값 설정하기 (II)
boost::tuples::get<0>(_tuple) = true;
boost::tuples::get<1>(_tuple) = false;
boost::tuples::get<2>(_tuple) = "this is type2 assignment";
std::cout << "is_timeout=" << boost::tuples::get<0>(_tuple) << std::endl
<< "io_status=" << boost::tuples::get<1>(_tuple) << std::endl
<< "str =" << boost::tuples::get<2>(_tuple) << std::endl;
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool boost_format()
{
boost::wformat f = boost::wformat(L"msg = %s, int = %d, float = %f") % L"string..." % 100 % 3.14;
// to cout
std::wcout << L"f = " << f << std::endl;
// to string
std::wstring& fs = f.str();
std::wcout << L"fs = " << fs << std::endl;
return true;
}