仿autoptr利用栈对象自动运行互斥代码对称代码,
在类构造时运行fNew函数,在类析构时运行fDel函数
class AutoRunSomeCode
{
public:
AutoRunSomeCode(std::function<void()> fNew, std::function<void()> fDel)
:FuncByNew(fNew), FuncByDelete(fDel)
{
FuncByNew();
}
~AutoRunSomeCode()
{
FuncByDelete();
};
protected:
std::function<void()> FuncByNew;
std::function<void()> FuncByDelete;
};
#define AUTO_RUN_SOME_CODE(X, Y) AutoRunSomeCode arsc([=](){X}, [=](){Y})
函数开始时将鼠标光标设为繁忙状态,函数结束时自动还原鼠标状态
void MainWindow::on_pushButton_2_clicked()
{
AUTO_RUN_SOME_CODE
({
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
},{
QApplication::restoreOverrideCursor();
});
QDialog pdlg2;
pdlg2.setWindowTitle("bbbbbb");
pdlg2.exec();
}