Skip to content

Commit b10c589

Browse files
committed
replacing reinterpret_cast with bit_cast
1 parent 61ee4ae commit b10c589

File tree

9 files changed

+98
-5619
lines changed

9 files changed

+98
-5619
lines changed

libtokamap/TODO.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
- [ ] Make mapping values case insensitive
22
- [ ] Remove references to IDSs
3-
- [ ] Replace reinterpret_cast with bit_cast
4-
- [ ] Replace boost::split with std::views::split
3+
- [x] ~~Replace reinterpret_cast with bit_cast~~ (still exist in map_arguments.hpp)
4+
- [x] ~~Replace boost::split with std::views::split~~
55
- [ ] Switch from using std::type_index to DataType enum?
66
- [ ] Add exception types
77
- [ ] Add README and docs for library
@@ -13,4 +13,4 @@
1313
- [ ] Replace std::string{} with string_literals
1414
- [ ] Fix logging
1515
- [ ] Add tests for parse_slices
16-
- [ ] Replace gsl::span with std::span
16+
- [x] ~~Replace gsl::span with std::span~~

libtokamap/ext_include/gsl/gsl-lite.hpp

Lines changed: 0 additions & 5544 deletions
This file was deleted.

libtokamap/src/handlers/mapping_handler.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "mapping_handler.hpp"
22

3-
#include <algorithm>
43
#include <cctype>
54
#include <cstddef>
65
#include <cstdint>
@@ -16,6 +15,7 @@
1615
#include <optional>
1716
#include <ostream>
1817
#include <ranges>
18+
#include <span>
1919
#include <sstream>
2020
#include <stdexcept>
2121
#include <string>
@@ -26,7 +26,6 @@
2626
#include <utility>
2727
#include <vector>
2828

29-
#include "gsl/gsl-lite.hpp"
3029
#include "map_types/base_mapping.hpp"
3130
#include "map_types/custom_mapping.hpp"
3231
#include "map_types/data_source_mapping.hpp"
@@ -68,7 +67,7 @@ std::ostream& operator<<(std::ostream& out, const std::vector<size_t>& shape)
6867

6968
template <typename T>
7069
requires(std::is_arithmetic_v<T>)
71-
std::ostream& operator<<(std::ostream& out, const gsl::span<const T>& data)
70+
std::ostream& operator<<(std::ostream& out, const std::span<const T>& data)
7271
{
7372
out << "[";
7473
out << std::setprecision(3);
@@ -90,7 +89,7 @@ std::ostream& operator<<(std::ostream& out, const gsl::span<const T>& data)
9089

9190
template <typename T> void print(std::ostream& out, const char* buffer, size_t size)
9291
{
93-
gsl::span<const T> data{reinterpret_cast<const T*>(buffer), size};
92+
std::span<const T> data{std::bit_cast<const T*>(buffer), size};
9493
out << ", data=" << data;
9594
}
9695
} // namespace

libtokamap/src/map_types/expr_mapping.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ template <typename T> TypedDataArray ExprMapping::eval_expr(const MapArguments&
8282
return array;
8383
}
8484

85-
const T* raw_data = reinterpret_cast<const T*>(array.buffer());
85+
const T* raw_data = std::bit_cast<const T*>(array.buffer());
8686
size_t data_size = array.size();
8787

8888
if (data_size > 1) {

libtokamap/src/map_types/map_arguments.hpp

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <cstdint>
55
#include <cstdlib>
66
#include <cstring>
7-
#include <gsl/gsl-lite.hpp>
87
#include <limits>
98
#include <memory>
109
#include <nlohmann/json.hpp>
@@ -22,7 +21,21 @@ namespace libtokamap
2221

2322
enum class SignalType : uint8_t { DEFAULT, DATA, TIME, ERROR, DIM, INVALID };
2423

25-
enum class DataType : uint8_t { Unknown, Char, Short, Int, Long, Int64, UChar, UShort, UInt, ULong, UInt64, Float, Double };
24+
enum class DataType : uint8_t {
25+
Unknown,
26+
Char,
27+
Short,
28+
Int,
29+
Long,
30+
Int64,
31+
UChar,
32+
UShort,
33+
UInt,
34+
ULong,
35+
UInt64,
36+
Float,
37+
Double
38+
};
2639

2740
inline DataType type_index_map(std::type_index type_index)
2841
{
@@ -162,10 +175,10 @@ class TypedDataArray
162175
throw std::runtime_error{"invalid type given to apply"};
163176
}
164177

165-
gsl::span<T> data{reinterpret_cast<T*>(m_buffer), m_size};
166-
for (T& element : data) {
167-
element *= scale_factor;
168-
element += offset;
178+
auto* data = reinterpret_cast<T*>(m_buffer);
179+
for (size_t idx = 0; idx < m_size; ++idx) {
180+
data[idx] *= scale_factor;
181+
data[idx] += offset;
169182
}
170183
}
171184

@@ -194,16 +207,16 @@ class TypedDataArray
194207
new_size *= len;
195208
}
196209

