Skip to content

Commit

Permalink
Simplify conversions to Json::Value
Browse files Browse the repository at this point in the history
Also remove potential ODR violation from json_value.h
  • Loading branch information
Bronek committed Dec 18, 2024
1 parent b9f1200 commit 241429b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 44 deletions.
59 changes: 15 additions & 44 deletions include/xrpl/json/json_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class Value
Value(UInt value);
Value(double value);
Value(const char* value);
Value(ripple::Number const& value);
/** \brief Constructs a value from a static string.
* Like other value string constructor but do not duplicate the string for
Expand All @@ -240,9 +241,15 @@ class Value
operator=(Value&& other);

template <typename T>
requires(!std::convertible_to<T, Value>)
Value&
operator=(T const& rhs);
operator=(T const& rhs)
requires(
!std::is_convertible_v<T, Value> &&
std::is_convertible_v<decltype(to_json(std::declval<T>())), Value>)
{
*this = to_json(rhs);
return *this;
}

Value(Value&& other) noexcept;

Expand Down Expand Up @@ -444,6 +451,12 @@ class Value
int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
};

inline Value
to_json(ripple::Number const& number)
{
return to_string(number);
}

bool
operator==(const Value&, const Value&);

Expand Down Expand Up @@ -689,48 +702,6 @@ class ValueIterator : public ValueIteratorBase
}
};

// https://ericniebler.com/2014/10/21/customization-point-design-in-c11-and-beyond/
namespace detail {

inline Value
to_json(ripple::Number const& number)
{
return to_string(number);
}

struct to_json_fn
{
template <typename T>
Value
operator()(T&& t) const
{
return to_json(std::forward<T>(t));
}
};

template <typename T>
struct static_const
{
static constexpr T value = {};
};

} // namespace detail

namespace {

constexpr auto const& to_json = detail::static_const<detail::to_json_fn>::value;

}

template <typename T>
requires(!std::convertible_to<T, Value>)
Value&
Value::operator=(T const& rhs)
{
*this = to_json(rhs);
return *this;
}

} // namespace Json

#endif // CPPTL_JSON_H_INCLUDED
7 changes: 7 additions & 0 deletions src/libxrpl/json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ Value::Value(const char* value) : type_(stringValue), allocated_(true)
value_.string_ = valueAllocator()->duplicateStringValue(value);
}

Value::Value(ripple::Number const& value) : type_(stringValue), allocated_(true)
{
auto const tmp = to_string(value);
value_.string_ =
valueAllocator()->duplicateStringValue(tmp.c_str(), tmp.length());
}

Value::Value(std::string const& value) : type_(stringValue), allocated_(true)
{
value_.string_ = valueAllocator()->duplicateStringValue(
Expand Down
2 changes: 2 additions & 0 deletions src/test/jtx/basic_prop.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#ifndef RIPPLE_TEST_JTX_BASIC_PROP_H_INCLUDED
#define RIPPLE_TEST_JTX_BASIC_PROP_H_INCLUDED

#include <memory>

namespace ripple {
namespace test {
namespace jtx {
Expand Down

0 comments on commit 241429b

Please sign in to comment.