Skip to content

Commit ffe3b09

Browse files
committed
Convert channel_count to unsigned int to prevent MSVC warnings
1 parent b1f0917 commit ffe3b09

File tree

11 files changed

+85
-83
lines changed

11 files changed

+85
-83
lines changed

src/data_receiver.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void data_receiver::close_stream() {
7171
}
7272

7373
template <class T>
74-
double data_receiver::pull_sample_typed(T *buffer, int buffer_elements, double timeout) {
74+
double data_receiver::pull_sample_typed(T *buffer, uint32_t buffer_elements, double timeout) {
7575
if (conn_.lost())
7676
throw lost_error("The stream read by this outlet has been lost. To recover, you need to "
7777
"re-resolve the source and re-create the inlet.");
@@ -95,13 +95,13 @@ double data_receiver::pull_sample_typed(T *buffer, int buffer_elements, double t
9595
}
9696
}
9797

98-
template double data_receiver::pull_sample_typed<char>(char *, int, double);
99-
template double data_receiver::pull_sample_typed<int16_t>(int16_t *, int, double);
100-
template double data_receiver::pull_sample_typed<int32_t>(int32_t *, int, double);
101-
template double data_receiver::pull_sample_typed<int64_t>(int64_t *, int, double);
102-
template double data_receiver::pull_sample_typed<float>(float *, int, double);
103-
template double data_receiver::pull_sample_typed<double>(double *, int, double);
104-
template double data_receiver::pull_sample_typed<std::string>(std::string *, int, double);
98+
template double data_receiver::pull_sample_typed<char>(char *, uint32_t, double);
99+
template double data_receiver::pull_sample_typed<int16_t>(int16_t *, uint32_t, double);
100+
template double data_receiver::pull_sample_typed<int32_t>(int32_t *, uint32_t, double);
101+
template double data_receiver::pull_sample_typed<int64_t>(int64_t *, uint32_t, double);
102+
template double data_receiver::pull_sample_typed<float>(float *, uint32_t, double);
103+
template double data_receiver::pull_sample_typed<double>(double *, uint32_t, double);
104+
template double data_receiver::pull_sample_typed<std::string>(std::string *, uint32_t, double);
105105

106106
double data_receiver::pull_sample_untyped(void *buffer, int buffer_bytes, double timeout) {
107107
if (conn_.lost())

src/data_receiver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class data_receiver : public cancellable_registry {
6161

6262
/// Retrieve a sample from the sample queue and assign its contents to the given typed buffer.
6363
template <class T>
64-
double pull_sample_typed(T *buffer, int buffer_elements, double timeout = FOREVER);
64+
double pull_sample_typed(T *buffer, uint32_t buffer_elements, double timeout = FOREVER);
6565

6666
/// Read sample from the inlet and read it into a pointer to raw data.
6767
double pull_sample_untyped(void *buffer, int buffer_bytes, double timeout = FOREVER);

src/lsl_inlet_c.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ LIBLSL_C_API unsigned long lsl_pull_chunk_str(lsl_inlet in, char **data_buffer,
282282
// capture output in a temporary string buffer
283283
if (data_buffer_elements) {
284284
std::vector<std::string> tmp(data_buffer_elements);
285-
unsigned long result = in->pull_chunk_multiplexed(&tmp[0], timestamp_buffer,
285+
uint32_t result = in->pull_chunk_multiplexed(&tmp[0], timestamp_buffer,
286286
data_buffer_elements, timestamp_buffer_elements, timeout);
287287
// allocate memory and copy over into buffer
288288
for (std::size_t k = 0; k < tmp.size(); k++) {
@@ -320,7 +320,7 @@ LIBLSL_C_API unsigned long lsl_pull_chunk_buf(lsl_inlet in, char **data_buffer,
320320
// capture output in a temporary string buffer
321321
if (data_buffer_elements) {
322322
std::vector<std::string> tmp(data_buffer_elements);
323-
unsigned long result = in->pull_chunk_multiplexed(&tmp[0], timestamp_buffer,
323+
uint32_t result = in->pull_chunk_multiplexed(&tmp[0], timestamp_buffer,
324324
data_buffer_elements, timestamp_buffer_elements, timeout);
325325
// allocate memory and copy over into buffer
326326
for (uint32_t k = 0; k < tmp.size(); k++) {

src/lsl_resolver_c.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ LIBLSL_C_API int32_t lsl_resolver_results(
2828
std::vector<stream_info_impl> tmp = res->results(buffer_elements);
2929
// allocate new stream_info_impl's and assign to the buffer
3030
for (uint32_t k = 0; k < tmp.size(); k++) buffer[k] = new stream_info_impl(tmp[k]);
31-
return tmp.size();
32-
} catch (std::exception &e) {
31+
return static_cast<int32_t>(tmp.size());
32+
} catch(std::exception &e) {
3333
LOG_F(WARNING, "Unexpected error querying lsl_resolver_results: %s", e.what());
3434
return lsl_internal_error;
3535
}

src/sample.cpp

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
using 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

113113
void 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+
292298
sample &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

376371
sample *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)])

src/sample.h

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const uint8_t TAG_DEDUCED_TIMESTAMP = 1;
4040
const uint8_t TAG_TRANSMITTED_TIMESTAMP = 2;
4141

4242
/// channel format properties
43-
const int format_sizes[] = {0, sizeof(float), sizeof(double), sizeof(std::string), sizeof(int32_t),
43+
const uint8_t format_sizes[] = {0, sizeof(float), sizeof(double), sizeof(std::string), sizeof(int32_t),
4444
sizeof(int16_t), sizeof(int8_t), 8};
4545
const bool format_ieee754[] = {false, std::numeric_limits<float>::is_iec559,
4646
std::numeric_limits<double>::is_iec559, false, false, false, false, false};
@@ -54,8 +54,13 @@ const bool format_float[] = {false, true, true, false, false, false, false, fals
5454
/// A factory to create samples of a given format/size. Must outlive all of its created samples.
5555
class factory {
5656
public:
57-
/// Create a new factory and optionally pre-allocate samples.
58-
factory(lsl_channel_format_t fmt, int num_chans, int num_reserve);
57+
/**
58+
* Create a new factory and optionally pre-allocate samples.
59+
* @param fmt Sample format
60+
* @param num_chans nr of channels
61+
* @param num_reserve nr of samples to pre-allocate in the storage pool
62+
*/
63+
factory(lsl_channel_format_t fmt, uint32_t num_chans, uint32_t num_reserve);
5964

6065
/// Destroy the factory and delete all of its samples.
6166
~factory();
@@ -69,7 +74,7 @@ class factory {
6974

7075
/// Create a new sample whose memory is not managed by the factory.
7176
static sample *new_sample_unmanaged(
72-
lsl_channel_format_t fmt, int num_chans, double timestamp, bool pushthrough);
77+
lsl_channel_format_t fmt, uint32_t num_chans, double timestamp, bool pushthrough);
7378

7479
private:
7580
/// ensure that a given value is a multiple of some base, round up if necessary
@@ -84,11 +89,11 @@ class factory {
8489
/// the channel format to construct samples with
8590
lsl_channel_format_t fmt_;
8691
/// the number of channels to construct samples with
87-
int num_chans_;
92+
const uint32_t num_chans_;
8893
/// size of a sample, in bytes
89-
int sample_size_;
94+
const uint32_t sample_size_;
9095
/// size of the allocated storage, in bytes
91-
int storage_size_;
96+
uint32_t storage_size_;
9297
/// a slab of storage for pre-allocated samples
9398
char *storage_;
9499
/// a sentinel element for our freelist
@@ -116,15 +121,15 @@ class sample {
116121
/// the channel format
117122
lsl_channel_format_t format_;
118123
/// number of channels
119-
int num_channels_;
124+
uint32_t num_channels_;
120125
/// reference count used by sample_p
121126
std::atomic<int> refcount_;
122127
/// linked list of samples, for use in a freelist
123128
std::atomic<sample *> next_;
124129
/// the factory used to reclaim this sample, if any
125130
factory *factory_;
126131
/// the data payload begins here
127-
BOOST_ALIGNMENT(8) char data_;
132+
alignas(8) char data_;
128133

129134
public:
130135
// === Construction ===
@@ -150,14 +155,16 @@ class sample {
150155
/// Test for equality with another sample.
151156
bool operator==(const sample &rhs) const noexcept;
152157

158+
std::size_t datasize() const { return format_sizes[format_] * num_channels_; }
159+
153160
// === type-safe accessors ===
154161

155162
/// Assign an array of numeric values (with type conversions).
156163
template <class T> sample &assign_typed(const T *s) {
157164
if ((sizeof(T) == format_sizes[format_]) &&
158165
((std::is_integral<T>::value && format_integral[format_]) ||
159166
(std::is_floating_point<T>::value && format_float[format_]))) {
160-
memcpy(&data_, s, format_sizes[format_] * num_channels_);
167+
memcpy(&data_, s, datasize());
161168
} else {
162169
switch (format_) {
163170
case cft_float32:
@@ -207,7 +214,7 @@ class sample {
207214
if ((sizeof(T) == format_sizes[format_]) &&
208215
((std::is_integral<T>::value && format_integral[format_]) ||
209216
(std::is_floating_point<T>::value && format_float[format_]))) {
210-
memcpy(d, &data_, format_sizes[format_] * num_channels_);
217+
memcpy(d, &data_, datasize());
211218
} else {
212219
switch (format_) {
213220
case cft_float32:
@@ -258,7 +265,7 @@ class sample {
258265
/// Assign numeric data to the sample.
259266
sample &assign_untyped(const void *newdata) {
260267
if (format_ != cft_string)
261-
memcpy(&data_, newdata, format_sizes[format_] * num_channels_);
268+
memcpy(&data_, newdata, datasize());
262269
else
263270
throw std::invalid_argument("Cannot assign untyped data to a string-formatted sample.");
264271
return *this;
@@ -267,7 +274,7 @@ class sample {
267274
/// Retrieve numeric data from the sample.
268275
sample &retrieve_untyped(void *newdata) {
269276
if (format_ != cft_string)
270-
memcpy(newdata, &data_, format_sizes[format_] * num_channels_);
277+
memcpy(newdata, &data_, datasize());
271278
else
272279
throw std::invalid_argument(
273280
"Cannot retrieve untyped data from a string-formatted sample.");
@@ -295,7 +302,7 @@ class sample {
295302
}
296303

297304
/// Load a value from a stream buffer; specialization of the above.
298-
void load_value(std::streambuf &sb, uint8_t &v, int use_byte_order) {
305+
void load_value(std::streambuf &sb, uint8_t &v, int) {
299306
load_raw(sb, &v, sizeof(v));
300307
}
301308

@@ -353,7 +360,7 @@ class sample {
353360

354361
private:
355362
/// Construct a new sample for a given channel format/count combination.
356-
sample(lsl_channel_format_t fmt, int num_channels, factory *fact)
363+
sample(lsl_channel_format_t fmt, uint32_t num_channels, factory *fact)
357364
: format_(fmt), num_channels_(num_channels), refcount_(0), next_(nullptr), factory_(fact) {
358365
if (format_ == cft_string)
359366
for (std::string *p = (std::string *)&data_, *e = p + num_channels_; p < e;

src/send_buffer.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include <memory>
44

55

6-
// === implementation of the send_buffer class ===
7-
86
using namespace lsl;
97

108
/**

src/stream_info_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class stream_info_impl {
108108
const std::string &type() const { return type_; }
109109

110110
/// Get the number of channels of a stream.
111-
int channel_count() const { return channel_count_; }
111+
uint32_t channel_count() const { return channel_count_; }
112112

113113
/// Get the sampling rate of a stream (in Hz) as advertised by the device.
114114
double nominal_srate() const { return nominal_srate_; }
@@ -227,7 +227,7 @@ class stream_info_impl {
227227
// data information
228228
std::string name_;
229229
std::string type_;
230-
int channel_count_;
230+
uint32_t channel_count_;
231231
double nominal_srate_;
232232
lsl_channel_format_t channel_format_;
233233
std::string source_id_;

0 commit comments

Comments
 (0)