Buffer: pass by value instead of by reference when object will be copied anyway

Let copy elision and move semantics kick in
This commit is contained in:
Maxime Coste 2012-06-29 13:19:29 +02:00
parent 36e4dacdf5
commit 49e1d91804
2 changed files with 13 additions and 15 deletions

View File

@ -21,9 +21,9 @@ T clamp(T min, T max, T val)
return val; return val;
} }
Buffer::Buffer(const String& name, Type type, Buffer::Buffer(String name, Type type,
const String& initial_content) String initial_content)
: m_name(name), m_type(type), : m_name(std::move(name)), m_type(type),
m_history(1), m_history_cursor(m_history.begin()), m_history(1), m_history_cursor(m_history.begin()),
m_last_save_undo_index(0), m_last_save_undo_index(0),
m_hook_manager(GlobalHookManager::instance()), m_hook_manager(GlobalHookManager::instance()),
@ -31,14 +31,14 @@ Buffer::Buffer(const String& name, Type type,
{ {
BufferManager::instance().register_buffer(this); BufferManager::instance().register_buffer(this);
if (not initial_content.empty()) 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) 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) 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() Buffer::~Buffer()

View File

@ -91,14 +91,13 @@ struct Modification
BufferIterator position; BufferIterator position;
String content; String content;
Modification(Type type, BufferIterator position, const String& content) Modification(Type type, BufferIterator position, String content)
: type(type), position(position), content(content) {} : type(type), position(position), content(std::move(content)) {}
Modification inverse() const; Modification inverse() const;
static Modification make_erase(BufferIterator begin, BufferIterator end); static Modification make_erase(BufferIterator begin, BufferIterator end);
static Modification make_insert(BufferIterator position, static Modification make_insert(BufferIterator position, String content);
const String& content);
}; };
// A Buffer is a in-memory representation of a file // A Buffer is a in-memory representation of a file
@ -116,8 +115,7 @@ public:
Scratch Scratch
}; };
Buffer(const String& name, Type type, Buffer(String name, Type type, String initial_content = "\n");
const String& initial_content = "\n");
Buffer(const Buffer&) = delete; Buffer(const Buffer&) = delete;
Buffer(Buffer&&) = delete; Buffer(Buffer&&) = delete;
Buffer& operator= (const 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, inline Modification Modification::make_insert(BufferIterator position,
const String& content) String content)
{ {
return Modification(Insert, position, content); return Modification(Insert, position, std::move(content));
} }
} }