mirror of
https://github.com/vtnerd/monero-lws.git
synced 2024-12-23 03:49:23 +00:00
Remove enumeration function from ::wire reading and writing (#84)
This commit is contained in:
parent
9f98d2a8c9
commit
6fa2d6799b
11 changed files with 5 additions and 55 deletions
|
@ -59,11 +59,14 @@
|
||||||
} \
|
} \
|
||||||
void read_bytes(::wire::reader& source, type_& dest) \
|
void read_bytes(::wire::reader& source, type_& dest) \
|
||||||
{ \
|
{ \
|
||||||
dest = type_(source.enumeration(map)); \
|
const auto val = type_ ## _from_string(source.string()); \
|
||||||
|
if (!val) \
|
||||||
|
WIRE_DLOG_THROW(::wire::error::schema::enumeration, #type_); \
|
||||||
|
dest = *val; \
|
||||||
} \
|
} \
|
||||||
void write_bytes(::wire::writer& dest, const type_ source) \
|
void write_bytes(::wire::writer& dest, const type_ source) \
|
||||||
{ \
|
{ \
|
||||||
dest.enumeration(std::size_t(source), map); \
|
dest.string(get_string(source)); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define WIRE_DEFINE_OBJECT(type, map) \
|
#define WIRE_DEFINE_OBJECT(type, map) \
|
||||||
|
|
|
@ -316,22 +316,6 @@ namespace wire
|
||||||
WIRE_DLOG_THROW(error::schema::fixed_binary, "of size" << dest.size() * 2 << " but got " << value.size());
|
WIRE_DLOG_THROW(error::schema::fixed_binary, "of size" << dest.size() * 2 << " but got " << value.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t json_reader::enumeration(epee::span<char const* const> enums)
|
|
||||||
{
|
|
||||||
rapidjson_sax json_enum{error::schema::string};
|
|
||||||
read_next_value(json_enum);
|
|
||||||
|
|
||||||
const boost::string_ref value{json_enum.value.string.ptr, json_enum.value.string.length};
|
|
||||||
for (std::size_t i = 0; i < enums.size(); ++i)
|
|
||||||
{
|
|
||||||
if (value == enums[i])
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
WIRE_DLOG_THROW(error::schema::enumeration, value << " is not a valid enum");
|
|
||||||
return enums.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t json_reader::start_array()
|
std::size_t json_reader::start_array()
|
||||||
{
|
{
|
||||||
if (get_next_token() != '[')
|
if (get_next_token() != '[')
|
||||||
|
|
|
@ -88,9 +88,6 @@ namespace wire
|
||||||
//! \throw wire::exception if next token cannot be read as hex into `dest`.
|
//! \throw wire::exception if next token cannot be read as hex into `dest`.
|
||||||
void binary(epee::span<std::uint8_t> dest) override final;
|
void binary(epee::span<std::uint8_t> dest) override final;
|
||||||
|
|
||||||
//! \throw wire::exception if invalid next token invalid enum. \return Index in `enums`.
|
|
||||||
std::size_t enumeration(epee::span<char const* const> enums) override final;
|
|
||||||
|
|
||||||
|
|
||||||
//! \throw wire::exception if next token not `[`.
|
//! \throw wire::exception if next token not `[`.
|
||||||
std::size_t start_array() override final;
|
std::size_t start_array() override final;
|
||||||
|
|
|
@ -130,13 +130,6 @@ namespace wire
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_writer::enumeration(const std::size_t index, const epee::span<char const* const> enums)
|
|
||||||
{
|
|
||||||
if (enums.size() < index)
|
|
||||||
throw std::logic_error{"Invalid enum/string value"};
|
|
||||||
string({enums[index], std::strlen(enums[index])});
|
|
||||||
}
|
|
||||||
|
|
||||||
void json_writer::start_array(std::size_t)
|
void json_writer::start_array(std::size_t)
|
||||||
{
|
{
|
||||||
formatter_.StartArray();
|
formatter_.StartArray();
|
||||||
|
|
|
@ -101,8 +101,6 @@ namespace wire
|
||||||
void string(boost::string_ref) override final;
|
void string(boost::string_ref) override final;
|
||||||
void binary(epee::span<const std::uint8_t> source) override final;
|
void binary(epee::span<const std::uint8_t> source) override final;
|
||||||
|
|
||||||
void enumeration(std::size_t index, epee::span<char const* const> enums) override final;
|
|
||||||
|
|
||||||
void start_array(std::size_t) override final;
|
void start_array(std::size_t) override final;
|
||||||
void end_array() override final;
|
void end_array() override final;
|
||||||
|
|
||||||
|
|
|
@ -428,14 +428,6 @@ namespace wire
|
||||||
std::memcpy(dest.data(), bytes.data(), dest.size());
|
std::memcpy(dest.data(), bytes.data(), dest.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t msgpack_reader::enumeration(const epee::span<char const* const> enums)
|
|
||||||
{
|
|
||||||
const std::uintmax_t value = unsigned_integer();
|
|
||||||
if (enums.size() < value)
|
|
||||||
WIRE_DLOG_THROW(error::schema::enumeration, value << " is not a valid enum");
|
|
||||||
return std::size_t(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t msgpack_reader::start_array()
|
std::size_t msgpack_reader::start_array()
|
||||||
{
|
{
|
||||||
const std::size_t upcoming =
|
const std::size_t upcoming =
|
||||||
|
|
|
@ -118,9 +118,6 @@ namespace wire
|
||||||
//! \throw wire::exception if next token cannot be read as hex into `dest`.
|
//! \throw wire::exception if next token cannot be read as hex into `dest`.
|
||||||
void binary(epee::span<std::uint8_t> dest) override final;
|
void binary(epee::span<std::uint8_t> dest) override final;
|
||||||
|
|
||||||
//! \throw wire::exception if invalid next token invalid enum. \return Index in `enums`.
|
|
||||||
std::size_t enumeration(epee::span<char const* const> enums) override final;
|
|
||||||
|
|
||||||
|
|
||||||
//! \throw wire::exception if next token not `[`.
|
//! \throw wire::exception if next token not `[`.
|
||||||
std::size_t start_array() override final;
|
std::size_t start_array() override final;
|
||||||
|
|
|
@ -178,13 +178,6 @@ namespace wire
|
||||||
--expected_;
|
--expected_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void msgpack_writer::enumeration(const std::size_t index, const epee::span<char const* const> enums)
|
|
||||||
{
|
|
||||||
if (enums.size() < index)
|
|
||||||
throw std::logic_error{"Invalid enum/string value"};
|
|
||||||
unsigned_integer(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
void msgpack_writer::start_array(const std::size_t items)
|
void msgpack_writer::start_array(const std::size_t items)
|
||||||
{
|
{
|
||||||
write_count<msgpack::ftag_array, msgpack::array_types>(bytes_, items);
|
write_count<msgpack::ftag_array, msgpack::array_types>(bytes_, items);
|
||||||
|
|
|
@ -136,8 +136,6 @@ namespace wire
|
||||||
//! \throw wire::exception if `source.size()` exceeds 2^32-1
|
//! \throw wire::exception if `source.size()` exceeds 2^32-1
|
||||||
void binary(epee::span<const std::uint8_t> source) override final;
|
void binary(epee::span<const std::uint8_t> source) override final;
|
||||||
|
|
||||||
void enumeration(std::size_t index, epee::span<char const* const> enums) override final;
|
|
||||||
|
|
||||||
//! \throw wire::exception if `items` exceeds 2^32-1.
|
//! \throw wire::exception if `items` exceeds 2^32-1.
|
||||||
void start_array(std::size_t items) override final;
|
void start_array(std::size_t items) override final;
|
||||||
void end_array() override final { --expected_; }
|
void end_array() override final { --expected_; }
|
||||||
|
|
|
@ -104,9 +104,6 @@ namespace wire
|
||||||
//! \throw wire::exception if next value cannot be read as binary into `dest`.
|
//! \throw wire::exception if next value cannot be read as binary into `dest`.
|
||||||
virtual void binary(epee::span<std::uint8_t> dest) = 0;
|
virtual void binary(epee::span<std::uint8_t> dest) = 0;
|
||||||
|
|
||||||
//! \throw wire::exception if next value invalid enum. \return Index in `enums`.
|
|
||||||
virtual std::size_t enumeration(epee::span<char const* const> enums) = 0;
|
|
||||||
|
|
||||||
//! \throw wire::exception if next value not array
|
//! \throw wire::exception if next value not array
|
||||||
virtual std::size_t start_array() = 0;
|
virtual std::size_t start_array() = 0;
|
||||||
|
|
||||||
|
|
|
@ -67,8 +67,6 @@ namespace wire
|
||||||
virtual void string(boost::string_ref) = 0;
|
virtual void string(boost::string_ref) = 0;
|
||||||
virtual void binary(epee::span<const std::uint8_t> bytes) = 0;
|
virtual void binary(epee::span<const std::uint8_t> bytes) = 0;
|
||||||
|
|
||||||
virtual void enumeration(std::size_t index, epee::span<char const* const> enums) = 0;
|
|
||||||
|
|
||||||
virtual void start_array(std::size_t) = 0;
|
virtual void start_array(std::size_t) = 0;
|
||||||
virtual void end_array() = 0;
|
virtual void end_array() = 0;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue