Fixed issies with multiple definitions
This commit is contained in:
parent
7201035a20
commit
d3c6337068
|
@ -36,30 +36,10 @@ namespace io {
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
float read(std::istream& is) {
|
float read(std::istream& is);
|
||||||
union {
|
|
||||||
uint32_t a;
|
|
||||||
float b;
|
|
||||||
} u;
|
|
||||||
|
|
||||||
u.a = read_bytes<uint32_t>(is, sizeof(uint32_t));
|
|
||||||
|
|
||||||
return u.b;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
size_t read(std::istream& is) {
|
size_t read(std::istream& is);
|
||||||
size_t value = 0;
|
|
||||||
uint8_t length = io::read<uint8_t>(is);
|
|
||||||
if (length & 0b10000000) {
|
|
||||||
value = read_bytes<size_t>(is, length & 0b01111111);
|
|
||||||
} else {
|
|
||||||
value = length;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T> requires std::is_convertible_v<std::string, T>
|
template <typename T> requires std::is_convertible_v<std::string, T>
|
||||||
T read(std::istream& is, size_t length) {
|
T read(std::istream& is, size_t length) {
|
||||||
|
|
|
@ -32,35 +32,11 @@ namespace io {
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void write(std::ostream& os, float value) {
|
void write(std::ostream& os, float value);
|
||||||
union {
|
|
||||||
uint32_t a;
|
|
||||||
float b;
|
|
||||||
} u;
|
|
||||||
|
|
||||||
u.b = value;
|
|
||||||
write_bytes(os, u.a, sizeof(uint32_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Special implementation for size_t (also uint64_t, so maybe this is not that smart)
|
// Special implementation for size_t (also uint64_t, so maybe this is not that smart)
|
||||||
template <>
|
template <>
|
||||||
void write(std::ostream& os, size_t value) {
|
void write(std::ostream& os, size_t value);
|
||||||
// Check if we need more then one byte
|
|
||||||
if (value > 0b01111111) {
|
|
||||||
// Calculate how many bytes we need to store the number
|
|
||||||
uint8_t length = 0;
|
|
||||||
auto x = value;
|
|
||||||
while (x != 0) {
|
|
||||||
x >>= 8;
|
|
||||||
length++;
|
|
||||||
}
|
|
||||||
|
|
||||||
write<uint8_t>(os, length | 0b10000000);
|
|
||||||
write_bytes(os, value, length);
|
|
||||||
} else {
|
|
||||||
write<uint8_t>(os, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> requires std::is_convertible_v<T, std::string_view>
|
template <typename T> requires std::is_convertible_v<T, std::string_view>
|
||||||
void write(std::ostream& os, T value, bool store_length = true) {
|
void write(std::ostream& os, T value, bool store_length = true) {
|
||||||
|
|
27
src/read.cpp
Normal file
27
src/read.cpp
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#include "io/read.h"
|
||||||
|
|
||||||
|
namespace io {
|
||||||
|
template<>
|
||||||
|
float read(std::istream& is) {
|
||||||
|
union {
|
||||||
|
uint32_t a;
|
||||||
|
float b;
|
||||||
|
} u;
|
||||||
|
|
||||||
|
u.a = read_bytes<uint32_t>(is, sizeof(uint32_t));
|
||||||
|
|
||||||
|
return u.b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
size_t read(std::istream& is) {
|
||||||
|
size_t value = 0;
|
||||||
|
uint8_t length = io::read<uint8_t>(is);
|
||||||
|
if (length & 0b10000000) {
|
||||||
|
value = read_bytes<size_t>(is, length & 0b01111111);
|
||||||
|
} else {
|
||||||
|
value = length;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,34 @@
|
||||||
#include "io/write.h"
|
#include "io/write.h"
|
||||||
|
|
||||||
// We do not implement anything without templates
|
namespace io {
|
||||||
|
template<>
|
||||||
|
void write(std::ostream& os, float value) {
|
||||||
|
union {
|
||||||
|
uint32_t a;
|
||||||
|
float b;
|
||||||
|
} u;
|
||||||
|
|
||||||
|
u.b = value;
|
||||||
|
write_bytes(os, u.a, sizeof(uint32_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Special implementation for size_t (also uint64_t, so maybe this is not that smart)
|
||||||
|
template<>
|
||||||
|
void write(std::ostream& os, size_t value) {
|
||||||
|
// Check if we need more then one byte
|
||||||
|
if (value > 0b01111111) {
|
||||||
|
// Calculate how many bytes we need to store the number
|
||||||
|
uint8_t length = 0;
|
||||||
|
auto x = value;
|
||||||
|
while (x != 0) {
|
||||||
|
x >>= 8;
|
||||||
|
length++;
|
||||||
|
}
|
||||||
|
|
||||||
|
write<uint8_t>(os, length | 0b10000000);
|
||||||
|
write_bytes(os, value, length);
|
||||||
|
} else {
|
||||||
|
write<uint8_t>(os, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user