From 3306ee126d9a63b03e3d0bfdd052402daf3e5dad Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Tue, 11 Jun 2013 20:38:12 -0700 Subject: [PATCH] More unique_ptr --- aegisub/libaegisub/common/log.cpp | 17 +++++++++-------- aegisub/libaegisub/include/libaegisub/log.h | 6 ++---- aegisub/libaegisub/include/libaegisub/util.h | 14 -------------- aegisub/src/dialog_log.cpp | 2 +- aegisub/src/main.cpp | 4 ++-- 5 files changed, 14 insertions(+), 29 deletions(-) diff --git a/aegisub/libaegisub/common/log.cpp b/aegisub/libaegisub/common/log.cpp index 80fbf2680..912eb4485 100644 --- a/aegisub/libaegisub/common/log.cpp +++ b/aegisub/libaegisub/common/log.cpp @@ -52,33 +52,34 @@ LogSink::LogSink() LogSink::~LogSink() { // The destructor for emitters may try to log messages, so disable all the // emitters before destructing any - std::vector emitters_temp; + decltype(emitters) emitters_temp; queue->Sync([&]{ swap(emitters_temp, emitters); }); - util::delete_clear(emitters_temp); } void LogSink::Log(SinkMessage const& sm) { queue->Async([=]{ messages.push_back(sm); - boost::for_each(emitters, [=](Emitter *em) { em->log(&messages.back()); }); + for (auto& em : emitters) em->log(&messages.back()); }); } -void LogSink::Subscribe(Emitter *em) { +void LogSink::Subscribe(std::unique_ptr em) { LOG_D("agi/log/emitter/subscribe") << "Subscribe: " << this; - queue->Sync([=] { emitters.push_back(em); }); + auto tmp = em.release(); + queue->Sync([=] { emitters.emplace_back(tmp); }); } void LogSink::Unsubscribe(Emitter *em) { queue->Sync([=] { - emitters.erase(remove(emitters.begin(), emitters.end(), em), emitters.end()); - delete em; + emitters.erase( + boost::remove_if(emitters, [=](std::unique_ptr const& e) { return e.get() == em; }), + emitters.end()); }); LOG_D("agi/log/emitter/unsubscribe") << "Un-Subscribe: " << this; } decltype(LogSink::messages) LogSink::GetMessages() const { - decltype(LogSink::messages) ret; + decltype(messages) ret; queue->Sync([&] { ret = messages; }); return ret; } diff --git a/aegisub/libaegisub/include/libaegisub/log.h b/aegisub/libaegisub/include/libaegisub/log.h index 061faf297..bfa1bfb50 100644 --- a/aegisub/libaegisub/include/libaegisub/log.h +++ b/aegisub/libaegisub/include/libaegisub/log.h @@ -87,7 +87,7 @@ class LogSink { std::unique_ptr queue; /// List of pointers to emitters - std::vector emitters; + std::vector> emitters; public: LogSink(); @@ -98,9 +98,7 @@ public: /// @brief Subscribe an emitter /// @param em Emitter to add - /// - /// LogSink takes ownership of the passed emitter - void Subscribe(Emitter *em); + void Subscribe(std::unique_ptr em); /// @brief Unsubscribe and delete an emitter /// @param em Emitter to delete diff --git a/aegisub/libaegisub/include/libaegisub/util.h b/aegisub/libaegisub/include/libaegisub/util.h index d663cdabc..602522617 100644 --- a/aegisub/libaegisub/include/libaegisub/util.h +++ b/aegisub/libaegisub/include/libaegisub/util.h @@ -51,20 +51,6 @@ namespace agi { /// based on the unfolded length. std::pair ifind(std::string const& haystack, std::string const& needle); - struct delete_ptr { - template - void operator()(T* ptr) const { - delete ptr; - } - }; - template - void delete_clear(T& container) { - if (!container.empty()) { - std::for_each(container.begin(), container.end(), delete_ptr()); - container.clear(); - } - } - /// Set the name of the calling thread in the Visual Studio debugger /// @param name New name for the thread void SetThreadName(const char *name); diff --git a/aegisub/src/dialog_log.cpp b/aegisub/src/dialog_log.cpp index e9dee32a0..e138554e3 100644 --- a/aegisub/src/dialog_log.cpp +++ b/aegisub/src/dialog_log.cpp @@ -106,7 +106,7 @@ LogWindow::LogWindow(agi::Context *c) sizer->Add(new wxButton(this, wxID_OK), wxSizerFlags(0).Border().Right()); SetSizerAndFit(sizer); - agi::log::log->Subscribe(emit_log = new EmitLog(text_ctrl)); + agi::log::log->Subscribe(std::unique_ptr(emit_log = new EmitLog(text_ctrl))); } LogWindow::~LogWindow() { diff --git a/aegisub/src/main.cpp b/aegisub/src/main.cpp index 02683b04a..7151b0433 100644 --- a/aegisub/src/main.cpp +++ b/aegisub/src/main.cpp @@ -131,7 +131,7 @@ bool AegisubApp::OnInit() { agi::log::log = new agi::log::LogSink; #ifdef _DEBUG - agi::log::log->Subscribe(new agi::log::EmitSTDOUT()); + agi::log::log->Subscribe(agi::util::make_unique()); #endif // Set config file @@ -155,7 +155,7 @@ bool AegisubApp::OnInit() { StartupLog("Create log writer"); auto path_log = config::path->Decode("?user/log/"); agi::fs::CreateDirectory(path_log); - agi::log::log->Subscribe(new agi::log::JsonEmitter(path_log)); + agi::log::log->Subscribe(agi::util::make_unique(path_log)); CleanCache(path_log, "*.json", 10, 100); StartupLog("Load user configuration");