Commit 23b49aab authored by Dominik Charousset's avatar Dominik Charousset

implemented serialization of floating point numbers

parent 429872ba
...@@ -28,8 +28,10 @@ ...@@ -28,8 +28,10 @@
\******************************************************************************/ \******************************************************************************/
#include <string>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <sstream>
#include <iterator> #include <iterator>
#include <exception> #include <exception>
#include <stdexcept> #include <stdexcept>
...@@ -51,13 +53,6 @@ inline void range_check(iterator begin, iterator end, size_t read_size) { ...@@ -51,13 +53,6 @@ inline void range_check(iterator begin, iterator end, size_t read_size) {
} }
} }
// @returns the next iterator position
template<typename T>
iterator read(iterator, iterator, T&,
typename enable_if<std::is_floating_point<T>::value>::type* = 0) {
throw std::logic_error("read floating point not implemented yet");
}
template<typename T> template<typename T>
iterator read(iterator begin, iterator end, T& storage, iterator read(iterator begin, iterator end, T& storage,
typename enable_if<std::is_integral<T>::value>::type* = 0) { typename enable_if<std::is_integral<T>::value>::type* = 0) {
...@@ -90,6 +85,18 @@ iterator read_unicode_string(iterator begin, iterator end, StringType& str) { ...@@ -90,6 +85,18 @@ iterator read_unicode_string(iterator begin, iterator end, StringType& str) {
return begin; return begin;
} }
// @returns the next iterator position
template<typename T>
iterator read(iterator begin, iterator end, T& value,
typename enable_if<std::is_floating_point<T>::value>::type* = 0) {
// floating points are written as strings
std::string str;
auto result = read_unicode_string<char>(begin, end, str);
std::istringstream iss(str);
iss >> value;
return result;
}
iterator read(iterator begin, iterator end, std::u16string& storage) { iterator read(iterator begin, iterator end, std::u16string& storage) {
// char16_t is guaranteed to has *at least* 16 bytes, // char16_t is guaranteed to has *at least* 16 bytes,
// but not to have *exactly* 16 bytes; thus use uint16_t // but not to have *exactly* 16 bytes; thus use uint16_t
......
...@@ -69,10 +69,11 @@ class binary_writer { ...@@ -69,10 +69,11 @@ class binary_writer {
} }
template<typename T> template<typename T>
void operator()(const T&, void operator()(const T& value,
typename enable_if<std::is_floating_point<T>::value>::type* = 0) { typename enable_if<std::is_floating_point<T>::value>::type* = 0) {
throw std::logic_error("binary_serializer::write_floating_point " // write floating points as strings
"not implemented yet"); std::string str = std::to_string(value);
(*this)(str);
} }
void operator()(const std::string& str) { void operator()(const std::string& str) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment