Wed, 27 Jan 2021 19:32:55 +0200
various little touchups
--- 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 <cassert> +#include <cstdlib> #include <type_traits> -#include <stdlib.h> #if !defined(_MSC_VER) && !defined(__cdecl) # define __cdecl @@ -74,8 +74,8 @@ BEGIN_ZFC_NAMESPACE -template<typename T> -T min (T a, T b) +template<typename T, typename TT> +constexpr std::common_type_t<T, TT> min(T a, TT b) { return (a < b) ? a : b; } @@ -91,8 +91,8 @@ return result; } -template<typename T> -T max (T a, T b) +template<typename T, typename TT> +constexpr std::common_type_t<T, TT> max(T a, TT b) { return (a > b) ? a : b; } @@ -109,7 +109,7 @@ } template<typename T, typename TT, typename TTT> -std::common_type_t<T, TT, TTT> clamp(T a, TT b, TTT c) +constexpr std::common_type_t<T, TT, TTT> clamp(T a, TT b, TTT c) { return (a < b) ? b : (a > c) ? c : a; } @@ -119,9 +119,11 @@ return value != 1 ? "s" : ""; } -template <typename T, size_t N> -char (&_ArraySizeHelper(T (&array)[N]))[N]; -#define countof(array) (sizeof(_ArraySizeHelper( array ))) +template<typename T, std::size_t N> +constexpr std::size_t countof(T(&)[N]) +{ + return N; +} struct Exitception {};
--- 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<int>& data() const { return m_data; } + const std::vector<int>& 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<int> m_data; + std::vector<int> m_data; int m_length; bool m_final; Color m_activeColor;
--- 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<ColoredLine> m_outputLines; + std::vector<ColoredLine> m_outputLines; int m_outputScroll; std::string m_title; InputState m_inputState;
--- 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<unsigned char> &bytes) { std::string result;
--- 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<typename T> -using Vector = std::vector<T>; - -template<typename T> auto& last(T& container) { return *(container.begin() + container.size() - 1); } -using ByteArray = std::vector<unsigned char>; -std::string quote(const ByteArray& bytes); +std::string quote(const std::vector<unsigned char>& bytes); template<typename T> T splice(const T& container, int start, int end, int step = 1)
--- 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<char> newBuffer(length + 1); + std::vector<char> newBuffer(length + 1); vsnprintf(newBuffer.data(), length + 1, formatString, argsCopy); result = newBuffer.data(); }
--- 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<unsigned char>& 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<unsigned char>::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<unsigned char> Bytestream::readBuffer(int length) { ensureReadSpace(length); - ByteArray result(length); + std::vector<unsigned char> result(length); memcpy(result.data(), m_data.data() + m_position, length); m_position += length; return result;
--- 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<unsigned char>& data); int bytesLeft() const; - ByteArray::iterator getCurrentIterator(); + std::vector<unsigned char>::iterator getCurrentIterator(); int position() const; - ByteArray readBuffer(int length); + std::vector<unsigned char> 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<unsigned char>& 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<unsigned char>& m_data; int m_position; int8_t read();
--- 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<unsigned char>& 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<unsigned char>& message) { Bytestream stream(message); @@ -299,7 +299,7 @@ void RCONSession::sendPassword() { m_interface->print("Authenticating...\n"); - ByteArray message; + std::vector<unsigned char> 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<unsigned char> message; Bytestream stream(message); stream.writeByte(CLRC_COMMAND); stream.writeString(commandString); @@ -381,7 +381,7 @@ { if (m_serverProtocol >= 4) { - ByteArray message; + std::vector<unsigned char> message; Bytestream stream(message); stream.writeByte(CLRC_TABCOMPLETE); stream.writeString(part);
--- 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<unsigned char>& message); bool isActive() const; void processServerUpdates(Bytestream& packet); void requestTabCompletion(const std::string& part); - bool send(const ByteArray& packet); + bool send(const std::vector<unsigned char>& packet); bool sendCommand(const std::string& commandString); void sendHello(); void sendPassword();
--- 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<unsigned char>{&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<unsigned char>& data, std::ostream& errors) { int encodedlength = sizeof HuffmanBuffer; ::HUFFMAN_Encode(data.data(), reinterpret_cast<unsigned char*>(HuffmanBuffer), data.size(), &encodedlength);
--- 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<unsigned char> 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<unsigned char>& data, std::ostream &errors); [[nodiscard]] bool set_blocking(bool a, std::ostream &errors); const int file_descriptor; };