idl: make bytes compatible with bytes_ostream

This patch makes idl type "bytes" compatible with both bytes and
bytes_ostream.

Signed-off-by: Paweł Dziepak <pdziepak@scylladb.com>
This commit is contained in:
Paweł Dziepak
2016-08-17 15:38:59 +01:00
parent 2c5ec44281
commit dcf794b04d
4 changed files with 52 additions and 32 deletions

View File

@@ -776,7 +776,7 @@ def add_view(hout, info):
for m in members:
full_type = param_view_type(m["type"])
fprintln(hout, Template(reindent(4, """
$type $name() const {
auto $name() const {
auto in = v;
$skip
return deserialize(in, boost::type<$type>());
@@ -884,10 +884,10 @@ $name$temp_param serializer<$name$temp_param>::read(Input& buf) {""").substitute
deflt = param["default"][0] if "default" in param else param_type(param["type"]) + "()"
if deflt in local_names:
deflt = local_names[deflt]
fprintln(cout, Template(""" $typ $local = (in.size()>0) ?
fprintln(cout, Template(""" auto $local = (in.size()>0) ?
$func(in, boost::type<$typ>()) : $default;""").substitute({'func' : DESERIALIZER, 'typ': param_type(param["type"]), 'local' : local_param, 'default': deflt}))
else:
fprintln(cout, Template(""" $typ $local = $func(in, boost::type<$typ>());""").substitute({'func' : DESERIALIZER, 'typ': param_type(param["type"]), 'local' : local_param}))
fprintln(cout, Template(""" auto $local = $func(in, boost::type<$typ>());""").substitute({'func' : DESERIALIZER, 'typ': param_type(param["type"]), 'local' : local_param}))
params.append("std::move(" + local_param + ")")
fprintln(cout, Template("""
$name$temp_param res {$params};

View File

@@ -26,7 +26,7 @@ class result_digest final {
};
class result {
bytes_ostream buf();
bytes buf();
std::experimental::optional<query::result_digest> digest();
api::timestamp_type last_modified() [ [version 1.2] ] = api::missing_timestamp;
};

View File

@@ -30,6 +30,7 @@
#include "bytes_ostream.hh"
#include "core/simple-stream.hh"
#include "boost/variant/variant.hpp"
#include "bytes_ostream.hh"
namespace ser {
using size_type = uint32_t;
@@ -87,7 +88,7 @@ inline void serialize(Output& out, const T& v) {
};
template<typename T, typename Input>
inline T deserialize(Input& in, boost::type<T> t) {
inline auto deserialize(Input& in, boost::type<T> t) {
return serializer<T>::read(in);
};
@@ -146,6 +147,11 @@ struct normalize<managed_bytes> {
using type = bytes;
};
template <>
struct normalize<bytes_ostream> {
using type = bytes;
};
template <typename T, typename U>
struct is_equivalent : std::is_same<typename normalize<std::remove_const_t<std::remove_reference_t<T>>>::type, typename normalize<std::remove_const_t <std::remove_reference_t<U>>>::type> {
};

View File

@@ -258,14 +258,40 @@ struct serializer<std::map<K, V>> {
}
};
class deserialized_bytes_proxy {
utils::input_stream _stream;
public:
explicit deserialized_bytes_proxy(utils::input_stream stream)
: _stream(std::move(stream)) { }
[[gnu::always_inline]]
operator bytes() && {
bytes v(bytes::initialized_later(), _stream.size());
_stream.read(reinterpret_cast<char*>(v.begin()), _stream.size());
return v;
}
[[gnu::always_inline]]
operator managed_bytes() && {
managed_bytes v(managed_bytes::initialized_later(), _stream.size());
_stream.read(reinterpret_cast<char*>(v.begin()), _stream.size());
return v;
}
[[gnu::always_inline]]
operator bytes_ostream() && {
bytes_ostream v;
_stream.copy_to(v);
return v;
}
};
template<>
struct serializer<bytes> {
template<typename Input>
static bytes read(Input& in) {
static deserialized_bytes_proxy read(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
bytes v(bytes::initialized_later(), sz);
in.read(reinterpret_cast<char*>(v.begin()), sz);
return v;
return deserialized_bytes_proxy(in.read_substream(sz));
}
template<typename Output>
static void write(Output& out, bytes_view v) {
@@ -280,6 +306,13 @@ struct serializer<bytes> {
static void write(Output& out, const managed_bytes& v) {
write(out, static_cast<bytes_view>(v));
}
template<typename Output>
static void write(Output& out, const bytes_ostream& v) {
safe_serialize_as_uint32(out, uint32_t(v.size()));
for (bytes_view frag : v.fragments()) {
out.write(reinterpret_cast<const char*>(frag.begin()), frag.size());
}
}
template<typename Input>
static void skip(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
@@ -295,29 +328,10 @@ template<typename Output>
void serialize(Output& out, const managed_bytes& v) {
serializer<bytes>::write(out, v);
}
template<>
struct serializer<bytes_ostream> {
template<typename Input>
static bytes_ostream read(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
bytes_ostream v;
auto dst = v.write_place_holder(sz);
in.read(reinterpret_cast<char*>(dst), sz);
return v;
}
template<typename Output>
static void write(Output& out, const bytes_ostream& v) {
safe_serialize_as_uint32(out, uint32_t(v.size()));
for (bytes_view frag : v.fragments()) {
out.write(reinterpret_cast<const char*>(frag.begin()), frag.size());
}
}
template<typename Input>
static void skip(Input& in) {
serializer<bytes>::skip(in);
}
};
template<typename Output>
void serialize(Output& out, const bytes_ostream& v) {
serializer<bytes>::write(out, v);
}
template<typename T>
struct serializer<std::experimental::optional<T>> {