From 49e1d918043c656dde9500ba7e09de173c8fef4a Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 29 Jun 2012 13:19:29 +0200 Subject: [PATCH] Buffer: pass by value instead of by reference when object will be copied anyway Let copy elision and move semantics kick in --- src/buffer.cc | 14 +++++++------- src/buffer.hh | 14 ++++++-------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 4c29837a..a342ac07 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -21,9 +21,9 @@ T clamp(T min, T max, T val) return val; } -Buffer::Buffer(const String& name, Type type, - const String& initial_content) - : m_name(name), m_type(type), +Buffer::Buffer(String name, Type type, + String initial_content) + : m_name(std::move(name)), m_type(type), m_history(1), m_history_cursor(m_history.begin()), m_last_save_undo_index(0), m_hook_manager(GlobalHookManager::instance()), @@ -31,14 +31,14 @@ Buffer::Buffer(const String& name, Type type, { BufferManager::instance().register_buffer(this); if (not initial_content.empty()) - apply_modification(Modification::make_insert(begin(), initial_content)); + apply_modification(Modification::make_insert(begin(), std::move(initial_content))); if (type == Type::NewFile) - m_hook_manager.run_hook("BufNew", name, Context(*this)); + m_hook_manager.run_hook("BufNew", m_name, Context(*this)); else if (type == Type::File) - m_hook_manager.run_hook("BufOpen", name, Context(*this)); + m_hook_manager.run_hook("BufOpen", m_name, Context(*this)); - m_hook_manager.run_hook("BufCreate", name, Context(*this)); + m_hook_manager.run_hook("BufCreate", m_name, Context(*this)); } Buffer::~Buffer() diff --git a/src/buffer.hh b/src/buffer.hh index 80807efe..5fcf5811 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -91,14 +91,13 @@ struct Modification BufferIterator position; String content; - Modification(Type type, BufferIterator position, const String& content) - : type(type), position(position), content(content) {} + Modification(Type type, BufferIterator position, String content) + : type(type), position(position), content(std::move(content)) {} Modification inverse() const; static Modification make_erase(BufferIterator begin, BufferIterator end); - static Modification make_insert(BufferIterator position, - const String& content); + static Modification make_insert(BufferIterator position, String content); }; // A Buffer is a in-memory representation of a file @@ -116,8 +115,7 @@ public: Scratch }; - Buffer(const String& name, Type type, - const String& initial_content = "\n"); + Buffer(String name, Type type, String initial_content = "\n"); Buffer(const Buffer&) = delete; Buffer(Buffer&&) = delete; Buffer& operator= (const Buffer&) = delete; @@ -225,9 +223,9 @@ inline Modification Modification::make_erase(BufferIterator begin, } inline Modification Modification::make_insert(BufferIterator position, - const String& content) + String content) { - return Modification(Insert, position, content); + return Modification(Insert, position, std::move(content)); } }