From b63c658da569b4d3542e2078c818e8bfe16cdc4e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 10 Dec 2012 18:55:11 +0100 Subject: [PATCH] remote.cc: move write function as member of Message --- src/remote.cc | 120 +++++++++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/src/remote.cc b/src/remote.cc index bf6300a6..72528a38 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -38,55 +38,55 @@ public: m_stream.insert(m_stream.end(), val, val + size); } + template + void write(const T& val) + { + write((const char*)&val, sizeof(val)); + }; + + void write(const String& str) + { + write(str.length()); + write(str.c_str(), (int)str.length()); + }; + + template + void write(const memoryview& view) + { + write(view.size()); + for (auto& val : view) + write(val); + }; + + template + void write(const std::vector& vec) + { + write(memoryview(vec)); + } + + void write(const DisplayAtom& atom) + { + write(atom.fg_color); + write(atom.bg_color); + write(atom.attribute); + write(atom.content.content()); + } + + void write(const DisplayLine& line) + { + write(line.atoms()); + } + + void write(const DisplayBuffer& display_buffer) + { + write(display_buffer.lines()); + } + private: std::vector m_stream; int m_socket; }; -template -void write(Message& msg, const T& val) -{ - msg.write((const char*)&val, sizeof(val)); -}; - -void write(Message& msg, const String& str) -{ - write(msg, str.length()); - msg.write(str.c_str(), (int)str.length()); -}; - -template -void write(Message& msg, const memoryview& view) -{ - write(msg, view.size()); - for (auto& val : view) - write(msg, val); -}; - -template -void write(Message& msg, const std::vector& vec) -{ - write(msg, memoryview(vec)); -} - -void write(Message& msg, const DisplayAtom& atom) -{ - write(msg, atom.fg_color); - write(msg, atom.bg_color); - write(msg, atom.attribute); - write(msg, atom.content.content()); -} - -void write(Message& msg, const DisplayLine& line) -{ - write(msg, line.atoms()); -} - -void write(Message& msg, const DisplayBuffer& display_buffer) -{ - write(msg, display_buffer.lines()); -} - void read(int socket, char* buffer, size_t size) { while (size) @@ -173,41 +173,41 @@ RemoteUI::~RemoteUI() void RemoteUI::print_status(const String& status, CharCount cursor_pos) { Message msg(m_socket); - write(msg, RemoteUIMsg::PrintStatus); - write(msg, status); - write(msg, cursor_pos); + msg.write(RemoteUIMsg::PrintStatus); + msg.write(status); + msg.write(cursor_pos); } void RemoteUI::menu_show(const memoryview& choices, const DisplayCoord& anchor, MenuStyle style) { Message msg(m_socket); - write(msg, RemoteUIMsg::MenuShow); - write(msg, choices); - write(msg, anchor); - write(msg, style); + msg.write(RemoteUIMsg::MenuShow); + msg.write(choices); + msg.write(anchor); + msg.write(style); } void RemoteUI::menu_select(int selected) { Message msg(m_socket); - write(msg, RemoteUIMsg::MenuSelect); - write(msg, selected); + msg.write(RemoteUIMsg::MenuSelect); + msg.write(selected); } void RemoteUI::menu_hide() { Message msg(m_socket); - write(msg, RemoteUIMsg::MenuHide); + msg.write(RemoteUIMsg::MenuHide); } void RemoteUI::draw(const DisplayBuffer& display_buffer, const String& mode_line) { Message msg(m_socket); - write(msg, RemoteUIMsg::Draw); - write(msg, display_buffer); - write(msg, mode_line); + msg.write(RemoteUIMsg::Draw); + msg.write(display_buffer); + msg.write(mode_line); } static const Key::Modifiers resize_modifier = (Key::Modifiers)0x80; @@ -247,7 +247,7 @@ RemoteClient::RemoteClient(int socket, UserInterface* ui) { Key key{ resize_modifier, Codepoint(((int)m_dimensions.line << 16) | (int)m_dimensions.column) }; Message msg(socket); - write(msg, key); + msg.write(key); } void RemoteClient::process_next_message() @@ -291,14 +291,14 @@ void RemoteClient::write_next_key() Message msg(m_socket); // do that before checking dimensions as get_key may // handle a resize event. - write(msg, m_ui->get_key()); + msg.write(m_ui->get_key()); DisplayCoord dimensions = m_ui->dimensions(); if (dimensions != m_dimensions) { m_dimensions = dimensions; Key key{ resize_modifier, Codepoint(((int)dimensions.line << 16) | (int)dimensions.column) }; - write(msg, key); + msg.write(key); } }