[PATCH] Add move constructor and move assignment operator for FileName class

Yuriy Skalko yuriy.skalko at gmail.com
Thu Jan 7 00:33:12 UTC 2021


This should reduce memory allocations for this heavily used class.

Yuriy
-------------- next part --------------
From 15c952108bd6e79a712f159326804a145186b50b Mon Sep 17 00:00:00 2001
From: Yuriy Skalko <yuriy.skalko at gmail.com>
Date: Thu, 7 Jan 2021 02:27:31 +0200
Subject: [PATCH] Add move constructor and move assignment operator for
 FileName class

---
 src/support/FileName.cpp | 16 ++++++++++++++++
 src/support/FileName.h   | 14 ++++++++++----
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp
index 5295741e1a..b9a1d4315b 100644
--- a/src/support/FileName.cpp
+++ b/src/support/FileName.cpp
@@ -158,6 +158,13 @@ FileName::FileName(FileName const & rhs) : d(new Private)
 }
 
 
+FileName::FileName(FileName && rhs) noexcept
+	: d(rhs.d)
+{
+	rhs.d = nullptr;
+}
+
+
 FileName::FileName(FileName const & rhs, string const & suffix) : d(new Private)
 {
 	set(rhs, suffix);
@@ -174,6 +181,15 @@ FileName & FileName::operator=(FileName const & rhs)
 }
 
 
+FileName & FileName::operator=(FileName && rhs) noexcept
+{
+	auto temp = rhs.d;
+	rhs.d = d;
+	d = temp;
+	return *this;
+}
+
+
 bool FileName::empty() const
 {
 	return d->name.empty();
diff --git a/src/support/FileName.h b/src/support/FileName.h
index 1cf1e730f4..2bc2e48b80 100644
--- a/src/support/FileName.h
+++ b/src/support/FileName.h
@@ -42,15 +42,21 @@ public:
 	 */
 	explicit FileName(std::string const & abs_filename);
 
-	/// copy constructor.
+	/// copy constructor
 	FileName(FileName const &);
 
-	/// constructor with base name and suffix.
+	/// move constructor
+	FileName(FileName &&) noexcept;
+
+	/// constructor with base name and suffix
 	FileName(FileName const & fn, std::string const & suffix);
 
-	///
+	/// copy assign
 	FileName & operator=(FileName const &);
 
+	/// move assign
+	FileName & operator=(FileName &&) noexcept;
+
 	virtual ~FileName();
 	/** Set a new filename.
 	 * \param filename the file in question. Must have an absolute path.
@@ -219,7 +225,7 @@ private:
 	bool copyTo(FileName const &, bool, FileNameSet &) const;
 	///
 	struct Private;
-	Private * const d;
+	Private * d;
 };
 
 
-- 
2.28.0.windows.1



More information about the lyx-devel mailing list