Skip to content

Commit

Permalink
edit to_pb to const member function
Browse files Browse the repository at this point in the history
  • Loading branch information
171930433 committed Jul 5, 2024
1 parent 36f04f2 commit 386fb22
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
6 changes: 3 additions & 3 deletions iguana/dynamic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ constexpr inline uint8_t ENABLE_ALL = 0x0F;

template <typename T, uint8_t ENABLE_FLAG = ENABLE_PB>
struct base_impl : public base {
void to_pb(std::string& str) override {
void to_pb(std::string& str) const override {
if constexpr ((ENABLE_FLAG & ENABLE_PB) != 0) {
to_pb_adl((iguana_adl_t*)nullptr, *(static_cast<T*>(this)), str);
to_pb_adl((iguana_adl_t*)nullptr, *(static_cast<T const*>(this)), str);
}
else {
throw std::runtime_error("Protobuf Disabled");
Expand Down Expand Up @@ -160,7 +160,7 @@ struct base_impl : public base {

virtual ~base_impl() {}

size_t cache_size = 0;
mutable size_t cache_size = 0;
};

IGUANA_INLINE std::shared_ptr<base> create_instance(std::string_view name) {
Expand Down
9 changes: 5 additions & 4 deletions iguana/pb_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ constexpr inline WireType get_wire_type() {
}

template <typename T>
constexpr bool is_lenprefix_v = (get_wire_type<T>() ==
WireType::LengthDelimeted);
constexpr bool is_lenprefix_v =
(get_wire_type<T>() == WireType::LengthDelimeted);

[[nodiscard]] IGUANA_INLINE uint32_t encode_zigzag(int32_t v) {
return (static_cast<uint32_t>(v) << 1U) ^
Expand Down Expand Up @@ -419,6 +419,7 @@ IGUANA_INLINE size_t pb_oneof_size(Type&& t, Arr& size_arr) {
int len = 0;
std::visit(
[&len, &size_arr](auto&& value) IGUANA__INLINE_LAMBDA {
using raw_value_type = decltype(value);
using value_type =
std::remove_const_t<std::remove_reference_t<decltype(value)>>;
constexpr auto offset =
Expand All @@ -427,7 +428,7 @@ IGUANA_INLINE size_t pb_oneof_size(Type&& t, Arr& size_arr) {
((field_no + offset) << 3) |
static_cast<uint32_t>(get_wire_type<value_type>());
len = pb_key_value_size<variant_uint32_size_constexpr(key), false>(
std::forward<value_type>(value), size_arr);
std::forward<raw_value_type>(value), size_arr);
},
std::forward<Type>(t));
return len;
Expand All @@ -454,7 +455,7 @@ IGUANA_INLINE size_t pb_key_value_size(Type&& t, Arr& size_arr) {
std::decay_t<decltype(tuple)>>;
constexpr auto value = std::get<decltype(i)::value>(tuple);
using U = typename field_type::value_type;
auto& val = value.value(t);
auto const& val = value.value(t);
if constexpr (variant_v<U>) {
constexpr auto offset =
get_variant_index<U, typename field_type::sub_type,
Expand Down
7 changes: 4 additions & 3 deletions iguana/pb_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ IGUANA_INLINE void to_pb_oneof(Type&& t, It&& it, uint32_t*& sz_ptr) {
using T = std::decay_t<Type>;
std::visit(
[&it, &sz_ptr](auto&& value) IGUANA__INLINE_LAMBDA {
using raw_value_type = decltype(value);
using value_type =
std::remove_const_t<std::remove_reference_t<decltype(value)>>;
constexpr auto offset =
get_variant_index<T, value_type, std::variant_size_v<T> - 1>();
constexpr uint32_t key =
((field_no + offset) << 3) |
static_cast<uint32_t>(get_wire_type<value_type>());
to_pb_impl<key, false>(std::forward<value_type>(value), it, sz_ptr);
to_pb_impl<key, false>(std::forward<raw_value_type>(value), it, sz_ptr);
},
std::forward<Type>(t));
}
Expand Down Expand Up @@ -449,7 +450,7 @@ IGUANA_INLINE void build_sub_proto(Map& map, std::string_view str_type,
} // namespace detail

template <typename T, typename Stream>
IGUANA_INLINE void to_pb(T& t, Stream& out) {
IGUANA_INLINE void to_pb(T const& t, Stream& out) {
std::vector<uint32_t> size_arr;
auto byte_len = detail::pb_key_value_size<0>(t, size_arr);
detail::resize(out, byte_len);
Expand Down Expand Up @@ -491,7 +492,7 @@ IGUANA_INLINE void to_proto_file(Stream& stream, std::string_view ns = "") {
#endif

template <typename T, typename Stream>
IGUANA_INLINE void to_pb_adl(iguana_adl_t* p, T& t, Stream& out) {
IGUANA_INLINE void to_pb_adl(iguana_adl_t* p, T const& t, Stream& out) {
to_pb(t, out);
}

Expand Down
3 changes: 2 additions & 1 deletion iguana/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ struct field_info {
};

struct base {
virtual void to_pb(std::string &str) {}
virtual void to_pb(std::string &str) const {}
virtual void from_pb(std::string_view str) {}
virtual void to_xml(std::string &str) const {}
virtual void from_xml(std::string_view str) {}
Expand Down Expand Up @@ -803,6 +803,7 @@ struct field_t {
uint32_t field_no;

auto &value(owner_type &value) const { return value.*member_ptr; }
auto const &value(owner_type const &value) const { return value.*member_ptr; }
};

template <typename T>
Expand Down

0 comments on commit 386fb22

Please sign in to comment.