Skip to content

Commit 1af809a

Browse files
committed
Move sample::save_value, sample::load_value to implementation, return loaded value for convenience
1 parent 1719863 commit 1af809a

File tree

2 files changed

+28
-53
lines changed

2 files changed

+28
-53
lines changed

src/sample.cpp

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,32 @@ sample &sample::retrieve_typed(std::string *d) {
100100
return *this;
101101
}
102102

103-
void sample::save_raw(std::streambuf &sb, const void *address, std::size_t count) {
103+
/// Helper function to save raw binary data to a stream buffer.
104+
void save_raw(std::streambuf &sb, const void *address, std::size_t count) {
104105
if ((std::size_t)sb.sputn((const char *)address, (std::streamsize)count) != count)
105106
throw std::runtime_error("Output stream error.");
106107
}
107108

108-
void sample::load_raw(std::streambuf &sb, void *address, std::size_t count) {
109+
/// Helper function to load raw binary data from a stream buffer.
110+
void load_raw(std::streambuf &sb, void *address, std::size_t count) {
109111
if ((std::size_t)sb.sgetn((char *)address, (std::streamsize)count) != count)
110112
throw std::runtime_error("Input stream error.");
111113
}
112114

115+
/// Load a value from a stream buffer with correct endian treatment.
116+
template <typename T> T load_value(std::streambuf &sb, int use_byte_order) {
117+
T tmp;
118+
load_raw(sb, &tmp, sizeof(T));
119+
if (sizeof(T) > 1 && use_byte_order != BOOST_BYTE_ORDER) endian_reverse_inplace(tmp);
120+
return tmp;
121+
}
122+
123+
/// Save a value to a stream buffer with correct endian treatment.
124+
template <typename T> void save_value(std::streambuf &sb, T v, int use_byte_order) {
125+
if (use_byte_order != BOOST_BYTE_ORDER) endian_reverse_inplace(v);
126+
save_raw(sb, &v, sizeof(T));
127+
}
128+
113129
void sample::save_streambuf(
114130
std::streambuf &sb, int /*protocol_version*/, int use_byte_order, void *scratchpad) const {
115131
// write sample header
@@ -157,47 +173,29 @@ void sample::save_streambuf(
157173

158174
void sample::load_streambuf(std::streambuf &sb, int, int use_byte_order, bool suppress_subnormals) {
159175
// read sample header
160-
uint8_t tag;
161-
load_value(sb, tag, use_byte_order);
162-
if (tag == TAG_DEDUCED_TIMESTAMP) {
176+
if (load_value<uint8_t>(sb, use_byte_order) == TAG_DEDUCED_TIMESTAMP)
163177
// deduce the timestamp
164178
timestamp = DEDUCED_TIMESTAMP;
165-
} else {
179+
else
166180
// read the time stamp
167-
load_value(sb, timestamp, use_byte_order);
168-
}
181+
timestamp = load_value<double>(sb, use_byte_order);
182+
169183
// read channel data
170184
if (format_ == cft_string) {
171185
for (std::string *p = (std::string *)&data_, *e = p + num_channels_; p < e; p++) {
172186
// read string length as variable-length integer
173187
std::size_t len = 0;
174-
uint8_t lenbytes;
175-
load_value(sb, lenbytes, use_byte_order);
188+
auto lenbytes = load_value<uint8_t>(sb, use_byte_order);
189+
176190
if (sizeof(std::size_t) < 8 && lenbytes > sizeof(std::size_t))
177191
throw std::runtime_error(
178192
"This platform does not support strings of 64-bit length.");
179193
switch (lenbytes) {
180-
case sizeof(uint8_t): {
181-
uint8_t tmp;
182-
load_value(sb, tmp, use_byte_order);
183-
len = tmp;
184-
}; break;
185-
case sizeof(uint16_t): {
186-
uint16_t tmp;
187-
load_value(sb, tmp, use_byte_order);
188-
len = tmp;
189-
}; break;
190-
case sizeof(uint32_t): {
191-
uint32_t tmp;
192-
load_value(sb, tmp, use_byte_order);
193-
len = tmp;
194-
}; break;
194+
case sizeof(uint8_t): len = load_value<uint8_t>(sb, use_byte_order); break;
195+
case sizeof(uint16_t): len = load_value<uint16_t>(sb, use_byte_order); break;
196+
case sizeof(uint32_t): len = load_value<uint32_t>(sb, use_byte_order); break;
195197
#ifndef BOOST_NO_INT64_T
196-
case sizeof(uint64_t): {
197-
uint64_t tmp;
198-
load_value(sb, tmp, use_byte_order);
199-
len = tmp;
200-
}; break;
198+
case sizeof(uint64_t): len = load_value<uint64_t>(sb, use_byte_order); break;
201199
#endif
202200
default: throw std::runtime_error("Stream contents corrupted (invalid varlen int).");
203201
}

src/sample.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -282,29 +282,6 @@ class sample {
282282

283283
// === serialization functions ===
284284

285-
/// Helper function to save raw binary data to a stream buffer.
286-
static void save_raw(std::streambuf &sb, const void *address, std::size_t count);
287-
288-
/// Helper function to load raw binary data from a stream buffer.
289-
static void load_raw(std::streambuf &sb, void *address, std::size_t count);
290-
291-
/// Save a value to a stream buffer with correct endian treatment.
292-
template <typename T> static void save_value(std::streambuf &sb, T v, int use_byte_order) {
293-
if (use_byte_order != BOOST_BYTE_ORDER) endian_reverse_inplace(v);
294-
save_raw(sb, &v, sizeof(T));
295-
}
296-
297-
/// Load a value from a stream buffer with correct endian treatment.
298-
template <typename T> static void load_value(std::streambuf &sb, T &v, int use_byte_order) {
299-
load_raw(sb, &v, sizeof(v));
300-
if (use_byte_order != BOOST_BYTE_ORDER) endian_reverse_inplace(v);
301-
}
302-
303-
/// Load a value from a stream buffer; specialization of the above.
304-
void load_value(std::streambuf &sb, uint8_t &v, int) {
305-
load_raw(sb, &v, sizeof(v));
306-
}
307-
308285
/// Serialize a sample to a stream buffer (protocol 1.10).
309286
void save_streambuf(std::streambuf &sb, int protocol_version, int use_byte_order,
310287
void *scratchpad = nullptr) const;

0 commit comments

Comments
 (0)