定义宏删除一些构造函数

1
2
3
4
5
6
7
8
9
10
11
#define DISABLE_COPY(Class) \
Class(const Class &) = delete; \
Class &operator=(const Class &) = delete;

#define DISABLE_MOVE(Class) \
Class(Class &&) = delete; \
Class &operator=(Class &&) = delete;

#define DISABLE_COPY_MOVE(Class) \
DISABLE_COPY(Class) \
DISABLE_MOVE(Class)
阅读全文 »

muduo中的回调函数模板

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
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)

#ifndef MUDUO_BASE_WEAKCALLBACK_H
#define MUDUO_BASE_WEAKCALLBACK_H

#include <functional>
#include <memory>

namespace muduo
{

// A barely usable WeakCallback

template<typename CLASS, typename... ARGS>
class WeakCallback
{
public:

WeakCallback(const std::weak_ptr<CLASS>& object,
const std::function<void (CLASS*, ARGS...)>& function)
: object_(object), function_(function)
{
}

// Default dtor, copy ctor and assignment are okay

void operator()(ARGS&&... args) const
{
std::shared_ptr<CLASS> ptr(object_.lock());
if (ptr)
{
function_(ptr.get(), std::forward<ARGS>(args)...);
}
// else
// {
// LOG_TRACE << "expired";
// }
}

private:

std::weak_ptr<CLASS> object_;
std::function<void (CLASS*, ARGS...)> function_;
};

template<typename CLASS, typename... ARGS>
WeakCallback<CLASS, ARGS...> makeWeakCallback(const std::shared_ptr<CLASS>& object,
void (CLASS::*function)(ARGS...))
{
return WeakCallback<CLASS, ARGS...>(object, function);
}

template<typename CLASS, typename... ARGS>
WeakCallback<CLASS, ARGS...> makeWeakCallback(const std::shared_ptr<CLASS>& object,
void (CLASS::*function)(ARGS...) const)
{
return WeakCallback<CLASS, ARGS...>(object, function);
}

} // namespace muduo

#endif // MUDUO_BASE_WEAKCALLBACK_H

对于类成员函数注册的回调函数,一定要在该实例的生命周期内调用

阅读全文 »

经过一些测试,网络位置是位于 %AppData%\Microsoft\Windows\Network Shortcuts 文件夹中的只读文件夹,其中包含两个文件:包含精确内容的 desktop.ini(参见代码)和 target.lnk 目标的捷径。

阅读全文 »

botan

一、RSA

1.公钥加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <botan/auto_rng.h>
#include <botan/data_src.h>
#include <botan/pubkey.h>
#include <botan/x509_key.h>

bool encryptByRSA(const QString &paramDataSource, QByteArray &paramDataDest, const QString &pubKey)
{
try {
Botan::AutoSeeded_RNG rng;
Botan::DataSource_Memory pem(pubKey.toStdString());
std::unique_ptr<Botan::X509_PublicKey> temp_pub(Botan::X509::load_key(pem));
//X509_PublicKey* temp_pub = X509::load_key(paramPubKeyPath.toStdString());
Botan::PK_Encryptor_EME enc(*temp_pub, rng, "EME-PKCS1-v1_5");
std::vector<uint8_t> en = enc.encrypt((unsigned char *) paramDataSource.toUtf8().data(),
paramDataSource.toUtf8().size(),
rng);
paramDataDest = QByteArray::fromRawData((char *) &en[0], en.size()).toBase64();
return true;
} catch (std::runtime_error &e) {
qWarning() << e.what();
return false;
}
}

问题描述

由于QFileDialog仅支持多选文件或者单选目录,但是有些场景又需要文件文件夹混合多选。

Qt文档

The QFileDialog class enables a user to traverse the file system in order to select one or many files or a directory.

机翻

QFileDialog 类使用户能够遍历文件系统以选择一个或多个文件或目录。

阅读全文 »

值语义

对象的拷贝与原对象无关。拷贝之后就与原对象脱离关系,彼此独立互不影响(深拷贝)。比如说int,C++中的内置类型都是值语义,标准库类型string、vector、map等等也是值语义。

对象语义

对象拷贝是禁止的(Noncopyable) 或者一个对象被系统标准的复制方式复制后,与被复制的对象之间依然共享底层资源,对任何一个的改变都将改变另一个(浅拷贝)。

阅读全文 »

日志打印:Release下打印行号和文件名

The class provides information about the source code location a qDebug(), qInfo(), qWarning(), qCritical() or qFatal() message was generated.
Note: By default, this information is recorded only in debug builds. You can overwrite this explicitly by defining QT_MESSAGELOGCONTEXT or QT_NO_MESSAGELOGCONTEXT.

pro或者pri文件内加入

1
DEFINES += QT_MESSAGELOGCONTEXT
阅读全文 »

QIODevice在一个死循环(一般在子线程)内读写,会引发内存泄漏,如下代码

1
2
3
4
5
6
QIODevice ioDevice;
while (1) {
// 其他操作
ioDevice.write(buf); // 此处会引发内存泄露,虽然增长比较缓慢
QThread::msleep(10);
}
阅读全文 »