55
66using namespace lsl ;
77
8- bool sample::operator ==(const sample &rhs) const BOOST_NOEXCEPT {
8+ bool sample::operator ==(const sample &rhs) const noexcept {
99 if ((timestamp != rhs.timestamp ) || (format_ != rhs.format_ ) ||
1010 (num_channels_ != rhs.num_channels_ ))
1111 return false ;
1212 if (format_ != cft_string)
13- return memcmp (&(rhs.data_ ), &data_, format_sizes[format_] * num_channels_ ) == 0 ;
13+ return memcmp (&(rhs.data_ ), &data_, datasize () ) == 0 ;
1414 else {
1515 std::string *data = (std::string *)&data_;
1616 std::string *rhsdata = (std::string *)&(rhs.data_ );
@@ -111,7 +111,7 @@ void sample::load_raw(std::streambuf &sb, void *address, std::size_t count) {
111111}
112112
113113void sample::save_streambuf (
114- std::streambuf &sb, int protocol_version, int use_byte_order, void *scratchpad) const {
114+ std::streambuf &sb, int /* protocol_version*/ , int use_byte_order, void *scratchpad) const {
115115 // write sample header
116116 if (timestamp == DEDUCED_TIMESTAMP) {
117117 save_value (sb, TAG_DEDUCED_TIMESTAMP, use_byte_order);
@@ -146,17 +146,16 @@ void sample::save_streambuf(
146146 } else {
147147 // write numeric data in binary
148148 if (use_byte_order == BOOST_BYTE_ORDER || format_sizes[format_] == 1 ) {
149- save_raw (sb, &data_, format_sizes[format_] * num_channels_ );
149+ save_raw (sb, &data_, datasize () );
150150 } else {
151- memcpy (scratchpad, &data_, format_sizes[format_] * num_channels_ );
151+ memcpy (scratchpad, &data_, datasize () );
152152 convert_endian (scratchpad);
153- save_raw (sb, scratchpad, format_sizes[format_] * num_channels_ );
153+ save_raw (sb, scratchpad, datasize () );
154154 }
155155 }
156156}
157157
158- void sample::load_streambuf (
159- std::streambuf &sb, int protocol_version, int use_byte_order, bool suppress_subnormals) {
158+ void sample::load_streambuf (std::streambuf &sb, int , int use_byte_order, bool suppress_subnormals) {
160159 // read sample header
161160 uint8_t tag;
162161 load_value (sb, tag, use_byte_order);
@@ -208,7 +207,7 @@ void sample::load_streambuf(
208207 }
209208 } else {
210209 // read numeric channel data
211- load_raw (sb, &data_, format_sizes[format_] * num_channels_ );
210+ load_raw (sb, &data_, datasize () );
212211 if (use_byte_order != BOOST_BYTE_ORDER && format_sizes[format_] > 1 ) convert_endian (&data_);
213212 if (suppress_subnormals && format_float[format_]) {
214213 if (format_ == cft_float32) {
@@ -226,8 +225,7 @@ void sample::load_streambuf(
226225 }
227226}
228227
229- template <class Archive >
230- void sample::serialize_channels (Archive &ar, const uint32_t archive_version) {
228+ template <class Archive > void sample::serialize_channels (Archive &ar, const uint32_t ) {
231229 switch (format_) {
232230 case cft_float32:
233231 for (float *p = (float *)&data_, *e = p + num_channels_; p < e; ar & *p++)
@@ -289,52 +287,49 @@ void sample::load(eos::portable_iarchive &ar, const uint32_t archive_version) {
289287 serialize_channels (ar, archive_version);
290288}
291289
290+ template <typename T> void test_pattern (T *data, uint32_t num_channels, int offset) {
291+ const int mod = std::is_integral<T>::value ? (int )std::numeric_limits<T>::max () : 1 ;
292+ for (std::size_t k = 0 ; k < num_channels; k++) {
293+ data[k] = static_cast <T>((k + static_cast <std::size_t >(offset)) % mod);
294+ if (k % 2 == 1 ) data[k] = -data[k];
295+ }
296+ }
297+
292298sample &sample::assign_test_pattern (int offset) {
293299 pushthrough = 1 ;
294300 timestamp = 123456.789 ;
295301
296302 switch (format_) {
297- case cft_float32: {
298- float *data = (float *)&data_;
299- for (int k = 0 ; k < num_channels_; k++)
300- data[k] = ((float )k + (float )offset) * (k % 2 == 0 ? 1 : -1 );
303+ case cft_float32:
304+ test_pattern (reinterpret_cast <float *>(&data_), num_channels_, offset + 0 );
301305 break ;
302- }
303- case cft_double64: {
304- double *data = (double *)&data_;
305- for (int k = 0 ; k < num_channels_; k++)
306- data[k] = (k + offset + 16777217 ) * (k % 2 == 0 ? 1 : -1 );
306+ case cft_double64:
307+ test_pattern (reinterpret_cast <double *>(&data_), num_channels_, offset + 16777217 );
307308 break ;
308- }
309309 case cft_string: {
310310 std::string *data = (std::string *)&data_;
311- for (int k = 0 ; k < num_channels_; k++)
312- data[k] = to_string ((k + 10 ) * (k % 2 == 0 ? 1 : -1 ));
311+ offset += 10 ;
312+ for (uint32_t k = 0u ; k < num_channels_; k++)
313+ data[k] = to_string (k * (k % 2 == 0 ? 1 : -1 ));
313314 break ;
314315 }
315- case cft_int32: {
316- int32_t *data = (int32_t *)&data_;
317- for (int k = 0 ; k < num_channels_; k++)
318- data[k] = ((k + offset + 65537 ) % 2147483647 ) * (k % 2 == 0 ? 1 : -1 );
316+ case cft_int32:
317+ test_pattern (reinterpret_cast <int32_t *>(&data_), num_channels_, offset + 65537 );
319318 break ;
320- }
321- case cft_int16: {
322- int16_t *data = (int16_t *)&data_;
323- for (int k = 0 ; k < num_channels_; k++)
324- data[k] = (int16_t )(((k + offset + 257 ) % 32767 ) * (k % 2 == 0 ? 1 : -1 ));
319+ case cft_int16:
320+ test_pattern (reinterpret_cast <int16_t *>(&data_), num_channels_, offset + 257 );
325321 break ;
326- }
327- case cft_int8: {
328- int8_t *data = (int8_t *)&data_;
329- for (int k = 0 ; k < num_channels_; k++)
330- data[k] = (int8_t )(((k + offset + 1 ) % 127 ) * (k % 2 == 0 ? 1 : -1 ));
322+ case cft_int8:
323+ test_pattern (reinterpret_cast <int8_t *>(&data_), num_channels_, offset + 1 );
331324 break ;
332- }
333325#ifndef BOOST_NO_INT64_T
334326 case cft_int64: {
335327 int64_t *data = (int64_t *)&data_;
336- for (int k = 0 ; k < num_channels_; k++)
337- data[k] = ((2 + k + (int64_t )offset + 2147483647 )) * (int64_t )(k % 2 == 0 ? 1 : -1 );
328+ int64_t offset64 = 2147483649ll + offset;
329+ for (uint32_t k = 0 ; k < num_channels_; k++) {
330+ data[k] = (k + offset64);
331+ if (k % 2 == 1 ) data[k] = -data[k];
332+ }
338333 break ;
339334 }
340335#endif
@@ -344,11 +339,11 @@ sample &sample::assign_test_pattern(int offset) {
344339 return *this ;
345340}
346341
347- factory::factory (lsl_channel_format_t fmt, int num_chans, int num_reserve)
342+ factory::factory (lsl_channel_format_t fmt, uint32_t num_chans, uint32_t num_reserve)
348343 : fmt_(fmt), num_chans_(num_chans),
349344 sample_size_(
350345 ensure_multiple (sizeof (sample) - sizeof(char ) + format_sizes[fmt] * num_chans, 16)),
351- storage_size_(sample_size_ * std::max (1 , num_reserve)), storage_(new char [storage_size_]),
346+ storage_size_(sample_size_ * std::max (1u , num_reserve)), storage_(new char [storage_size_]),
352347 sentinel_(new_sample_unmanaged(fmt, num_chans, 0.0 , false )), head_(sentinel_),
353348 tail_(sentinel_) {
354349 // pre-construct an array of samples in the storage area and chain into a freelist
@@ -374,7 +369,7 @@ sample_p factory::new_sample(double timestamp, bool pushthrough) {
374369}
375370
376371sample *factory::new_sample_unmanaged (
377- lsl_channel_format_t fmt, int num_chans, double timestamp, bool pushthrough) {
372+ lsl_channel_format_t fmt, uint32_t num_chans, double timestamp, bool pushthrough) {
378373#pragma warning(suppress : 4291)
379374 sample *result = new (new char [ensure_multiple (
380375 sizeof (sample) - sizeof (char ) + format_sizes[fmt] * num_chans, 16 )])
0 commit comments