rippled
MultivarJson.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2023 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_JSON_MULTIVARJSON_H_INCLUDED
21 #define RIPPLE_JSON_MULTIVARJSON_H_INCLUDED
22 
23 #include <ripple/json/json_value.h>
24 
25 #include <array>
26 #include <cassert>
27 #include <concepts>
28 #include <cstdlib>
29 #include <type_traits>
30 #include <utility>
31 
32 namespace ripple {
33 template <std::size_t Size>
35 {
37  constexpr static std::size_t size = Size;
38 
39  explicit MultivarJson(Json::Value const& init = {})
40  {
41  if (init == Json::Value{})
42  return; // All elements are already default-initialized
43  for (auto& v : val)
44  v = init;
45  }
46 
47  Json::Value const&
48  select(auto&& selector) const
49  requires std::same_as<std::size_t, decltype(selector())>
50  {
51  auto const index = selector();
52  assert(index < size);
53  return val[index];
54  }
55 
56  void
57  set(const char* key,
58  auto const&
59  v) requires std::constructible_from<Json::Value, decltype(v)>
60  {
61  for (auto& a : this->val)
62  a[key] = v;
63  }
64 
65  // Intentionally not using class enum here, MultivarJson is scope enough
66  enum IsMemberResult : int { none = 0, some, all };
67 
68  [[nodiscard]] IsMemberResult
69  isMember(const char* key) const
70  {
71  int count = 0;
72  for (auto& a : this->val)
73  if (a.isMember(key))
74  count += 1;
75 
76  return (count == 0 ? none : (count < size ? some : all));
77  }
78 };
79 
80 // Wrapper for Json for all supported API versions.
82 
83 /*
84 
85 NOTE:
86 
87 If a future API version change adds another possible format, change the size of
88 `MultiApiJson`, and update `apiVersionSelector()` to return the appropriate
89 selection value for the new `apiVersion` and higher.
90 
91 The more different JSON formats we support, the more CPU cycles we need to
92 prepare JSON for different API versions e.g. when publishing streams to
93 `subscribe` clients. Hence it is desirable to keep MultiApiJson small and
94 instead fully deprecate and remove support for old API versions. For example, if
95 we removed support for API version 1 and added a different format for API
96 version 3, the `apiVersionSelector` would change to
97 `static_cast<std::size_t>(apiVersion > 2)`
98 
99 Such hypothetical change should correspond with change in RPCHelpers.h
100 `apiMinimumSupportedVersion = 2;`
101 
102 */
103 
104 // Helper to create appropriate selector for indexing MultiApiJson by apiVersion
105 constexpr auto
106 apiVersionSelector(unsigned int apiVersion) noexcept
107 {
108  return [apiVersion]() constexpr
109  {
110  return static_cast<std::size_t>(
111  apiVersion <= 1 //
112  ? 0 //
113  : (apiVersion <= 2 //
114  ? 1 //
115  : 2));
116  };
117 }
118 
119 // Helper to execute a callback for every version. Want both min and max version
120 // provided explicitly, so user will know to do update `size` when they change
121 template <
122  unsigned int minVer,
123  unsigned int maxVer,
124  std::size_t size,
125  typename Fn>
126  requires //
127  (maxVer >= minVer) && //
128  (size == maxVer + 1 - minVer) && //
129  (apiVersionSelector(minVer)() == 0) && //
130  (apiVersionSelector(maxVer)() + 1 == size) && //
131  requires(Json::Value& json, Fn fn)
132 {
133  fn(json, static_cast<unsigned int>(1));
134 }
135 void
136 visit(MultivarJson<size>& json, Fn fn)
137 {
138  [&]<std::size_t... offset>(std::index_sequence<offset...>)
139  {
140  static_assert(((apiVersionSelector(minVer + offset)() >= 0) && ...));
141  static_assert(((apiVersionSelector(minVer + offset)() < size) && ...));
142  (fn(json.val[apiVersionSelector(minVer + offset)()], minVer + offset),
143  ...);
144  }
146 }
147 
148 } // namespace ripple
149 
150 #endif
ripple::MultivarJson::isMember
Json::Value const &select(auto &&selector) const requires std IsMemberResult isMember(const char *key) const
Definition: MultivarJson.h:69
ripple::MultivarJson::size
constexpr static std::size_t size
Definition: MultivarJson.h:37
utility
std::index_sequence
ripple::MultivarJson::val
std::array< Json::Value, Size > val
Definition: MultivarJson.h:36
ripple::TxSearched::all
@ all
ripple::requires
requires(T::ConsequencesFactory==Transactor::Normal) TxConsequences consequences_helper(PreflightContext const &ctx)
Definition: applySteps.cpp:181
concepts
ripple::set
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
Definition: BasicConfig.h:313
array
ripple::apiVersionSelector
constexpr auto apiVersionSelector(unsigned int apiVersion) noexcept
Definition: MultivarJson.h:106
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
cstdlib
cassert
ripple::MultivarJson::MultivarJson
MultivarJson(Json::Value const &init={})
Definition: MultivarJson.h:39
std::visit
T visit(T... args)
std::size_t
ripple::MultivarJson
Definition: MultivarJson.h:34
type_traits
ripple::TxSearched::some
@ some
Json::Value
Represents a JSON value.
Definition: json_value.h:145