Sfoglia il codice sorgente

LibCore: Allow moving, but not copying, DirIterator

An explicit move constructor is required to null-out the moved-from
directory descriptor. Otherwise, we would call closedir() twice when
using ErrorOr<DirIterator>::release_value().
Timothy Flynn 3 anni fa
parent
commit
7f780e43a6

+ 10 - 0
Userland/Libraries/LibCore/DirIterator.cpp

@@ -29,6 +29,16 @@ DirIterator::~DirIterator()
     }
     }
 }
 }
 
 
+DirIterator::DirIterator(DirIterator&& other)
+    : m_dir(other.m_dir)
+    , m_error(other.m_error)
+    , m_next(move(other.m_next))
+    , m_path(move(other.m_path))
+    , m_flags(other.m_flags)
+{
+    other.m_dir = nullptr;
+}
+
 bool DirIterator::advance_next()
 bool DirIterator::advance_next()
 {
 {
     if (!m_dir)
     if (!m_dir)

+ 3 - 0
Userland/Libraries/LibCore/DirIterator.h

@@ -23,6 +23,9 @@ public:
     explicit DirIterator(String path, Flags = Flags::NoFlags);
     explicit DirIterator(String path, Flags = Flags::NoFlags);
     ~DirIterator();
     ~DirIterator();
 
 
+    DirIterator(DirIterator&&);
+    DirIterator(DirIterator const&) = delete;
+
     bool has_error() const { return m_error != 0; }
     bool has_error() const { return m_error != 0; }
     int error() const { return m_error; }
     int error() const { return m_error; }
     const char* error_string() const { return strerror(m_error); }
     const char* error_string() const { return strerror(m_error); }