Skip to content

Commit c09ca0a

Browse files
committed
Implement array macros as functions for C++
1 parent 9de9afb commit c09ca0a

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

examples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ add_example(check_equivalence)
1313
add_example(bsp-ls)
1414
add_example(benchmark_read)
1515
add_example(benchmark_write)
16+
17+
add_subdirectory(cpp)

include/binsparse/array.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,7 @@ void bsp_destroy_array_t(bsp_array_t array) {
339339
}
340340

341341
#endif
342+
343+
#ifdef __cplusplus
344+
#include <binsparse/detail/cpp/array.hpp>
345+
#endif
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
5+
#include <type_traits>
6+
#include <variant>
7+
8+
namespace binsparse {
9+
10+
namespace __detail {
11+
12+
using array_ptr_variant_t =
13+
std::variant<uint8_t*, uint16_t*, uint32_t*, uint64_t*, int8_t*, int16_t*,
14+
int32_t*, int64_t*, float*, double*, float _Complex*,
15+
double _Complex*>;
16+
17+
array_ptr_variant_t get_typed_ptr(bsp_array_t array) {
18+
if (array.type == BSP_UINT8) {
19+
uint8_t* data = (uint8_t*) array.data;
20+
return data;
21+
} else if (array.type == BSP_UINT16) {
22+
uint16_t* data = (uint16_t*) array.data;
23+
return data;
24+
} else if (array.type == BSP_UINT32) {
25+
uint32_t* data = (uint32_t*) array.data;
26+
return data;
27+
} else if (array.type == BSP_UINT64) {
28+
uint64_t* data = (uint64_t*) array.data;
29+
return data;
30+
} else if (array.type == BSP_INT8) {
31+
int8_t* data = (int8_t*) array.data;
32+
return data;
33+
} else if (array.type == BSP_INT16) {
34+
int16_t* data = (int16_t*) array.data;
35+
return data;
36+
} else if (array.type == BSP_INT32) {
37+
int32_t* data = (int32_t*) array.data;
38+
return data;
39+
} else if (array.type == BSP_INT64) {
40+
int64_t* data = (int64_t*) array.data;
41+
return data;
42+
} else if (array.type == BSP_FLOAT32) {
43+
float* data = (float*) array.data;
44+
return data;
45+
} else if (array.type == BSP_FLOAT64) {
46+
double* data = (double*) array.data;
47+
return data;
48+
} else if (array.type == BSP_BINT8) {
49+
int8_t* data = (int8_t*) array.data;
50+
return data;
51+
} else if (array.type == BSP_COMPLEX_FLOAT32) {
52+
float _Complex* data = (float _Complex*) array.data;
53+
return data;
54+
} else if (array.type == BSP_COMPLEX_FLOAT64) {
55+
double _Complex* data = (double _Complex*) array.data;
56+
return data;
57+
}
58+
return {};
59+
}
60+
61+
} // namespace __detail
62+
63+
} // namespace binsparse
64+
65+
// value = array[index]
66+
template <typename T>
67+
void bsp_array_read(bsp_array_t array, size_t index, T& value) {
68+
auto variant_ptr = binsparse::__detail::get_typed_ptr(array);
69+
70+
std::visit(
71+
[&](auto* ptr) {
72+
using U = std::remove_pointer_t<decltype(ptr)>;
73+
74+
if constexpr (std::is_assignable_v<T, U>) {
75+
value = ptr[index];
76+
}
77+
},
78+
variant_ptr);
79+
}
80+
81+
// array[index] = value
82+
template <typename U>
83+
void bsp_array_write(bsp_array_t array, size_t index, U value) {
84+
auto variant_ptr = binsparse::__detail::get_typed_ptr(array);
85+
86+
std::visit(
87+
[&](auto* ptr) {
88+
using T = std::remove_pointer_t<decltype(ptr)>;
89+
90+
if constexpr (std::is_assignable_v<T, U>) {
91+
ptr[index] = value;
92+
}
93+
},
94+
variant_ptr);
95+
}
96+
97+
// array_0[index_0] = array_1[index_1]
98+
void bsp_array_awrite(bsp_array_t array_0, size_t index_0, bsp_array_t array_1,
99+
size_t index_1) {
100+
auto variant_ptr_0 = binsparse::__detail::get_typed_ptr(array_0);
101+
auto variant_ptr_1 = binsparse::__detail::get_typed_ptr(array_1);
102+
103+
std::visit(
104+
[&](auto* ptr_0, auto* ptr_1) {
105+
using T = std::remove_pointer_t<decltype(ptr_0)>;
106+
using U = std::remove_pointer_t<decltype(ptr_1)>;
107+
108+
if constexpr (std::is_assignable_v<T, U>) {
109+
ptr_0[index_0] = ptr_1[index_1];
110+
}
111+
},
112+
variant_ptr_0, variant_ptr_1);
113+
}
114+
115+
#endif

0 commit comments

Comments
 (0)