Skip to content

Commit

Permalink
Remove enumeration function from ::wire reading and writing (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtnerd authored Dec 6, 2023
1 parent 29d3859 commit 4543f57
Show file tree
Hide file tree
Showing 11 changed files with 5 additions and 55 deletions.
7 changes: 5 additions & 2 deletions src/wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@
} \
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) \
{ \
dest.enumeration(std::size_t(source), map); \
dest.string(get_string(source)); \
}

#define WIRE_DEFINE_OBJECT(type, map) \
Expand Down
16 changes: 0 additions & 16 deletions src/wire/json/read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,6 @@ namespace wire
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()
{
if (get_next_token() != '[')
Expand Down
3 changes: 0 additions & 3 deletions src/wire/json/read.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ namespace wire
//! \throw wire::exception if next token cannot be read as hex into `dest`.
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 `[`.
std::size_t start_array() override final;
Expand Down
7 changes: 0 additions & 7 deletions src/wire/json/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
formatter_.StartArray();
Expand Down
2 changes: 0 additions & 2 deletions src/wire/json/write.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ namespace wire
void string(boost::string_ref) 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 end_array() override final;

Expand Down
8 changes: 0 additions & 8 deletions src/wire/msgpack/read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,6 @@ namespace wire
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()
{
const std::size_t upcoming =
Expand Down
3 changes: 0 additions & 3 deletions src/wire/msgpack/read.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ namespace wire
//! \throw wire::exception if next token cannot be read as hex into `dest`.
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 `[`.
std::size_t start_array() override final;
Expand Down
7 changes: 0 additions & 7 deletions src/wire/msgpack/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,6 @@ namespace wire
--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)
{
write_count<msgpack::ftag_array, msgpack::array_types>(bytes_, items);
Expand Down
2 changes: 0 additions & 2 deletions src/wire/msgpack/write.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ namespace wire
//! \throw wire::exception if `source.size()` exceeds 2^32-1
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.
void start_array(std::size_t items) override final;
void end_array() override final { --expected_; }
Expand Down
3 changes: 0 additions & 3 deletions src/wire/read.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ namespace wire
//! \throw wire::exception if next value cannot be read as binary into `dest`.
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
virtual std::size_t start_array() = 0;

Expand Down
2 changes: 0 additions & 2 deletions src/wire/write.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ namespace wire
virtual void string(boost::string_ref) = 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 end_array() = 0;

Expand Down

0 comments on commit 4543f57

Please sign in to comment.