summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Chevrier <marc.chevrier@gmail.com>2019-12-18 17:37:05 (GMT)
committerMarc Chevrier <marc.chevrier@gmail.com>2019-12-19 10:15:17 (GMT)
commitdc2daa6860285be066813d24ecca88f61ae3e18a (patch)
treeefedf084c371b60515a3e71f5d50c03ca64d55b9
parent23130c539fc6ae1f4809e4c639a1a15a5f299c48 (diff)
downloadCMake-dc2daa6860285be066813d24ecca88f61ae3e18a.zip
CMake-dc2daa6860285be066813d24ecca88f61ae3e18a.tar.gz
CMake-dc2daa6860285be066813d24ecca88f61ae3e18a.tar.bz2
cmFileMonitor: modernize memory management
-rw-r--r--Source/cmFileMonitor.cxx35
-rw-r--r--Source/cmFileMonitor.h3
2 files changed, 17 insertions, 21 deletions
diff --git a/Source/cmFileMonitor.cxx b/Source/cmFileMonitor.cxx
index ac8a37e..8cfdb2d 100644
--- a/Source/cmFileMonitor.cxx
+++ b/Source/cmFileMonitor.cxx
@@ -7,9 +7,9 @@
#include <unordered_map>
#include <utility>
-#include "cmsys/SystemTools.hxx"
+#include <cm/memory>
-#include "cmAlgorithms.h"
+#include "cmsys/SystemTools.hxx"
namespace {
void on_directory_change(uv_fs_event_t* handle, const char* filename,
@@ -37,12 +37,12 @@ public:
class cmVirtualDirectoryWatcher : public cmIBaseWatcher
{
public:
- ~cmVirtualDirectoryWatcher() override { cmDeleteAll(this->Children); }
+ ~cmVirtualDirectoryWatcher() override = default;
cmIBaseWatcher* Find(const std::string& ps)
{
const auto i = this->Children.find(ps);
- return (i == this->Children.end()) ? nullptr : i->second;
+ return (i == this->Children.end()) ? nullptr : i->second.get();
}
void Trigger(const std::string& pathSegment, int events,
@@ -96,11 +96,7 @@ public:
return result;
}
- void Reset()
- {
- cmDeleteAll(this->Children);
- this->Children.clear();
- }
+ void Reset() { this->Children.clear(); }
void AddChildWatcher(const std::string& ps, cmIBaseWatcher* watcher)
{
@@ -108,11 +104,12 @@ public:
assert(this->Children.find(ps) == this->Children.end());
assert(watcher);
- this->Children.emplace(std::make_pair(ps, watcher));
+ this->Children.emplace(ps, std::unique_ptr<cmIBaseWatcher>(watcher));
}
private:
- std::unordered_map<std::string, cmIBaseWatcher*> Children; // owned!
+ std::unordered_map<std::string, std::unique_ptr<cmIBaseWatcher>>
+ Children; // owned!
};
// Root of all the different (on windows!) root directories:
@@ -295,14 +292,11 @@ void on_fs_close(uv_handle_t* handle)
} // namespace
cmFileMonitor::cmFileMonitor(uv_loop_t* l)
- : Root(new cmRootWatcher(l))
+ : Root(cm::make_unique<cmRootWatcher>(l))
{
}
-cmFileMonitor::~cmFileMonitor()
-{
- delete this->Root;
-}
+cmFileMonitor::~cmFileMonitor() = default;
void cmFileMonitor::MonitorPaths(const std::vector<std::string>& paths,
Callback const& cb)
@@ -316,7 +310,7 @@ void cmFileMonitor::MonitorPaths(const std::vector<std::string>& paths,
if (segmentCount < 2) { // Expect at least rootdir and filename
continue;
}
- cmVirtualDirectoryWatcher* currentWatcher = this->Root;
+ cmVirtualDirectoryWatcher* currentWatcher = this->Root.get();
for (size_t i = 0; i < segmentCount; ++i) {
assert(currentWatcher);
@@ -334,11 +328,12 @@ void cmFileMonitor::MonitorPaths(const std::vector<std::string>& paths,
cmIBaseWatcher* nextWatcher = currentWatcher->Find(currentSegment);
if (!nextWatcher) {
if (rootSegment) { // Root part
- assert(currentWatcher == this->Root);
- nextWatcher = new cmRootDirectoryWatcher(this->Root, currentSegment);
+ assert(currentWatcher == this->Root.get());
+ nextWatcher =
+ new cmRootDirectoryWatcher(this->Root.get(), currentSegment);
assert(currentWatcher->Find(currentSegment) == nextWatcher);
} else if (fileSegment) { // File part
- assert(currentWatcher != this->Root);
+ assert(currentWatcher != this->Root.get());
nextWatcher = new cmFileWatcher(
dynamic_cast<cmRealDirectoryWatcher*>(currentWatcher),
currentSegment, cb);
diff --git a/Source/cmFileMonitor.h b/Source/cmFileMonitor.h
index 7ffc929..b510a2c 100644
--- a/Source/cmFileMonitor.h
+++ b/Source/cmFileMonitor.h
@@ -5,6 +5,7 @@
#include "cmConfigure.h" // IWYU pragma: keep
#include <functional>
+#include <memory>
#include <string>
#include <vector>
@@ -30,5 +31,5 @@ public:
std::vector<std::string> WatchedDirectories() const;
private:
- cmRootWatcher* Root;
+ std::unique_ptr<cmRootWatcher> Root;
};