# HG changeset patch # User Teemu Piippo # Date 1611768775 -7200 # Node ID 2e6cbacafdc7ef9080ca34780f36466f58fdbf77 # Parent 90bf9049e5eb8f1977603b559dbf0223293dfb69 various little touchups diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/basics.h --- a/sources/basics.h Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/basics.h Wed Jan 27 19:32:55 2021 +0200 @@ -30,8 +30,8 @@ #pragma once #include +#include #include -#include #if !defined(_MSC_VER) && !defined(__cdecl) # define __cdecl @@ -74,8 +74,8 @@ BEGIN_ZFC_NAMESPACE -template -T min (T a, T b) +template +constexpr std::common_type_t min(T a, TT b) { return (a < b) ? a : b; } @@ -91,8 +91,8 @@ return result; } -template -T max (T a, T b) +template +constexpr std::common_type_t max(T a, TT b) { return (a > b) ? a : b; } @@ -109,7 +109,7 @@ } template -std::common_type_t clamp(T a, TT b, TTT c) +constexpr std::common_type_t clamp(T a, TT b, TTT c) { return (a < b) ? b : (a > c) ? c : a; } @@ -119,9 +119,11 @@ return value != 1 ? "s" : ""; } -template -char (&_ArraySizeHelper(T (&array)[N]))[N]; -#define countof(array) (sizeof(_ArraySizeHelper( array ))) +template +constexpr std::size_t countof(T(&)[N]) +{ + return N; +} struct Exitception {}; diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/coloredline.h --- a/sources/coloredline.h Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/coloredline.h Wed Jan 27 19:32:55 2021 +0200 @@ -49,24 +49,17 @@ NUM_COLORS }; -// ------------------------------------------------------------------------------------------------- -// -enum -{ - RLINE_ON_COLOR = 256, - RLINE_OFF_COLOR = 264, - RLINE_ON_BOLD = 272, - RLINE_OFF_BOLD -}; +constexpr int RLINE_ON_COLOR = 256; +constexpr int RLINE_OFF_COLOR = 264; +constexpr int RLINE_ON_BOLD = 272; +constexpr int RLINE_OFF_BOLD = 273; -// ------------------------------------------------------------------------------------------------- -// class ColoredLine { public: ColoredLine(); - const Vector& data() const { return m_data; } + const std::vector& data() const { return m_data; } int length() const { return m_length; } void addChar(char ch); void addString(const std::string& msg); @@ -77,7 +70,7 @@ void activateColor(Color color, bool bold); void setColor(Color a, bool on); - Vector m_data; + std::vector m_data; int m_length; bool m_final; Color m_activeColor; diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/interface.h --- a/sources/interface.h Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/interface.h Wed Jan 27 19:32:55 2021 +0200 @@ -78,7 +78,7 @@ bool m_needOutputRender; bool m_needNicklistRender; struct { char ch; int x; } m_cursorCharacter; - Vector m_outputLines; + std::vector m_outputLines; int m_outputScroll; std::string m_title; InputState m_inputState; diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/list.cpp --- a/sources/list.cpp Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/list.cpp Wed Jan 27 19:32:55 2021 +0200 @@ -75,7 +75,7 @@ } } -std::string quote(const ByteArray &bytes) +std::string quote(const std::vector &bytes) { std::string result; diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/list.h --- a/sources/list.h Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/list.h Wed Jan 27 19:32:55 2021 +0200 @@ -35,16 +35,12 @@ BEGIN_ZFC_NAMESPACE template -using Vector = std::vector; - -template auto& last(T& container) { return *(container.begin() + container.size() - 1); } -using ByteArray = std::vector; -std::string quote(const ByteArray& bytes); +std::string quote(const std::vector& bytes); template T splice(const T& container, int start, int end, int step = 1) diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/mystring.cpp --- a/sources/mystring.cpp Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/mystring.cpp Wed Jan 27 19:32:55 2021 +0200 @@ -137,7 +137,7 @@ else { // vsnprintf needs more space, so we have to allocate a new buffer and try again. - Vector newBuffer(length + 1); + std::vector newBuffer(length + 1); vsnprintf(newBuffer.data(), length + 1, formatString, argsCopy); result = newBuffer.data(); } diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/network/bytestream.cpp --- a/sources/network/bytestream.cpp Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/network/bytestream.cpp Wed Jan 27 19:32:55 2021 +0200 @@ -36,7 +36,7 @@ * \brief Constructs a byte cursor. The cursor is placed to the beginning of the stream. * \param data */ -Bytestream::Bytestream(ByteArray& data) : +Bytestream::Bytestream(std::vector& data) : m_data(data), m_position(0) {} @@ -66,7 +66,7 @@ /*! * \returns an iterator to the current data position. */ -ByteArray::iterator Bytestream::getCurrentIterator() +std::vector::iterator Bytestream::getCurrentIterator() { return m_data.begin() + m_position; } @@ -145,10 +145,10 @@ * \param length Amount of bytes to read. * \returns the read buffer. */ -ByteArray Bytestream::readBuffer(int length) +std::vector Bytestream::readBuffer(int length) { ensureReadSpace(length); - ByteArray result(length); + std::vector result(length); memcpy(result.data(), m_data.data() + m_position, length); m_position += length; return result; diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/network/bytestream.h --- a/sources/network/bytestream.h Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/network/bytestream.h Wed Jan 27 19:32:55 2021 +0200 @@ -56,12 +56,12 @@ class Bytestream { public: - Bytestream(ByteArray& data); + Bytestream(std::vector& data); int bytesLeft() const; - ByteArray::iterator getCurrentIterator(); + std::vector::iterator getCurrentIterator(); int position() const; - ByteArray readBuffer(int length); + std::vector readBuffer(int length); int8_t readByte(); int32_t readLong(); int16_t readShort(); @@ -70,7 +70,7 @@ void rewind(); void seek(int position); void write(const unsigned char* val, unsigned int length); - void writeBuffer(const ByteArray& other); + void writeBuffer(const std::vector& other); void writeByte(int8_t value); void writeDouble(double val); void writeFloat(float value); @@ -79,7 +79,7 @@ void writeString(const std::string& string); private: - ByteArray& m_data; + std::vector& m_data; int m_position; int8_t read(); diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/network/rconsession.cpp --- a/sources/network/rconsession.cpp Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/network/rconsession.cpp Wed Jan 27 19:32:55 2021 +0200 @@ -80,7 +80,7 @@ // ------------------------------------------------------------------------------------------------- // -bool RCONSession::send(const ByteArray& packet) +bool RCONSession::send(const std::vector& packet) { std::stringstream errors; const bool result = m_socket.send(m_address, packet, errors); @@ -134,7 +134,7 @@ // ------------------------------------------------------------------------------------------------- // -void RCONSession::handlePacket(ByteArray& message) +void RCONSession::handlePacket(std::vector& message) { Bytestream stream(message); @@ -299,7 +299,7 @@ void RCONSession::sendPassword() { m_interface->print("Authenticating...\n"); - ByteArray message; + std::vector message; Bytestream stream(message); stream.writeByte(CLRC_PASSWORD); stream.writeString(md5((m_salt + m_password).data())); @@ -338,7 +338,7 @@ if (m_state != RCON_CONNECTED or commandString.empty()) return false; - ByteArray message; + std::vector message; Bytestream stream(message); stream.writeByte(CLRC_COMMAND); stream.writeString(commandString); @@ -381,7 +381,7 @@ { if (m_serverProtocol >= 4) { - ByteArray message; + std::vector message; Bytestream stream(message); stream.writeByte(CLRC_TABCOMPLETE); stream.writeString(part); diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/network/rconsession.h --- a/sources/network/rconsession.h Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/network/rconsession.h Wed Jan 27 19:32:55 2021 +0200 @@ -103,11 +103,11 @@ const std::string& getLevel() const; net::UDPSocket* getSocket(); RCONSessionState getState() const; - void handlePacket(ByteArray& message); + void handlePacket(std::vector& message); bool isActive() const; void processServerUpdates(Bytestream& packet); void requestTabCompletion(const std::string& part); - bool send(const ByteArray& packet); + bool send(const std::vector& packet); bool sendCommand(const std::string& commandString); void sendHello(); void sendPassword(); diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/network/udpsocket.cpp --- a/sources/network/udpsocket.cpp Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/network/udpsocket.cpp Wed Jan 27 19:32:55 2021 +0200 @@ -134,11 +134,11 @@ decodedPacket, length, &decodedLength); datagram.address.host = ntohl(claddr.sin_addr.s_addr); datagram.address.port = ntohs(claddr.sin_port); - datagram.message = ByteArray{&decodedPacket[0], &decodedPacket[decodedLength]}; + datagram.message = std::vector{&decodedPacket[0], &decodedPacket[decodedLength]}; return true; } -bool net::UDPSocket::send(const net::ip_address& address, const ByteArray& data, std::ostream& errors) +bool net::UDPSocket::send(const net::ip_address& address, const std::vector& data, std::ostream& errors) { int encodedlength = sizeof HuffmanBuffer; ::HUFFMAN_Encode(data.data(), reinterpret_cast(HuffmanBuffer), data.size(), &encodedlength); diff -r 90bf9049e5eb -r 2e6cbacafdc7 sources/network/udpsocket.h --- a/sources/network/udpsocket.h Wed Jan 27 19:28:18 2021 +0200 +++ b/sources/network/udpsocket.h Wed Jan 27 19:32:55 2021 +0200 @@ -43,7 +43,7 @@ struct net::Datagram { - ByteArray message; + std::vector message; net::ip_address address; }; @@ -56,7 +56,7 @@ virtual ~UDPSocket(); [[nodiscard]] bool bind(port_t port, std::ostream &errors); [[nodiscard]] bool read(Datagram& datagram, std::ostream& errors); - [[nodiscard]] bool send(const ip_address& address, const ByteArray& data, std::ostream &errors); + [[nodiscard]] bool send(const ip_address& address, const std::vector& data, std::ostream &errors); [[nodiscard]] bool set_blocking(bool a, std::ostream &errors); const int file_descriptor; };