Skip to content

Commit 044f4d0

Browse files
committed
pull ScopeExit into its own file
1 parent 4af9084 commit 044f4d0

5 files changed

Lines changed: 43 additions & 31 deletions

File tree

Common/Cpp/ScopeExit.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* ScopeExit
2+
*
3+
* From: https://github.com/PokemonAutomation/Arduino-Source
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_ScopeExit_h
8+
#define PokemonAutomation_ScopeExit_h
9+
10+
#include <functional>
11+
12+
namespace PokemonAutomation{
13+
class ScopeExit{
14+
ScopeExit(const ScopeExit&) = delete;
15+
void operator=(const ScopeExit&) = delete;
16+
17+
public:
18+
template <typename Lambda>
19+
ScopeExit(Lambda&& lambda)
20+
: m_lambda(std::move(lambda))
21+
{}
22+
~ScopeExit(){
23+
m_lambda();
24+
}
25+
26+
private:
27+
std::function<void()> m_lambda;
28+
};
29+
30+
}
31+
#endif

SerialPrograms/Source/CommonFramework/Main.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Common/Cpp/Concurrency/PeriodicRunner.h"
1212
#include "Common/Cpp/Exceptions.h"
1313
#include "Common/Cpp/ImageResolution.h"
14+
#include "Common/Cpp/ScopeExit.h"
1415
#include "Common/Qt/GlobalThreadPoolsQt.h"
1516
#include "StaticRegistration.h"
1617
#include "CommonFramework/Tools/GlobalThreadPools.h"
@@ -60,23 +61,6 @@ void set_working_directory(){
6061
}
6162

6263

63-
class ScopeExit{
64-
ScopeExit(const ScopeExit&) = delete;
65-
void operator=(const ScopeExit&) = delete;
66-
67-
public:
68-
template <typename Lambda>
69-
ScopeExit(Lambda&& lambda)
70-
: m_lambda(std::move(lambda))
71-
{}
72-
~ScopeExit(){
73-
m_lambda();
74-
}
75-
76-
private:
77-
std::function<void()> m_lambda;
78-
};
79-
8064

8165
int run_program(int argc, char *argv[]){
8266
#if defined(__APPLE__)

SerialPrograms/Source/CommonFramework/ResourceDownload/DownloadThread.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "CommonFramework/Globals.h"
99
#include "CommonFramework/Exceptions/OperationFailedException.h"
1010
#include "Common/Cpp/Filesystem.h"
11+
#include "Common/Cpp/ScopeExit.h"
1112
#include "CommonFramework/Tools/FileDownloader.h"
1213
#include "CommonFramework/Tools/FileUnzip.h"
1314
#include "CommonFramework/Tools/FileHash.h"
@@ -51,14 +52,9 @@ void DownloadThread::start_download_thread(){
5152
}
5253

5354
// runs when lambda is finished
54-
// updates action state, removes self from download queue
55-
struct ScopeGuard {
56-
DownloadThread* thread_ptr;
57-
bool& success_ref;
58-
~ScopeGuard() {
59-
thread_ptr->m_hooks.on_finished(success_ref);
60-
}
61-
} guard{this, success};
55+
ScopeExit on_exit([&, this]{
56+
m_hooks.on_finished(success);
57+
});
6258

6359
try {
6460
// std::this_thread::sleep_for(std::chrono::seconds(7));
@@ -78,10 +74,10 @@ void DownloadThread::start_download_thread(){
7874
}catch (const std::exception& e) {
7975
std::cout << "Standard exception: " << e.what() << std::endl;
8076
success = false;
81-
m_hooks.report_exception_caught("ResourceDownloadButton::start_download");
77+
m_hooks.report_exception_caught("DownloadThread::start_download_thread");
8278
} catch(...){
8379
success = false;
84-
m_hooks.report_exception_caught("ResourceDownloadButton::start_download");
80+
m_hooks.report_exception_caught("DownloadThread::start_download_thread");
8581
}
8682

8783
}

SerialPrograms/Source/CommonFramework/Tools/FileUnzip.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "miniz-3.1.1/miniz.h"
1717
#include "Common/Cpp/Filesystem.h"
1818
#include "Common/Cpp/Exceptions.h"
19+
#include "Common/Cpp/ScopeExit.h"
1920
#include "FileUnzip.h"
2021
#include <filesystem>
2122
#include <fstream>
@@ -130,10 +131,9 @@ void unzip_file(
130131
}
131132

132133
// This automatically calls mz_zip_reader_end when this function exits for any reason.
133-
struct ZipCleanup {
134-
mz_zip_archive* p;
135-
~ZipCleanup() { mz_zip_reader_end(p); }
136-
} cleanup{&zip_archive};
134+
ScopeExit on_exit([&]{
135+
mz_zip_reader_end(&zip_archive);
136+
});
137137

138138
// Get total number of files in the archive
139139
int num_files = (int)mz_zip_reader_get_num_files(&zip_archive);

SerialPrograms/cmake/SourceFiles.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ file(GLOB LIBRARY_SOURCES
187187
../Common/Cpp/Rectangle.h
188188
../Common/Cpp/Rectangle.tpp
189189
../Common/Cpp/RecursiveThrottler.h
190+
../Common/Cpp/ScopeExit.h
190191
../Common/Cpp/SIMDDebuggers.h
191192
../Common/Cpp/SerialConnection/SerialConnection.cpp
192193
../Common/Cpp/SerialConnection/SerialConnection.h

0 commit comments

Comments
 (0)