Skip to content

Commit a2cc8d4

Browse files
committed
Hide sample::timestamp_ behind an accessor function
1 parent 86ded83 commit a2cc8d4

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

src/data_receiver.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ double data_receiver::pull_sample_typed(T *buffer, uint32_t buffer_elements, dou
108108
throw std::range_error("The number of buffer elements provided does not match the "
109109
"number of channels in the sample.");
110110
s->retrieve_typed(buffer);
111-
return s->timestamp;
111+
return s->timestamp();
112112
} else return 0.0;
113113
}
114114

@@ -126,7 +126,7 @@ double data_receiver::pull_sample_untyped(void *buffer, int buffer_bytes, double
126126
throw std::range_error("The size of the provided buffer does not match the number of "
127127
"bytes in the sample.");
128128
s->retrieve_untyped(buffer);
129-
return s->timestamp;
129+
return s->timestamp();
130130
}
131131
else return 0.0;
132132
}
@@ -316,11 +316,11 @@ void data_receiver::data_thread() {
316316
else
317317
*inarch >> *samp;
318318
// deduce timestamp if necessary
319-
if (samp->timestamp == DEDUCED_TIMESTAMP) {
320-
samp->timestamp = last_timestamp;
321-
if (srate != IRREGULAR_RATE) samp->timestamp += 1.0 / srate;
319+
if (samp->timestamp() == DEDUCED_TIMESTAMP) {
320+
samp->timestamp() = last_timestamp;
321+
if (srate != IRREGULAR_RATE) samp->timestamp() += 1.0 / srate;
322322
}
323-
last_timestamp = samp->timestamp;
323+
last_timestamp = samp->timestamp();
324324
// push it into the sample queue
325325
sample_queue_.push_sample(samp);
326326
// periodically update the last receive time to keep the watchdog happy

src/sample.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ lsl::sample::~sample() noexcept {
9393
}
9494

9595
bool sample::operator==(const sample &rhs) const noexcept {
96-
if ((timestamp != rhs.timestamp) || (format_ != rhs.format_) ||
96+
if ((timestamp_ != rhs.timestamp_) || (format_ != rhs.format_) ||
9797
(num_channels_ != rhs.num_channels_))
9898
return false;
9999
if (format_ != cft_string)
@@ -189,11 +189,11 @@ template <typename T> void save_value(std::streambuf &sb, T v, bool reverse_byte
189189
void sample::save_streambuf(
190190
std::streambuf &sb, int /*protocol_version*/, bool reverse_byte_order, void *scratchpad) const {
191191
// write sample header
192-
if (timestamp == DEDUCED_TIMESTAMP) {
192+
if (timestamp_ == DEDUCED_TIMESTAMP) {
193193
save_byte(sb, TAG_DEDUCED_TIMESTAMP);
194194
} else {
195195
save_byte(sb, TAG_TRANSMITTED_TIMESTAMP);
196-
save_value(sb, timestamp, reverse_byte_order);
196+
save_value(sb, timestamp_, reverse_byte_order);
197197
}
198198
// write channel data
199199
if (format_ == cft_string) {
@@ -231,10 +231,10 @@ void sample::load_streambuf(
231231
// read sample header
232232
if (load_byte(sb) == TAG_DEDUCED_TIMESTAMP)
233233
// deduce the timestamp
234-
timestamp = DEDUCED_TIMESTAMP;
234+
timestamp_ = DEDUCED_TIMESTAMP;
235235
else
236236
// read the time stamp
237-
timestamp = load_value<double>(sb, reverse_byte_order);
237+
timestamp_ = load_value<double>(sb, reverse_byte_order);
238238

239239
// read channel data
240240
if (format_ == cft_string) {
@@ -329,10 +329,10 @@ template <class Archive> void sample::serialize_channels(Archive &ar, const uint
329329

330330
void lsl::sample::serialize(eos::portable_oarchive &ar, const uint32_t archive_version) const {
331331
// write sample header
332-
if (timestamp == DEDUCED_TIMESTAMP) {
332+
if (timestamp_ == DEDUCED_TIMESTAMP) {
333333
ar &TAG_DEDUCED_TIMESTAMP;
334334
} else {
335-
ar &TAG_TRANSMITTED_TIMESTAMP &timestamp;
335+
ar &TAG_TRANSMITTED_TIMESTAMP &timestamp_;
336336
}
337337
// write channel data
338338
const_cast<sample *>(this)->serialize_channels(ar, archive_version);
@@ -344,10 +344,10 @@ void lsl::sample::serialize(eos::portable_iarchive &ar, const uint32_t archive_v
344344
ar &tag;
345345
if (tag == TAG_DEDUCED_TIMESTAMP) {
346346
// deduce the timestamp
347-
timestamp = DEDUCED_TIMESTAMP;
347+
timestamp_ = DEDUCED_TIMESTAMP;
348348
} else {
349349
// read the time stamp
350-
ar &timestamp;
350+
ar &timestamp_;
351351
}
352352
// read channel data
353353
serialize_channels(ar, archive_version);
@@ -364,7 +364,7 @@ template <typename T> void test_pattern(T *data, uint32_t num_channels, int offs
364364

365365
sample &sample::assign_test_pattern(int offset) {
366366
pushthrough = true;
367-
timestamp = 123456.789;
367+
timestamp_ = 123456.789;
368368

369369
switch (format_) {
370370
case cft_float32:
@@ -436,7 +436,7 @@ sample_p factory::new_sample(double timestamp, bool pushthrough) {
436436
sample *result = pop_freelist();
437437
if (!result)
438438
result = new (new char[sample_size_]) sample(fmt_, num_chans_, this);
439-
result->timestamp = timestamp;
439+
result->timestamp_ = timestamp;
440440
result->pushthrough = pushthrough;
441441
return sample_p(result);
442442
}

src/sample.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ class factory {
8585
class sample {
8686
public:
8787
friend class factory;
88-
/// time-stamp of the sample
89-
double timestamp{0.0};
9088
/// whether the sample shall be buffered or pushed through
9189
bool pushthrough{false};
9290

@@ -101,6 +99,8 @@ class sample {
10199
std::atomic<sample *> next_;
102100
/// the factory used to reclaim this sample, if any
103101
factory *factory_;
102+
/// time-stamp of the sample
103+
double timestamp_{0.0};
104104
/// the data payload begins here
105105
alignas(8) int32_t data_{0};
106106

@@ -110,6 +110,8 @@ class sample {
110110
/// Destructor for a sample.
111111
~sample() noexcept;
112112

113+
double &timestamp() { return timestamp_; }
114+
113115
/// Delete a sample.
114116
void operator delete(void *x) noexcept;
115117

testing/int/samples.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ TEST_CASE("consumer_queue", "[queue][basic]") {
1616
CHECK(queue.read_available() == size);
1717

1818
// Are the right samples dropped when going over capacity?
19-
CHECK(static_cast<int>(queue.pop_sample()->timestamp) == 1);
19+
CHECK(static_cast<int>(queue.pop_sample()->timestamp()) == 1);
2020

2121
// Does flush() return the correct count?
2222
CHECK(queue.flush() == size - 1);

0 commit comments

Comments
 (0)