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() {
// The destructor for emitters may try to log messages, so disable all the
// emitters before destructing any
std::vector<Emitter*> 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<Emitter> 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<Emitter> 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;
}

View File

@ -87,7 +87,7 @@ class LogSink {
std::unique_ptr<dispatch::Queue> queue;
/// List of pointers to emitters
std::vector<Emitter*> emitters;
std::vector<std::unique_ptr<Emitter>> 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<Emitter> em);
/// @brief Unsubscribe and delete an emitter
/// @param em Emitter to delete

View File

@ -51,20 +51,6 @@ namespace agi {
/// based on the unfolded length.
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
/// @param name New name for the thread
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());
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() {

View File

@ -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<agi::log::EmitSTDOUT>());
#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<agi::log::JsonEmitter>(path_log));
CleanCache(path_log, "*.json", 10, 100);
StartupLog("Load user configuration");