move Window ownership to the ClientManager instead of the Buffer

This commit is contained in:
Maxime Coste 2012-11-22 14:08:55 +01:00
parent 3b5530ac09
commit 08ad8e8a40
5 changed files with 16 additions and 33 deletions

View File

@ -43,7 +43,6 @@ Buffer::~Buffer()
{ {
m_hooks.run_hook("BufClose", m_name, Context(Editor(*this))); m_hooks.run_hook("BufClose", m_name, Context(Editor(*this)));
m_windows.clear();
BufferManager::instance().unregister_buffer(*this); BufferManager::instance().unregister_buffer(*this);
assert(m_change_listeners.empty()); assert(m_change_listeners.empty());
} }
@ -388,20 +387,6 @@ void Buffer::erase(BufferIterator begin, BufferIterator end)
do_erase(begin, end); do_erase(begin, end);
} }
Window& Buffer::new_window()
{
m_windows.emplace_back(new Window(*this));
return *m_windows.back();
}
void Buffer::delete_window(Window& window)
{
assert(&window.buffer() == this);
auto window_it = std::find(m_windows.begin(), m_windows.end(), &window);
assert(window_it != m_windows.end());
m_windows.erase(window_it);
}
bool Buffer::is_modified() const bool Buffer::is_modified() const
{ {
size_t history_cursor_index = m_history_cursor - m_history.begin(); size_t history_cursor_index = m_history_cursor - m_history.begin();

View File

@ -15,7 +15,6 @@ namespace Kakoune
{ {
class Buffer; class Buffer;
class Window;
struct BufferCoord : LineAndColumn<BufferCoord, LineCount, ByteCount> struct BufferCoord : LineAndColumn<BufferCoord, LineCount, ByteCount>
{ {
@ -145,12 +144,6 @@ public:
const String& name() const { return m_name; } const String& name() const { return m_name; }
// Window handling
using WindowList = std::vector<std::unique_ptr<Window>>;
const WindowList& windows() const { return m_windows; }
Window& new_window();
void delete_window(Window& window);
// returns true if the buffer is in a different state than // returns true if the buffer is in a different state than
// the last time it was saved // the last time it was saved
bool is_modified() const; bool is_modified() const;
@ -221,13 +214,11 @@ private:
void apply_modification(const Modification& modification); void apply_modification(const Modification& modification);
void revert_modification(const Modification& modification); void revert_modification(const Modification& modification);
WindowList m_windows;
size_t m_last_save_undo_index; size_t m_last_save_undo_index;
size_t m_timestamp; size_t m_timestamp;
// this mutable as adding or removing listeners is not muting the buffer // this is mutable as adding or removing listeners is not muting the
// observable state. // buffer observable state.
mutable std::vector<BufferChangeListener*> m_change_listeners; mutable std::vector<BufferChangeListener*> m_change_listeners;
OptionManager m_options; OptionManager m_options;

View File

@ -49,10 +49,13 @@ void ClientManager::remove_client_by_context(Context& context)
assert(false); assert(false);
} }
Window& ClientManager::get_unused_window_for_buffer(Buffer& buffer) const Window& ClientManager::get_unused_window_for_buffer(Buffer& buffer)
{ {
for (auto& w : buffer.windows()) for (auto& w : m_windows)
{ {
if (&w->buffer() != &buffer)
continue;
auto it = std::find_if(m_clients.begin(), m_clients.end(), auto it = std::find_if(m_clients.begin(), m_clients.end(),
[&](const Client& client) { [&](const Client& client) {
return &client.context->window() == w.get(); return &client.context->window() == w.get();
@ -63,7 +66,8 @@ Window& ClientManager::get_unused_window_for_buffer(Buffer& buffer) const
return *w; return *w;
} }
} }
return buffer.new_window(); m_windows.emplace_back(new Window(buffer));
return *m_windows.back();
} }
void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer) void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer)
@ -88,6 +92,10 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer)
} }
} }
} }
auto end = std::remove_if(m_windows.begin(), m_windows.end(),
[&buffer](std::unique_ptr<Window>& w)
{ return &w->buffer() == &buffer; });
m_windows.erase(end, m_windows.end());
} }
void ClientManager::redraw_clients() const void ClientManager::redraw_clients() const

View File

@ -18,7 +18,7 @@ public:
bool empty() const { return m_clients.empty(); } bool empty() const { return m_clients.empty(); }
size_t count() const { return m_clients.size(); } size_t count() const { return m_clients.size(); }
Window& get_unused_window_for_buffer(Buffer& buffer) const; Window& get_unused_window_for_buffer(Buffer& buffer);
void ensure_no_client_uses_buffer(Buffer& buffer); void ensure_no_client_uses_buffer(Buffer& buffer);
void redraw_clients() const; void redraw_clients() const;
@ -50,6 +50,7 @@ private:
}; };
std::vector<Client> m_clients; std::vector<Client> m_clients;
std::vector<std::unique_ptr<Window>> m_windows;
}; };
} }

View File

@ -22,6 +22,7 @@ class HighlighterGroup;
class Window : public Editor, public OptionManagerWatcher class Window : public Editor, public OptionManagerWatcher
{ {
public: public:
Window(Buffer& buffer);
~Window(); ~Window();
const DisplayCoord& position() const { return m_position; } const DisplayCoord& position() const { return m_position; }
@ -50,9 +51,6 @@ public:
void forget_timestamp() { m_timestamp = -1; } void forget_timestamp() { m_timestamp = -1; }
private: private:
friend class Buffer;
Window(Buffer& buffer);
Window(const Window&) = delete; Window(const Window&) = delete;
void on_incremental_insertion_end() override; void on_incremental_insertion_end() override;