Skip to content

Commit 1bccbb6

Browse files
committed
Add internal mapping from API types to implementation types, remove casts
1 parent 8ea3088 commit 1bccbb6

File tree

8 files changed

+269
-176
lines changed

8 files changed

+269
-176
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ set(LSL_WINVER "0x0601" CACHE STRING
2828
set(lslobj_sources
2929
src/api_config.cpp
3030
src/api_config.h
31+
src/api_types.hpp
3132
src/cancellable_streambuf.h
3233
src/cancellation.h
3334
src/cancellation.cpp

src/api_types.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
/** @file api_types.hpp API type forwards
4+
*
5+
* The C API uses pointers to opaque structs to hide implementation details.
6+
*
7+
* This header defines the actual typedefs so the C++ implementation doesn't
8+
* have to do error-prone type casts between internal and API types.
9+
*/
10+
11+
#define LSL_TYPES
12+
13+
namespace lsl {
14+
class continuous_resolver_impl;
15+
class resolver_impl;
16+
class stream_info_impl;
17+
class stream_inlet_impl;
18+
class stream_outlet_impl;
19+
} // namespace lsl
20+
21+
namespace pugi {
22+
struct xml_node_struct;
23+
}
24+
typedef lsl::resolver_impl *lsl_continuous_resolver;
25+
typedef lsl::stream_info_impl *lsl_streaminfo;
26+
typedef lsl::stream_outlet_impl *lsl_outlet;
27+
typedef lsl::stream_inlet_impl *lsl_inlet;
28+
typedef pugi::xml_node_struct *lsl_xml_ptr;

src/common.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define COMMON_H
33

44
extern "C" {
5+
#include "api_types.hpp"
6+
57
#include "../include/lsl/common.h"
68
}
79
#include "loguru/loguru.hpp"
@@ -41,10 +43,10 @@ namespace lsl {
4143
const double FOREVER = 32000000.0;
4244

4345
/// Constant to indicate that a sample has the next successive time stamp.
44-
const double DEDUCED_TIMESTAMP = -1.0;
46+
const double DEDUCED_TIMESTAMP = -1.0;
4547

4648
/// Constant to indicate that a stream has variable sampling rate.
47-
const double IRREGULAR_RATE = 0.0;
49+
const double IRREGULAR_RATE = 0.0;
4850

4951
/// Obtain a local system time stamp in seconds.
5052
double lsl_clock();

src/lsl_inlet_c.cpp

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "stream_inlet_impl.h"
22

33
extern "C" {
4+
#include "api_types.hpp"
5+
46
#include "../include/lsl/inlet.h"
57
// === implementation of the stream_inlet class ===
68

@@ -23,8 +25,11 @@ using namespace lsl;
2325
*/
2426
LIBLSL_C_API lsl_inlet lsl_create_inlet(lsl_streaminfo info, int32_t max_buflen, int32_t max_chunklen, int32_t recover) {
2527
try {
26-
stream_info_impl *infoimpl = (stream_info_impl*)info;
27-
lsl_inlet result = (lsl_inlet)new stream_inlet_impl(*infoimpl, infoimpl->nominal_srate()?(int)(infoimpl->nominal_srate()*max_buflen):max_buflen*100, max_chunklen, recover!=0);
28+
stream_info_impl *infoimpl = info;
29+
lsl_inlet result = new stream_inlet_impl(*infoimpl,
30+
infoimpl->nominal_srate() ? (int)(infoimpl->nominal_srate() * max_buflen)
31+
: max_buflen * 100,
32+
max_chunklen, recover != 0);
2833
return result;
2934
}
3035
catch(std::invalid_argument &e) {
@@ -43,7 +48,7 @@ LIBLSL_C_API lsl_inlet lsl_create_inlet(lsl_streaminfo info, int32_t max_buflen,
4348
*/
4449
LIBLSL_C_API void lsl_destroy_inlet(lsl_inlet in) {
4550
try {
46-
delete (stream_inlet_impl*)in;
51+
delete in;
4752
} catch (std::exception &e) { LOG_F(ERROR, "Unexpected error in %s: %s", __func__, e.what()); }
4853
}
4954

@@ -57,7 +62,7 @@ LIBLSL_C_API lsl_streaminfo lsl_get_fullinfo(lsl_inlet in, double timeout, int32
5762
if (ec)
5863
*ec = lsl_no_error;
5964
try {
60-
return (lsl_streaminfo)new stream_info_impl(((stream_inlet_impl*)in)->info(timeout));
65+
return new stream_info_impl(in->info(timeout));
6166
}
6267
catch(timeout_error &) {
6368
if (ec)
@@ -87,7 +92,7 @@ LIBLSL_C_API void lsl_open_stream(lsl_inlet in, double timeout, int32_t *ec) {
8792
if (ec)
8893
*ec = lsl_no_error;
8994
try {
90-
((stream_inlet_impl*)in)->open_stream(timeout);
95+
in->open_stream(timeout);
9196
}
9297
catch(timeout_error &) {
9398
if (ec)
@@ -112,7 +117,7 @@ LIBLSL_C_API void lsl_open_stream(lsl_inlet in, double timeout, int32_t *ec) {
112117
*/
113118
LIBLSL_C_API void lsl_close_stream(lsl_inlet in) {
114119
try {
115-
((stream_inlet_impl*)in)->close_stream();
120+
in->close_stream();
116121
} catch (std::exception &e) { LOG_F(ERROR, "Unexpected error in %s: %s", __func__, e.what()); }
117122
}
118123

@@ -129,7 +134,7 @@ LIBLSL_C_API double lsl_time_correction(lsl_inlet in, double timeout, int32_t *e
129134
if (ec)
130135
*ec = lsl_no_error;
131136
try {
132-
return ((stream_inlet_impl*)in)->time_correction(timeout);
137+
return in->time_correction(timeout);
133138
}
134139
catch(timeout_error &) {
135140
if (ec)
@@ -151,7 +156,7 @@ LIBLSL_C_API double lsl_time_correction_ex(lsl_inlet in, double *remote_time, do
151156
if (ec)
152157
*ec = lsl_no_error;
153158
try {
154-
double correction = ((stream_inlet_impl*)in)->time_correction(remote_time, uncertainty, timeout);
159+
double correction = in->time_correction(remote_time, uncertainty, timeout);
155160
return correction;
156161
}
157162
catch(timeout_error &) {
@@ -174,7 +179,7 @@ LIBLSL_C_API double lsl_time_correction_ex(lsl_inlet in, double *remote_time, do
174179
*/
175180
LIBLSL_C_API int32_t lsl_set_postprocessing(lsl_inlet in, uint32_t flags) {
176181
try {
177-
((stream_inlet_impl*)in)->set_postprocessing(flags);
182+
in->set_postprocessing(flags);
178183
return lsl_no_error;
179184
}
180185
catch(std::invalid_argument &) {
@@ -199,27 +204,27 @@ LIBLSL_C_API int32_t lsl_set_postprocessing(lsl_inlet in, uint32_t flags) {
199204
* @param ec Error code: if nonzero, can be either lsl_timeout_error (if the timeout has expired) or lsl_lost_error (if the stream source has been lost).
200205
*/
201206
LIBLSL_C_API double lsl_pull_sample_f(lsl_inlet in, float *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
202-
return ((stream_inlet_impl*)in)->pull_sample_noexcept(buffer,buffer_elements,timeout,(lsl_error_code_t*) ec);
207+
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
203208
}
204209

205210
LIBLSL_C_API double lsl_pull_sample_d(lsl_inlet in, double *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
206-
return ((stream_inlet_impl*)in)->pull_sample_noexcept(buffer,buffer_elements,timeout,(lsl_error_code_t*) ec);
211+
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
207212
}
208213

209214
LIBLSL_C_API double lsl_pull_sample_l(lsl_inlet in, int64_t *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
210-
return ((stream_inlet_impl*)in)->pull_sample_noexcept(buffer,buffer_elements,timeout,(lsl_error_code_t*) ec);
215+
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
211216
}
212217

213218
LIBLSL_C_API double lsl_pull_sample_i(lsl_inlet in, int32_t *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
214-
return ((stream_inlet_impl*)in)->pull_sample_noexcept(buffer,buffer_elements,timeout,(lsl_error_code_t*) ec);
219+
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
215220
}
216221

217222
LIBLSL_C_API double lsl_pull_sample_s(lsl_inlet in, int16_t *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
218-
return ((stream_inlet_impl*)in)->pull_sample_noexcept(buffer,buffer_elements,timeout,(lsl_error_code_t*) ec);
223+
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
219224
}
220225

221226
LIBLSL_C_API double lsl_pull_sample_c(lsl_inlet in, char *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
222-
return ((stream_inlet_impl*)in)->pull_sample_noexcept(buffer,buffer_elements,timeout,(lsl_error_code_t*) ec);
227+
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
223228
}
224229

225230
LIBLSL_C_API double lsl_pull_sample_str(lsl_inlet in, char **buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
@@ -228,8 +233,8 @@ LIBLSL_C_API double lsl_pull_sample_str(lsl_inlet in, char **buffer, int32_t buf
228233
try {
229234
// capture output in a temporary string buffer
230235
std::vector<std::string> tmp;
231-
double result = ((stream_inlet_impl*)in)->pull_sample(tmp,timeout);
232-
if (buffer_elements < (int)tmp.size())
236+
double result = in->pull_sample(tmp, timeout);
237+
if (buffer_elements < (int)tmp.size())
233238
throw std::range_error("The provided buffer has fewer elements than the stream's number of channels.");
234239
// allocate memory and copy over into buffer
235240
for (std::size_t k=0;k<tmp.size();k++) {
@@ -274,8 +279,8 @@ LIBLSL_C_API double lsl_pull_sample_buf(lsl_inlet in, char **buffer, uint32_t *b
274279
try {
275280
// capture output in a temporary string buffer
276281
std::vector<std::string> tmp;
277-
double result = ((stream_inlet_impl*)in)->pull_sample(tmp,timeout);
278-
if (buffer_elements < (int)tmp.size())
282+
double result = in->pull_sample(tmp, timeout);
283+
if (buffer_elements < (int)tmp.size())
279284
throw std::range_error("The provided buffer has fewer elements than the stream's number of channels.");
280285
// allocate memory and copy over into buffer
281286
for (std::size_t k=0;k<tmp.size();k++) {
@@ -329,7 +334,7 @@ LIBLSL_C_API double lsl_pull_sample_v(lsl_inlet in, void *buffer, int32_t buffer
329334
if (ec)
330335
*ec = lsl_no_error;
331336
try {
332-
return ((stream_inlet_impl*)in)->pull_numeric_raw(buffer,buffer_bytes,timeout);
337+
return in->pull_numeric_raw(buffer, buffer_bytes, timeout);
333338
}
334339
catch(timeout_error &) {
335340
if (ec)
@@ -356,27 +361,33 @@ LIBLSL_C_API double lsl_pull_sample_v(lsl_inlet in, void *buffer, int32_t buffer
356361
}
357362

358363
LIBLSL_C_API unsigned long lsl_pull_chunk_f(lsl_inlet in, float *data_buffer, double *timestamp_buffer, unsigned long data_buffer_elements, unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
359-
return ((stream_inlet_impl*)in)->pull_chunk_multiplexed_noexcept(data_buffer,timestamp_buffer,data_buffer_elements,timestamp_buffer_elements,timeout,(lsl_error_code_t*)ec);
364+
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
365+
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
360366
}
361367

362368
LIBLSL_C_API unsigned long lsl_pull_chunk_d(lsl_inlet in, double *data_buffer, double *timestamp_buffer, unsigned long data_buffer_elements, unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
363-
return ((stream_inlet_impl*)in)->pull_chunk_multiplexed_noexcept(data_buffer,timestamp_buffer,data_buffer_elements,timestamp_buffer_elements,timeout,(lsl_error_code_t*)ec);
369+
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
370+
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
364371
}
365372

366373
LIBLSL_C_API unsigned long lsl_pull_chunk_l(lsl_inlet in, int64_t *data_buffer, double *timestamp_buffer, unsigned long data_buffer_elements, unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
367-
return ((stream_inlet_impl*)in)->pull_chunk_multiplexed_noexcept(data_buffer,timestamp_buffer,data_buffer_elements,timestamp_buffer_elements,timeout,(lsl_error_code_t*)ec);
374+
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
375+
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
368376
}
369377

370378
LIBLSL_C_API unsigned long lsl_pull_chunk_i(lsl_inlet in, int32_t *data_buffer, double *timestamp_buffer, unsigned long data_buffer_elements, unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
371-
return ((stream_inlet_impl*)in)->pull_chunk_multiplexed_noexcept(data_buffer,timestamp_buffer,data_buffer_elements,timestamp_buffer_elements,timeout,(lsl_error_code_t*)ec);
379+
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
380+
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
372381
}
373382

374383
LIBLSL_C_API unsigned long lsl_pull_chunk_s(lsl_inlet in, int16_t *data_buffer, double *timestamp_buffer, unsigned long data_buffer_elements, unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
375-
return ((stream_inlet_impl*)in)->pull_chunk_multiplexed_noexcept(data_buffer,timestamp_buffer,data_buffer_elements,timestamp_buffer_elements,timeout,(lsl_error_code_t*)ec);
384+
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
385+
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
376386
}
377387

378388
LIBLSL_C_API unsigned long lsl_pull_chunk_c(lsl_inlet in, char *data_buffer, double *timestamp_buffer, unsigned long data_buffer_elements, unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
379-
return ((stream_inlet_impl*)in)->pull_chunk_multiplexed_noexcept(data_buffer,timestamp_buffer,data_buffer_elements,timestamp_buffer_elements,timeout,(lsl_error_code_t*)ec);
389+
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
390+
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
380391
}
381392

382393
LIBLSL_C_API unsigned long lsl_pull_chunk_str(lsl_inlet in, char **data_buffer, double *timestamp_buffer, unsigned long data_buffer_elements, unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
@@ -386,7 +397,8 @@ LIBLSL_C_API unsigned long lsl_pull_chunk_str(lsl_inlet in, char **data_buffer,
386397
// capture output in a temporary string buffer
387398
if (data_buffer_elements) {
388399
std::vector<std::string> tmp(data_buffer_elements);
389-
unsigned long result = ((stream_inlet_impl*)in)->pull_chunk_multiplexed(&tmp[0],timestamp_buffer,data_buffer_elements,timestamp_buffer_elements,timeout);
400+
unsigned long result = in->pull_chunk_multiplexed(&tmp[0], timestamp_buffer,
401+
data_buffer_elements, timestamp_buffer_elements, timeout);
390402
// allocate memory and copy over into buffer
391403
for (std::size_t k=0;k<tmp.size();k++) {
392404
data_buffer[k] = (char*)malloc(tmp[k].size()+1);
@@ -433,7 +445,8 @@ LIBLSL_C_API unsigned long lsl_pull_chunk_buf(lsl_inlet in, char **data_buffer,
433445
// capture output in a temporary string buffer
434446
if (data_buffer_elements) {
435447
std::vector<std::string> tmp(data_buffer_elements);
436-
unsigned long result = ((stream_inlet_impl*)in)->pull_chunk_multiplexed(&tmp[0],timestamp_buffer,data_buffer_elements,timestamp_buffer_elements,timeout);
448+
unsigned long result = in->pull_chunk_multiplexed(&tmp[0], timestamp_buffer,
449+
data_buffer_elements, timestamp_buffer_elements, timeout);
437450
// allocate memory and copy over into buffer
438451
for (uint32_t k=0;k<tmp.size();k++) {
439452
data_buffer[k] = (char*)malloc(tmp[k].size()+1);
@@ -479,7 +492,7 @@ LIBLSL_C_API unsigned long lsl_pull_chunk_buf(lsl_inlet in, char **data_buffer,
479492
*/
480493
LIBLSL_C_API uint32_t lsl_samples_available(lsl_inlet in) {
481494
try {
482-
return (uint32_t)((stream_inlet_impl*)in)->samples_available();
495+
return (uint32_t)in->samples_available();
483496
}
484497
catch(std::exception &) {
485498
return 0;
@@ -491,7 +504,7 @@ LIBLSL_C_API uint32_t lsl_samples_available(lsl_inlet in) {
491504
*/
492505
LIBLSL_C_API uint32_t lsl_was_clock_reset(lsl_inlet in) {
493506
try {
494-
return (uint32_t)((stream_inlet_impl*)in)->was_clock_reset();
507+
return (uint32_t)in->was_clock_reset();
495508
}
496509
catch(std::exception &) {
497510
return 0;
@@ -503,7 +516,7 @@ LIBLSL_C_API uint32_t lsl_was_clock_reset(lsl_inlet in) {
503516
*/
504517
LIBLSL_C_API int32_t lsl_smoothing_halftime(lsl_inlet in, float value) {
505518
try {
506-
((stream_inlet_impl*)in)->smoothing_halftime(value);
519+
in->smoothing_halftime(value);
507520
return lsl_no_error;
508521
}
509522
catch(std::invalid_argument &) {

0 commit comments

Comments
 (0)