197-
gsl::span<T> array{reinterpret_cast<T*>(m_buffer), m_size};
210+
auto* array = reinterpret_cast<T*>(m_buffer);
198211

199212
auto* new_buffer = new char[sizeof(T) * new_size];
200-
gsl::span<T> new_array{reinterpret_cast<T*>(new_buffer), new_size};
213+
auto* new_array = reinterpret_cast<T*>(new_buffer);
201214

202215
auto offsets = compute_offsets(m_shape, subsets);
203-
size_t n = 0;
216+
size_t idx = 0;
204217
for (const auto offset : offsets) {
205-
new_array[n] = array[offset];
206-
++n;
218+
new_array[idx] = array[offset];
219+
++idx;
207220
}
208221

209222
if (m_owning) {
@@ -227,12 +240,23 @@ class TypedDataArray
227240

228241
[[nodiscard]] char* buffer() const { return m_buffer; }
229242

230-
template <typename T> [[nodiscard]] gsl::span<T> span() const
243+
#if __cplusplus >= 202002L
244+
template <typename T> [[nodiscard]] std::span<T> span() const
245+
{
246+
if (m_type_index != std::type_index{typeid(T)}) {
247+
throw std::runtime_error{"invalid type given to span"};
248+
}
249+
return std::span<T>{reinterpret_cast<T*>(m_buffer), m_size};
250+
}
251+
#endif
252+
253+
template <typename T> [[nodiscard]] std::vector<T> as_vector() const
231254
{
232255
if (m_type_index != std::type_index{typeid(T)}) {
233256
throw std::runtime_error{"invalid type given to span"};
234257
}
235-
return gsl::span<T>{reinterpret_cast<T*>(m_buffer), m_size};
258+
const T* ptr = reinterpret_cast<T*>(m_buffer);
259+
return std::vector<T>{ptr, ptr + m_size};
236260
}
237261

238262
[[nodiscard]] size_t element_size()

libtokamap/src/utils/scale_offset.cpp

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "scale_offset.hpp"
22

3+
#include <bit>
34
#include <cstdlib>
4-
#include <gsl/gsl-lite.hpp>
5+
#include <span>
56
#include <stdexcept>
67

78
#include "map_types/map_arguments.hpp"
@@ -15,7 +16,7 @@ template <typename T> int offset_value(T& var, float offset)
1516
return 0;
1617
}
1718

