-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp11fun.cpp
executable file
·245 lines (176 loc) · 5.76 KB
/
cpp11fun.cpp
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
#include "cpp11fun.h"
#include <stdio.h>
#include <string>
#include <stdint.h>
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <string.h>
#include <utility>
#include <vector>
void ProcessValue(int& i)
{
std::cout << "LValue processed: " << i << std::endl;
}
void ProcessValue(int&& i)
{
std::cout << "RValue processed: " << i << std::endl;
}
void process_value(int& i) {
std::cout << "LValue processed: " << i << std::endl;
}
void process_value(int&& i) {
std::cout << "RValue processed: " << i << std::endl;
}
class Moveable {
public:
Moveable() : i(new int(3)) {
std::cout << "Moveable::Moveable() 构造函数 : ptr(i)=" << i << std::endl;
}
~Moveable() {
std::cout << "Moveable::~Moveable() 析构函数 ptr(i)=" << i << std::endl;
if (i) {
delete i;
i = nullptr;
}
}
Moveable(const Moveable& m) : i(new int(*m.i)) {
std::cout << "Moveable::Moveable(const Moveable &) 拷贝构造函数 ptr(i)=" << i << std::endl;
}
Moveable(Moveable&& m) {
this->i = m.i;
m.i = nullptr;
std::cout << "Moveable::Moveable(Moveable &&) 移动构造函数 ptr(i)=" << this->i << std::endl;
}
void Set(int x) {
*i = x;
}
const int* Get() {
return i;
}
private:
int* i;
};
int test_move2()
{
// Moveable a;
// a.Set(5);
// const int* ptr=a.Get();
// Moveable b(a);
// std::cout << "a ptr=" << a.Get() << "\n";
// std::cout << "b ptr=" << b.Get() << "\n";
{
Moveable a;
a.Set(5);
const int* ptr=a.Get();
Moveable b(std::move(a));
std::cout << "a ptr=" << a.Get() << "\n";
std::cout << "b ptr=" << b.Get() << "\n";
}
return 1;
// int a = 1;
// ProcessValue(a);
// // std::move函数可以以非常简单的方式将左值引用转换为右值引用
// ProcessValue(std::move(a));
// std::cout << "last a= " << a << std::endl;
// std::string a("test string");
// std::string b(std::move(a));
// std::cout << "a:" << a << std::endl << "b:" << b << std::endl;
return 1;
// int a = 0;
// process_value(a);
// process_value(1);
// std::cout<<"test std::move:\n";
// std::string str5 = "asdf";
// std::string &lr5 = str5;
// std::string &&rr5 = std::move(str5);
// rr5[0] = 'b';
// lr5[1] = 'z';
// std::cout<<rr5<<'\t'<<lr5<<'\t'<<str5<<std::endl;
// std::cout<<"test std::move done.\n"<<std::endl;
// return 0;
}
int test_move1()
{
std::string str = "Hello";
std::vector<std::string> v;
// uses the push_back(const T&) overload, which means we'll incur the cost of copying str
v.push_back(str);
std::cout << "After copy, str is \"" << str << "\"\n";
// uses the rvalue reference push_back(T&&) overload, which means no strings will be copied;
// instead, the contents of str will be moved into the vector.
// This is less expensive, but also means str might now be empty.
v.push_back(std::move(str));
std::cout << "After move, str is \"" << str << "\"\n";
std::cout << "The contents of the vector are \"" << v[0] << "\", \"" << v[1] << "\"\n";
return 0;
}
class Y {// : public boost::enable_shared_from_this<Y> {
public:
boost::shared_ptr<Y> f() {
// return shared_from_this();
return boost::shared_ptr<Y>(this);
}
Y* getthis() {
return this;
}
};
void tst_shared_ptr_1() {
boost::shared_ptr<Y> p(new Y);
std::cout << "p count=" << p.use_count() << std::endl;
boost::shared_ptr<Y> q = p->f();
std::cout << "q count=" << q.use_count() << std::endl;
std::cout << "p count=" << p.use_count() << std::endl;
}
std::string convert(const std::wstring& wstr) {
size_t size = wstr.size();
if (size == 0) {
return "";
}
size_t cs = size * sizeof(wchar_t) + 1;
char c[cs];
memset(c, '\0', cs);
int n = std::wcstombs(c, wstr.c_str(), cs);
// printf( "n=%d c=%s\n", n, c[0] );
if (n > 0) {
printf( "n=%d c=%s\n", n, std::string(c, n).c_str() );
return std::string(c, n);
}
return "";
}
std::string dealExceed128Bytes( const std::string& tip ) {
if ( tip.length() <= 128 )
return tip;
std::string ret_tip;
// 截断128字节数据,且追加后缀 ...
const int FIX_MAX_LEN = 128;
// if (tip.length() > FIX_MAX_LEN) {
// result.promtStKey = "newMSG";
// return;
printf( "tip.length() > 128, tip.length()=%d\n", tip.length() );
// size_t tmplen = tip.length();
wchar_t tmp_wbuffer[FIX_MAX_LEN + 1];
::memset( tmp_wbuffer, 0, sizeof(tmp_wbuffer) );
size_t transferLen = std::mbstowcs(tmp_wbuffer, tip.c_str(), FIX_MAX_LEN);
// LOG_INFO( "transferLen=" << transferLen );
std::wstring ws_tmp = tmp_wbuffer;
// std::wcout << "ws_tmp=" <<ws_tmp << std::endl;
std::string slst = convert(ws_tmp);
// char lstbuffer[ FIX_MAX_LEN*sizeof(wchar_t) + 1 ];
// memset( lstbuffer, 0, FIX_MAX_LEN*sizeof(wchar_t) + 1 );
// std::cout << "aaaaaaaaaa=" << FIX_MAX_LEN*sizeof(wchar_t) + 1 << std::endl;//
// transferLen = std::wcstombs(lstbuffer, tmp_wbuffer, FIX_MAX_LEN*sizeof(wchar_t) + 1);
// std::cout << "lst transferLen=" << transferLen << std::endl;//
// std::cout << "lstbuffer=" << lstbuffer << std::endl;//
if (slst.length() == tip.length()) {
ret_tip += slst;
} else {
ret_tip += slst;
ret_tip += "...";
}
// LOG_DEBUG( "transferLen_1=" << transferLen << "&last_tip=" << ret_tip.c_str() );
// printf( "slst=\n", slst.c_str() );
// printf( "ret_tip.size=%d&last_tip=%s\n", ret_tip.size(), ret_tip.c_str() );
// }
return ret_tip;
}