More unique_ptr

This commit is contained in:
Thomas Goyne 2013-06-11 20:38:12 -07:00
parent b67a0538ff
commit 3306ee126d
5 changed files with 14 additions and 29 deletions

View file

@ -52,33 +52,34 @@ LogSink::LogSink()
LogSink::~LogSink() { LogSink::~LogSink() {
// The destructor for emitters may try to log messages, so disable all the // The destructor for emitters may try to log messages, so disable all the
// emitters before destructing any // emitters before destructing any
std::vector<Emitter*> emitters_temp; decltype(emitters) emitters_temp;
queue->Sync([&]{ swap(emitters_temp, emitters); }); queue->Sync([&]{ swap(emitters_temp, emitters); });
util::delete_clear(emitters_temp);
} }
void LogSink::Log(SinkMessage const& sm) { void LogSink::Log(SinkMessage const& sm) {
queue->Async([=]{ queue->Async([=]{
messages.push_back(sm); 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<Emitter> em) {
LOG_D("agi/log/emitter/subscribe") << "Subscribe: " << this; 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) { void LogSink::Unsubscribe(Emitter *em) {
queue->Sync([=] { queue->Sync([=] {
emitters.erase(remove(emitters.begin(), emitters.end(), em), emitters.end()); emitters.erase(
delete em; boost::remove_if(emitters, [=](std::unique_ptr<Emitter> const& e) { return e.get() == em; }),
emitters.end());
}); });
LOG_D("agi/log/emitter/unsubscribe") << "Un-Subscribe: " << this; LOG_D("agi/log/emitter/unsubscribe") << "Un-Subscribe: " << this;
} }
decltype(LogSink::messages) LogSink::GetMessages() const { decltype(LogSink::messages) LogSink::GetMessages() const {
decltype(LogSink::messages) ret; decltype(messages) ret;
queue->Sync([&] { ret = messages; }); queue->Sync([&] { ret = messages; });
return ret; return ret;
} }

View file

@ -87,7 +87,7 @@ class LogSink {
std::unique_ptr<dispatch::Queue> queue; std::unique_ptr<dispatch::Queue> queue;
/// List of pointers to emitters /// List of pointers to emitters
std::vector<Emitter*> emitters; std::vector<std::unique_ptr<Emitter>> emitters;
public: public:
LogSink(); LogSink();
@ -98,9 +98,7 @@ public:
/// @brief Subscribe an emitter /// @brief Subscribe an emitter
/// @param em Emitter to add /// @param em Emitter to add
/// void Subscribe(std::unique_ptr<Emitter> em);
/// LogSink takes ownership of the passed emitter
void Subscribe(Emitter *em);
/// @brief Unsubscribe and delete an emitter /// @brief Unsubscribe and delete an emitter
/// @param em Emitter to delete /// @param em Emitter to delete

View file

@ -51,20 +51,6 @@ namespace agi {
/// based on the unfolded length. /// based on the unfolded length.
std::pair<size_t, size_t> ifind(std::string const& haystack, std::string const& needle); std::pair<size_t, size_t> ifind(std::string const& haystack, std::string const& needle);
struct delete_ptr {
template<class T>
void operator()(T* ptr) const {
delete ptr;
}
};
template<class T>
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 /// Set the name of the calling thread in the Visual Studio debugger
/// @param name New name for the thread /// @param name New name for the thread
void SetThreadName(const char *name); void SetThreadName(const char *name);

View file

@ -106,7 +106,7 @@ LogWindow::LogWindow(agi::Context *c)
sizer->Add(new wxButton(this, wxID_OK), wxSizerFlags(0).Border().Right()); sizer->Add(new wxButton(this, wxID_OK), wxSizerFlags(0).Border().Right());
SetSizerAndFit(sizer); SetSizerAndFit(sizer);
agi::log::log->Subscribe(emit_log = new EmitLog(text_ctrl)); agi::log::log->Subscribe(std::unique_ptr<agi::log::Emitter>(emit_log = new EmitLog(text_ctrl)));
} }
LogWindow::~LogWindow() { LogWindow::~LogWindow() {

View file

@ -131,7 +131,7 @@ bool AegisubApp::OnInit() {
agi::log::log = new agi::log::LogSink; agi::log::log = new agi::log::LogSink;
#ifdef _DEBUG #ifdef _DEBUG
agi::log::log->Subscribe(new agi::log::EmitSTDOUT()); agi::log::log->Subscribe(agi::util::make_unique<agi::log::EmitSTDOUT>());
#endif #endif
// Set config file // Set config file
@ -155,7 +155,7 @@ bool AegisubApp::OnInit() {
StartupLog("Create log writer"); StartupLog("Create log writer");
auto path_log = config::path->Decode("?user/log/"); auto path_log = config::path->Decode("?user/log/");
agi::fs::CreateDirectory(path_log); agi::fs::CreateDirectory(path_log);
agi::log::log->Subscribe(new agi::log::JsonEmitter(path_log)); agi::log::log->Subscribe(agi::util::make_unique<agi::log::JsonEmitter>(path_log));
CleanCache(path_log, "*.json", 10, 100); CleanCache(path_log, "*.json", 10, 100);
StartupLog("Load user configuration"); StartupLog("Load user configuration");