From 94c16d8539a6a02e9ee6930653996412c39e24ef Mon Sep 17 00:00:00 2001 From: Bronek Kozicki Date: Thu, 2 Nov 2023 20:26:09 +0000 Subject: [PATCH] Replace class enum JsonOptions with struct We may consider turning this into a general-purpose template and using it elsewhere --- src/ripple/app/misc/impl/Transaction.cpp | 2 +- src/ripple/protocol/STBase.h | 77 +++++++++++++++--------- src/ripple/protocol/STObject.h | 1 + 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src/ripple/app/misc/impl/Transaction.cpp b/src/ripple/app/misc/impl/Transaction.cpp index 277803c856f..c38a6b7438f 100644 --- a/src/ripple/app/misc/impl/Transaction.cpp +++ b/src/ripple/app/misc/impl/Transaction.cpp @@ -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) { diff --git a/src/ripple/protocol/STBase.h b/src/ripple/protocol/STBase.h index 09fa3813ed8..ceb7fe3e7bc 100644 --- a/src/ripple/protocol/STBase.h +++ b/src/ripple/protocol/STBase.h @@ -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(lh) | static_cast(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(lh) & ~static_cast(rh)); -} - -/// Return: true if lh contains rh -[[nodiscard]] constexpr bool -operator&(JsonOptions lh, JsonOptions rh) noexcept -{ - return (static_cast(lh) & static_cast(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(_all)}; + } +}; namespace detail { class STVar; diff --git a/src/ripple/protocol/STObject.h b/src/ripple/protocol/STObject.h index 19d4b264734..3e3862bf6c8 100644 --- a/src/ripple/protocol/STObject.h +++ b/src/ripple/protocol/STObject.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include