Skip to content

Commit de01888

Browse files
committed
Minor Serializer refactoring
1 parent 5c22a81 commit de01888

2 files changed

Lines changed: 23 additions & 33 deletions

File tree

libs/common/include/s25util/Serializer.h

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2005 - 2017 Settlers Freaks (sf-team at siedler25.org)
1+
// Copyright (c) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
22
//
33
// This file is part of Return To The Roots.
44
//
@@ -38,39 +38,35 @@ class Serializer
3838
Serializer() = default;
3939
Serializer(const void* data, unsigned initial_size);
4040

41-
virtual ~Serializer() = default;
42-
43-
/// Aufräumen
41+
/// Remove all data
4442
void Clear();
43+
/// Get current read position
44+
unsigned GetPos() const noexcept { return pos_; }
4545

46-
unsigned GetPos() const { return pos_; }
47-
48-
/// Getter
49-
unsigned GetLength() const { return length_; }
50-
51-
/// Schreibzugriff auf die Länge
46+
/// Get current number of bytes contained
47+
unsigned GetLength() const noexcept { return length_; }
48+
/// Set the current length
5249
void SetLength(unsigned length);
5350

54-
unsigned GetBytesLeft() const;
51+
unsigned GetBytesLeft() const noexcept;
5552

56-
/// Zugriff auf die Rohdaten
57-
const unsigned char* GetData() const { return data_.data(); }
53+
/// Access current data
54+
const uint8_t* GetData() const noexcept { return data_.data(); }
5855

59-
/// Schreibzugriff auf die Rohdaten
60-
unsigned char* GetDataWritable(unsigned length)
56+
/// Writable access to data. E.g. to write directly to the buffer and call SetLength afterwards
57+
uint8_t* GetDataWritable(unsigned length)
6158
{
6259
EnsureSize(length);
6360
return data_.data();
6461
}
6562

66-
/// Schreibt den Buffer in eine Datei
6763
void WriteToFile(BinaryFile& file) const;
68-
/// Liest den Buffer aus einer Datei
69-
virtual void ReadFromFile(BinaryFile& file);
64+
void ReadFromFile(BinaryFile& file);
65+
66+
// Write methods
7067

7168
void PushRawData(const void* data, unsigned length);
7269

73-
/// Sämtliche Integer
7470
void PushSignedInt(int32_t i) { Push(i); }
7571
void PushUnsignedInt(uint32_t i) { Push(i); }
7672

@@ -87,12 +83,10 @@ class Serializer
8783
void PushString(const std::string& str);
8884
void PushLongString(const std::string& str);
8985

90-
// Lesemethoden
86+
// Read methods
9187

92-
/// Rohdaten kopieren
9388
void PopRawData(void* data, unsigned length);
9489

95-
/// Sämtliche Integer
9690
int32_t PopSignedInt() { return Pop<int32_t>(); }
9791
uint32_t PopUnsignedInt() { return Pop<uint32_t>(); }
9892

@@ -115,24 +109,21 @@ class Serializer
115109
void Push(T val);
116110

117111
private:
118-
/// Erweitert ggf. Speicher um add_length
119-
void ExtendMemory(unsigned add_length) { EnsureSize(length_ + add_length); }
120-
112+
/// Adds the given number of bytes to the usable length
113+
void ExtendMemory(unsigned numBytes) { EnsureSize(length_ + numBytes); }
114+
/// Checks if data of size len can be popped
115+
void CheckSize(unsigned len) const;
121116
/// Makes sure the internal buffer is at least length bytes long
122117
void EnsureSize(unsigned length);
123118

124-
/// data mit den Daten
125119
boost::container::vector<uint8_t> data_;
126-
/// Logische Länge
120+
/// Length (number of bytes) of valid data. Also the current write position
127121
unsigned length_ = 0;
128-
/// Schreib/Leseposition
122+
/// Current read position
129123
unsigned pos_ = 0;
130-
131-
/// Checks if data of size len can be popped
132-
void CheckSize(unsigned len) const;
133124
};
134125

135-
inline unsigned Serializer::GetBytesLeft() const
126+
inline unsigned Serializer::GetBytesLeft() const noexcept
136127
{
137128
assert(pos_ <= length_);
138129
return length_ - pos_;

libs/common/src/Serializer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,5 @@ std::string Serializer::PopLongString()
119119
std::string str;
120120
str.resize(PopUnsignedInt());
121121
PopRawData(&str[0], static_cast<unsigned>(str.length()));
122-
123122
return str;
124123
}

0 commit comments

Comments
 (0)