18-
template <typename T> int offset_span(gsl::span<T> span, float offset)
19+
template <typename T> int offset_span(std::span<T> span, float offset)
1920
{
2021
if (span.empty()) {
2122
return 1;
@@ -32,7 +33,7 @@ template <typename T> int scale_value(T& var, float scale)
3233
return 0;
3334
}
3435

35-
template <typename T> int scale_span(gsl::span<T> span, float scale)
36+
template <typename T> int scale_span(std::span<T> span, float scale)
3637
{
3738
if (span.empty()) {
3839
return 1;
@@ -52,28 +53,28 @@ int libtokamap::map_transform::transform_offset(TypedDataArray& array, float off
5253
const size_t size = array.size();
5354
switch (type_index_map(array.type_index())) {
5455
case DataType::Short: {
55-
auto* data = reinterpret_cast<short*>(array.buffer());
56-
err = offset_span(gsl::span{data, size}, offset);
56+
auto* data = std::bit_cast<short*>(array.buffer());
57+
err = offset_span(std::span{data, size}, offset);
5758
break;
5859
}
5960
case DataType::Int: {
60-
auto* data = reinterpret_cast<int*>(array.buffer());
61-
err = offset_span(gsl::span{data, size}, offset);
61+
auto* data = std::bit_cast<int*>(array.buffer());
62+
err = offset_span(std::span{data, size}, offset);
6263
break;
6364
}
6465
case DataType::Long: {
65-
auto* data = reinterpret_cast<long*>(array.buffer());
66-
err = offset_span(gsl::span{data, size}, offset);
66+
auto* data = std::bit_cast<long*>(array.buffer());
67+
err = offset_span(std::span{data, size}, offset);
6768
break;
6869
}
6970
case DataType::Float: {
70-
auto* data = reinterpret_cast<float*>(array.buffer());
71-
err = offset_span(gsl::span{data, size}, offset);
71+
auto* data = std::bit_cast<float*>(array.buffer());
72+
err = offset_span(std::span{data, size}, offset);
7273
break;
7374
}
7475
case DataType::Double: {
75-
auto* data = reinterpret_cast<double*>(array.buffer());
76-
err = offset_span(gsl::span{data, size}, offset);
76+
auto* data = std::bit_cast<double*>(array.buffer());
77+
err = offset_span(std::span{data, size}, offset);
7778
break;
7879
}
7980
default:
@@ -82,27 +83,27 @@ int libtokamap::map_transform::transform_offset(TypedDataArray& array, float off
8283
} else {
8384
switch (type_index_map(array.type_index())) {
8485
case DataType::Short: {
85-
auto* data = reinterpret_cast<short*>(array.buffer());
86+
auto* data = std::bit_cast<short*>(array.buffer());
8687
err = offset_value(data, offset);
8788
break;
8889
}
8990
case DataType::Int: {
90-
auto* data = reinterpret_cast<int*>(array.buffer());
91+
auto* data = std::bit_cast<int*>(array.buffer());
9192
err = offset_value(data, offset);
9293
break;
9394
}
9495
case DataType::Long: {
95-
auto* data = reinterpret_cast<long*>(array.buffer());
96+
auto* data = std::bit_cast<long*>(array.buffer());
9697
err = offset_value(data, offset);
9798
break;
9899
}
99100
case DataType::Float: {
100-
auto* data = reinterpret_cast<float*>(array.buffer());
101+
auto* data = std::bit_cast<float*>(array.buffer());
101102
err = offset_value(data, offset);
102103
break;
103104
}
104105
case DataType::Double: {
105-
auto* data = reinterpret_cast<double*>(array.buffer());
106+
auto* data = std::bit_cast<double*>(array.buffer());
106107
err = offset_value(data, offset);
107108
break;
108109
}
@@ -121,28 +122,28 @@ int libtokamap::map_transform::transform_scale(TypedDataArray& array, float scal
121122
const size_t size = array.size();
122123
switch (type_index_map(array.type_index())) {
123124
case DataType::Short: {
124-
auto* data = reinterpret_cast<short*>(array.buffer());
125-
err = scale_span(gsl::span{data, size}, scale);
125+
auto* data = std::bit_cast<short*>(array.buffer());
126+
err = scale_span(std::span{data, size}, scale);
126127
break;
127128
}
128129
case DataType::Int: {
129-
auto* data = reinterpret_cast<int*>(array.buffer());
130-
err = scale_span(gsl::span{data, size}, scale);
130+
auto* data = std::bit_cast<int*>(array.buffer());
131+
err = scale_span(std::span{data, size}, scale);
131132
break;
132133
}
133134
case DataType::Long: {
134-
auto* data = reinterpret_cast<long*>(array.buffer());
135-
err = scale_span(gsl::span{data, size}, scale);
135+
auto* data = std::bit_cast<long*>(array.buffer());
136+
err = scale_span(std::span{data, size}, scale);
136137
break;
137138
}
138139
case DataType::Float: {
139-
auto* data = reinterpret_cast<float*>(array.buffer());
140-
err = scale_span(gsl::span{data, size}, scale);
140+
auto* data = std::bit_cast<float*>(array.buffer());
141+
err = scale_span(std::span{data, size}, scale);
141142
break;
142143
}
143144
case DataType::Double: {
144-
auto* data = reinterpret_cast<double*>(array.buffer());
145-
err = scale_span(gsl::span{data, size}, scale);
145+
auto* data = std::bit_cast<double*>(array.buffer());
146+
err = scale_span(std::span{data, size}, scale);
146147
break;
147148
}
148149
default:
@@ -151,27 +152,27 @@ int libtokamap::map_transform::transform_scale(TypedDataArray& array, float scal
151152
} else {
152153
switch (type_index_map(array.type_index())) {
153154
case DataType::Short: {
154-
auto* data = reinterpret_cast<short*>(array.buffer());
155+
auto* data = std::bit_cast<short*>(array.buffer());
155156
err = scale_value(data, scale);
156157
break;
157158
}
158159
case DataType::Int: {
159-
auto* data = reinterpret_cast<int*>(array.buffer());
160+
auto* data = std::bit_cast<int*>(array.buffer());
160161
err = scale_value(data, scale);
161162
break;
162163
}
163164
case DataType::Long: {
164-
auto* data = reinterpret_cast<long*>(array.buffer());
165+
auto* data = std::bit_cast<long*>(array.buffer());
165166
err = scale_value(data, scale);
166167
break;
167168
}
168169
case DataType::Float: {
169-
auto* data = reinterpret_cast<float*>(array.buffer());
170+
auto* data = std::bit_cast<float*>(array.buffer());
170171
err = scale_value(data, scale);
171172
break;
172173
}
173174
case DataType::Double: {
174-
auto* data = reinterpret_cast<double*>(array.buffer());
175+
auto* data = std::bit_cast<double*>(array.buffer());
175176
err = scale_value(data, scale);
176177
break;
177178
}

libtokamap/src/utils/subset.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <cstdint>
55
#include <cstdlib>
66
#include <ctre/ctre.hpp>
7-
#include <gsl/gsl-lite.hpp>
87
#include <optional>
98
#include <stdexcept>
109
#include <string>
@@ -156,7 +155,7 @@ void apply_scale_offset(libtokamap::TypedDataArray& input, std::optional<float>
156155
} // namespace
157156

158157
void libtokamap::subset::update_array(libtokamap::TypedDataArray& input, const std::optional<std::string>& slice,
159-
std::optional<float> scale_factor, std::optional<float> offset)
158+
std::optional<float> scale_factor, std::optional<float> offset)
160159
{
161160
apply_subset(input, slice);
162161
apply_scale_offset(input, scale_factor, offset);

0 commit comments

Comments
 (0)