Commit 16c48252 authored by Dominik Charousset's avatar Dominik Charousset

Refactor `binary_serializer` internals

Let the internal functor use a pointer + number of bytes instead of two
pointers. This saves some code in the implementation by moving the computation
of the two iterators to the functor and also should get rid of a false positive
in Coverity.
parent 3ceb5266
...@@ -41,15 +41,16 @@ class binary_serializer : public serializer { ...@@ -41,15 +41,16 @@ class binary_serializer : public serializer {
public: public:
using write_fun = std::function<void(const char*, const char*)>; using write_fun = std::function<void(const char*, size_t)>;
/// Creates a binary serializer writing to given iterator position. /// Creates a binary serializer writing to given iterator position.
template <class OutIter> template <class OutIter>
binary_serializer(OutIter iter, actor_namespace* ns = nullptr) : super(ns) { explicit binary_serializer(OutIter iter, actor_namespace* ns = nullptr)
: super(ns) {
struct fun { struct fun {
fun(OutIter pos) : pos_(pos) {} fun(OutIter pos) : pos_(pos) {}
void operator()(const char* first, const char* last) { void operator()(const char* first, size_t num_bytes) {
pos_ = std::copy(first, last, pos_); pos_ = std::copy(first, first + num_bytes, pos_);
} }
OutIter pos_; OutIter pos_;
}; };
...@@ -69,9 +70,7 @@ public: ...@@ -69,9 +70,7 @@ public:
void write_raw(size_t num_bytes, const void* data) override; void write_raw(size_t num_bytes, const void* data) override;
private: private:
write_fun out_; write_fun out_;
}; };
template <class T, template <class T,
......
...@@ -33,21 +33,16 @@ public: ...@@ -33,21 +33,16 @@ public:
template <class T> template <class T>
static inline void write_int(write_fun& f, const T& value) { static inline void write_int(write_fun& f, const T& value) {
auto first = reinterpret_cast<const char*>(&value); f(reinterpret_cast<const char*>(&value), sizeof(T));
auto last = first + sizeof(T);
f(first, last);
} }
static inline void write_string(write_fun& f, const std::string& str) { static inline void write_string(write_fun& f, const std::string& str) {
write_int(f, static_cast<uint32_t>(str.size())); write_int(f, static_cast<uint32_t>(str.size()));
auto first = str.data(); f(str.data(), str.size());
auto last = first + str.size();
f(first, last);
} }
void operator()(const bool& value) const { void operator()(const bool& value) const {
uint8_t tmp = value ? 1 : 0; write_int(out_, static_cast<uint8_t>(value ? 1 : 0));
write_int(out_, tmp);
} }
template <class T> template <class T>
...@@ -127,9 +122,7 @@ void binary_serializer::write_value(const primitive_variant& value) { ...@@ -127,9 +122,7 @@ void binary_serializer::write_value(const primitive_variant& value) {
} }
void binary_serializer::write_raw(size_t num_bytes, const void* data) { void binary_serializer::write_raw(size_t num_bytes, const void* data) {
auto first = reinterpret_cast<const char*>(data); out_(reinterpret_cast<const char*>(data), num_bytes);
auto last = first + num_bytes;
out_(first, last);
} }
} // namespace caf } // namespace caf
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