Skip to content

Commit

Permalink
Replace class enum JsonOptions with struct
Browse files Browse the repository at this point in the history
We may consider turning this into a general-purpose template and using it elsewhere
  • Loading branch information
Bronek committed Nov 2, 2023
1 parent b2a8a2d commit 94c16d8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/ripple/app/misc/impl/Transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Transaction::getJson(JsonOptions options, bool binary) const
{
// Note, we explicitly suppress `include_date` option here
Json::Value ret(
mTransaction->getJson(options % JsonOptions::include_date, binary));
mTransaction->getJson(options & ~JsonOptions::include_date, binary));

if (mLedgerIndex)
{
Expand Down
77 changes: 50 additions & 27 deletions src/ripple/protocol/STBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,57 @@
namespace ripple {

/// Note, should be treated as flags that can be | and &
enum class JsonOptions : unsigned int {
none = 0,
include_date = 1,
disable_API_prior_V2 = 2
};

/// Return: JsonOptions union of lh and rh
[[nodiscard]] constexpr JsonOptions
operator|(JsonOptions lh, JsonOptions rh) noexcept
{
return JsonOptions(
static_cast<unsigned int>(lh) | static_cast<unsigned int>(rh));
}

/// Return: JsonOptions similar to lh, but with rh disabled (i.e. "remainder")
[[nodiscard]] constexpr JsonOptions
operator%(JsonOptions lh, JsonOptions rh) noexcept
struct JsonOptions
{
return JsonOptions(
static_cast<unsigned int>(lh) & ~static_cast<unsigned int>(rh));
}

/// Return: true if lh contains rh
[[nodiscard]] constexpr bool
operator&(JsonOptions lh, JsonOptions rh) noexcept
{
return (static_cast<unsigned int>(lh) & static_cast<unsigned int>(rh)) != 0;
}
using underlying_t = unsigned int;
underlying_t value;

enum values : underlying_t {
none = 0u,
include_date = 1u,
disable_API_prior_V2 = 2u,
_all = 3u // NOTE see `operator~` and bump as needed when adding enums
};

constexpr JsonOptions(underlying_t v) noexcept : value(v)
{
}

[[nodiscard]] constexpr explicit operator underlying_t() const noexcept
{
return value;
}
[[nodiscard]] constexpr explicit operator bool() const noexcept
{
return value != 0u;
}
[[nodiscard]] constexpr auto friend
operator==(JsonOptions lh, JsonOptions rh) noexcept -> bool = default;
[[nodiscard]] constexpr auto friend
operator!=(JsonOptions lh, JsonOptions rh) noexcept -> bool = default;

/// Returns JsonOptions union of lh and rh
[[nodiscard]] constexpr JsonOptions friend
operator|(JsonOptions lh, JsonOptions rh) noexcept
{
return {lh.value | rh.value};
}

/// Returns JsonOptions intersection of lh and rh
[[nodiscard]] constexpr JsonOptions friend
operator&(JsonOptions lh, JsonOptions rh) noexcept
{
return {lh.value & rh.value};
}

/// Returns JsonOptions binary negation, can be used with & (above) for set
/// difference e.g. `(options & ~JsonOptions::include_date)`
[[nodiscard]] constexpr JsonOptions friend
operator~(JsonOptions v) noexcept
{
return {~v.value & static_cast<underlying_t>(_all)};
}
};

namespace detail {
class STVar;
Expand Down
1 change: 1 addition & 0 deletions src/ripple/protocol/STObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <ripple/protocol/HashPrefix.h>
#include <ripple/protocol/SOTemplate.h>
#include <ripple/protocol/STAmount.h>
#include <ripple/protocol/STBase.h>
#include <ripple/protocol/STIssue.h>
#include <ripple/protocol/STPathSet.h>
#include <ripple/protocol/STVector256.h>
Expand Down

0 comments on commit 94c16d8

Please sign in to comment.