continue cleanup

Wed, 27 Jan 2021 13:08:51 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 27 Jan 2021 13:08:51 +0200
changeset 180
2e7225dbd9b2
parent 179
7fc34735178e
child 181
e254398fcc7c

continue cleanup

sources/list.cpp file | annotate | diff | comparison | revisions
sources/list.h file | annotate | diff | comparison | revisions
sources/network/bytestream.cpp file | annotate | diff | comparison | revisions
sources/network/bytestream.h file | annotate | diff | comparison | revisions
sources/network/rconsession.cpp file | annotate | diff | comparison | revisions
sources/network/udpsocket.cpp file | annotate | diff | comparison | revisions
--- a/sources/list.cpp	Wed Jan 27 13:02:51 2021 +0200
+++ b/sources/list.cpp	Wed Jan 27 13:08:51 2021 +0200
@@ -76,21 +76,11 @@
 	}
 }
 
-/*!
- * \brief Constructs a byte array from an initializer list containing bytes.
- * \param initializerList List of bytes.
- */
-ByteArray::ByteArray(std::initializer_list<unsigned char> initializerList) :
-	Vector<unsigned char>(initializerList) {}
-
-/*!
- * \returns a quoted representation of the contents of the byte array.
- */
-String ByteArray::quote() const
+String quote(const ByteArray &bytes)
 {
 	String result;
 
-	for (unsigned char byte : *this)
+	for (unsigned char byte : bytes)
 		result += representByte(byte);
 
 	return "\"" + result + "\"";
--- a/sources/list.h	Wed Jan 27 13:02:51 2021 +0200
+++ b/sources/list.h	Wed Jan 27 13:08:51 2021 +0200
@@ -346,24 +346,6 @@
 //
 
 template<typename T>
-class List : public Container<T, std::deque<T> >
-{
-public:
-	typedef Container<T, std::deque<T> > Super;
-
-	List(){}
-
-	List (int numvalues) :
-		Super (numvalues) {}
-
-	List (const Super& other) :
-		Super (other) {}
-};
-
-// -------------------------------------------------------------------------------------------------
-//
-
-template<typename T>
 class Vector : public Container<T, std::vector<T> >
 {
 public:
@@ -399,24 +381,8 @@
 	}
 };
 
-class ByteArray : public Vector<unsigned char>
-{
-public:
-	ByteArray(std::initializer_list<unsigned char> initializerList);
-
-	template<typename ...Args>
-	ByteArray(Args&& ...args);
-
-	class String quote() const;
-};
-
-/*!
- * \brief Constructs a byte array by passing all arguments to Vector<unsigned char>'s constructor.
- * \param args Arguments to pass.
- */
-template<typename ...Args>
-ByteArray::ByteArray(Args&& ...args) :
-    Vector<unsigned char>(args...) {}
+using ByteArray = std::vector<unsigned char>;
+class String quote(const ByteArray& bytes);
 
 template<typename T>
 T splice(const T& container, int start, int end, int step = 1)
--- a/sources/network/bytestream.cpp	Wed Jan 27 13:02:51 2021 +0200
+++ b/sources/network/bytestream.cpp	Wed Jan 27 13:08:51 2021 +0200
@@ -66,7 +66,7 @@
 /*!
  * \returns an iterator to the current data position.
  */
-ByteArray::Iterator Bytestream::getCurrentIterator()
+ByteArray::iterator Bytestream::getCurrentIterator()
 {
 	return m_data.begin() + m_position;
 }
@@ -160,7 +160,7 @@
  */
 void Bytestream::writeByte(int8_t value)
 {
-	m_data.append(value);
+	m_data.push_back(value);
 }
 
 /*!
@@ -169,8 +169,9 @@
  */
 void Bytestream::writeShort(int16_t value)
 {
+	m_data.reserve(m_data.size() + 2);
 	for (int i : range(2))
-		m_data.append((value >> (i * 8)) & 0xFF);
+		m_data.push_back((value >> (i * 8)) & 0xFF);
 }
 
 /*!
@@ -179,8 +180,9 @@
  */
 void Bytestream::writeLong(int32_t value)
 {
+	m_data.reserve(m_data.size() + 4);
 	for (int i : range(4))
-		m_data.append((value >> (i * 8)) & 0xFF);
+		m_data.push_back((value >> (i * 8)) & 0xFF);
 }
 
 /*!
@@ -201,8 +203,11 @@
  */
 void Bytestream::writeString(const String& string)
 {
-	m_data.append(string.toBytes(), string.length());
-	m_data.append(0);
+	const int oldSize = m_data.size();
+	m_data.reserve(m_data.size() + string.length() + 1);
+	m_data.resize(m_data.size() + string.length());
+	std::copy(string.begin(), string.end(), m_data.begin() + oldSize);
+	m_data.push_back(0);
 }
 
 /*!
--- a/sources/network/bytestream.h	Wed Jan 27 13:02:51 2021 +0200
+++ b/sources/network/bytestream.h	Wed Jan 27 13:08:51 2021 +0200
@@ -60,7 +60,7 @@
 	Bytestream(ByteArray& data);
 
 	int bytesLeft() const;
-	ByteArray::Iterator getCurrentIterator();
+	ByteArray::iterator getCurrentIterator();
 	int position() const;
 	ByteArray readBuffer(int length);
 	int8_t readByte();
--- a/sources/network/rconsession.cpp	Wed Jan 27 13:02:51 2021 +0200
+++ b/sources/network/rconsession.cpp	Wed Jan 27 13:08:51 2021 +0200
@@ -227,7 +227,7 @@
 	catch (std::exception& e)
 	{
 		m_interface->printWarning("Couldn't process packet: %s\n", e.what());
-		m_interface->printWarning("Packet contents was: %s\n", message.quote().chars());
+		m_interface->printWarning("Packet contents was: %s\n", quote(message).chars());
 		m_interface->printWarning("Stream position in payload was: %d\n", stream.position());
 	}
 }
--- a/sources/network/udpsocket.cpp	Wed Jan 27 13:02:51 2021 +0200
+++ b/sources/network/udpsocket.cpp	Wed Jan 27 13:08:51 2021 +0200
@@ -135,7 +135,7 @@
 		decodedPacket, length, &decodedLength);
 	datagram.address.host = ntohl (claddr.sin_addr.s_addr);
 	datagram.address.port = ntohs (claddr.sin_port);
-	datagram.message = ByteArray(decodedPacket, decodedLength);
+	datagram.message = ByteArray{&decodedPacket[0], &decodedPacket[decodedLength]};
 	return true;
 }
 

mercurial