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 {
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.
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 {
fun(OutIter pos) : pos_(pos) {}
void operator()(const char* first, const char* last) {
pos_ = std::copy(first, last, pos_);
void operator()(const char* first, size_t num_bytes) {
pos_ = std::copy(first, first + num_bytes, pos_);
}
OutIter pos_;
};
......@@ -69,9 +70,7 @@ public:
void write_raw(size_t num_bytes, const void* data) override;
private:
write_fun out_;
};
template <class T,
......
......@@ -33,21 +33,16 @@ public:
template <class T>
static inline void write_int(write_fun& f, const T& value) {
auto first = reinterpret_cast<const char*>(&value);
auto last = first + sizeof(T);
f(first, last);
f(reinterpret_cast<const char*>(&value), sizeof(T));
}
static inline void write_string(write_fun& f, const std::string& str) {
write_int(f, static_cast<uint32_t>(str.size()));
auto first = str.data();
auto last = first + str.size();
f(first, last);
f(str.data(), str.size());
}
void operator()(const bool& value) const {
uint8_t tmp = value ? 1 : 0;
write_int(out_, tmp);
write_int(out_, static_cast<uint8_t>(value ? 1 : 0));
}
template <class T>
......@@ -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) {
auto first = reinterpret_cast<const char*>(data);
auto last = first + num_bytes;
out_(first, last);
out_(reinterpret_cast<const char*>(data), num_bytes);
}
} // 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