sources/network/bytestream.cpp

changeset 185
e83ec58cc458
parent 183
9b6a0daedfc0
child 190
90bf9049e5eb
equal deleted inserted replaced
184:afd63357a03d 185:e83ec58cc458
88 int16_t Bytestream::readShort() 88 int16_t Bytestream::readShort()
89 { 89 {
90 ensureReadSpace (2); 90 ensureReadSpace (2);
91 int16_t result = 0; 91 int16_t result = 0;
92 92
93 for (int i : range(2)) 93 for (int i : {0, 1})
94 result |= read() << (i * 8); 94 result |= read() << (i * 8);
95 95
96 return result; 96 return result;
97 } 97 }
98 98
102 */ 102 */
103 int32_t Bytestream::readLong() 103 int32_t Bytestream::readLong()
104 { 104 {
105 ensureReadSpace (4); 105 ensureReadSpace (4);
106 int32_t result = 0; 106 int32_t result = 0;
107 107
108 for (int i : range(4)) 108 for (int i = 0; i < 4; i += 1)
109 result |= read() << (i * 8); 109 result |= read() << (i * 8);
110 110
111 return result; 111 return result;
112 } 112 }
113 113
168 * \param value Value to write 168 * \param value Value to write
169 */ 169 */
170 void Bytestream::writeShort(int16_t value) 170 void Bytestream::writeShort(int16_t value)
171 { 171 {
172 m_data.reserve(m_data.size() + 2); 172 m_data.reserve(m_data.size() + 2);
173 for (int i : range(2)) 173 for (int i : {0, 1})
174 m_data.push_back((value >> (i * 8)) & 0xFF); 174 m_data.push_back((value >> (i * 8)) & 0xFF);
175 } 175 }
176 176
177 /*! 177 /*!
178 * \brief Writes an integer to the end of the data as 4 bytes. 178 * \brief Writes an integer to the end of the data as 4 bytes.
179 * \param value Value to write 179 * \param value Value to write
180 */ 180 */
181 void Bytestream::writeLong(int32_t value) 181 void Bytestream::writeLong(int32_t value)
182 { 182 {
183 m_data.reserve(m_data.size() + 4); 183 m_data.reserve(m_data.size() + 4);
184 for (int i : range(4)) 184 for (int i = 0; i < 4; i += 1)
185 m_data.push_back((value >> (i * 8)) & 0xFF); 185 m_data.push_back((value >> (i * 8)) & 0xFF);
186 } 186 }
187 187
188 /*! 188 /*!
189 * \brief Writes a floating-point number to the end of the data. 189 * \brief Writes a floating-point number to the end of the data.

mercurial