rippled
PathSet.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2015 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_LEDGER_TESTS_PATHSET_H_INCLUDED
21 #define RIPPLE_LEDGER_TESTS_PATHSET_H_INCLUDED
22 
23 #include <ripple/basics/Log.h>
24 #include <ripple/protocol/TxFlags.h>
25 #include <test/jtx.h>
26 
27 namespace ripple {
28 namespace test {
29 
32 inline bool
34  jtx::Env& env,
35  jtx::Account const& account,
36  STAmount const& takerPays,
37  STAmount const& takerGets)
38 {
39  bool exists = false;
41  *env.current(), account, [&](std::shared_ptr<SLE const> const& sle) {
42  if (sle->getType() == ltOFFER &&
43  sle->getFieldAmount(sfTakerPays) == takerPays &&
44  sle->getFieldAmount(sfTakerGets) == takerGets)
45  exists = true;
46  });
47  return exists;
48 }
49 
50 class Path
51 {
52 public:
54 
55  Path() = default;
56  Path(Path const&) = default;
57  Path&
58  operator=(Path const&) = default;
59  Path(Path&&) = default;
60  Path&
61  operator=(Path&&) = default;
62 
63  template <class First, class... Rest>
64  explicit Path(First&& first, Rest&&... rest)
65  {
66  addHelper(std::forward<First>(first), std::forward<Rest>(rest)...);
67  }
68  Path&
69  push_back(Issue const& iss);
70  Path&
71  push_back(jtx::Account const& acc);
72  Path&
73  push_back(STPathElement const& pe);
75  json() const;
76 
77 private:
78  template <class First, class... Rest>
79  void
80  addHelper(First&& first, Rest&&... rest);
81 };
82 
83 inline Path&
85 {
86  path.emplace_back(pe);
87  return *this;
88 }
89 
90 inline Path&
92 {
93  path.emplace_back(
95  beast::zero,
96  iss.currency,
97  iss.account);
98  return *this;
99 }
100 
101 inline Path&
103 {
104  path.emplace_back(account.id(), beast::zero, beast::zero);
105  return *this;
106 }
107 
108 template <class First, class... Rest>
109 void
110 Path::addHelper(First&& first, Rest&&... rest)
111 {
112  push_back(std::forward<First>(first));
113  if constexpr (sizeof...(rest) > 0)
114  addHelper(std::forward<Rest>(rest)...);
115 }
116 
117 inline Json::Value
118 Path::json() const
119 {
120  return path.getJson(JsonOptions::none);
121 }
122 
123 class PathSet
124 {
125 public:
127 
128  PathSet() = default;
129  PathSet(PathSet const&) = default;
130  PathSet&
131  operator=(PathSet const&) = default;
132  PathSet(PathSet&&) = default;
133  PathSet&
134  operator=(PathSet&&) = default;
135 
136  template <class First, class... Rest>
137  explicit PathSet(First&& first, Rest&&... rest)
138  {
139  addHelper(std::forward<First>(first), std::forward<Rest>(rest)...);
140  }
142  json() const
143  {
144  Json::Value v;
145  v["Paths"] = paths.getJson(JsonOptions::none);
146  return v;
147  }
148 
149 private:
150  template <class First, class... Rest>
151  void
152  addHelper(First first, Rest... rest)
153  {
154  paths.emplace_back(std::move(first.path));
155  if constexpr (sizeof...(rest) > 0)
156  addHelper(std::move(rest)...);
157  }
158 };
159 
160 } // namespace test
161 } // namespace ripple
162 
163 #endif
ripple::test::PathSet::operator=
PathSet & operator=(PathSet const &)=default
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
std::shared_ptr
STL class.
ripple::test::Path::json
Json::Value json() const
Definition: PathSet.h:118
ripple::Issue::currency
Currency currency
Definition: Issue.h:37
ripple::test::Path::push_back
Path & push_back(Issue const &iss)
Definition: PathSet.h:91
ripple::test::Path
Definition: PathSet.h:50
ripple::test::Path::Path
Path()=default
ripple::STPathElement::typeCurrency
@ typeCurrency
Definition: STPathSet.h:40
ripple::STPathSet
Definition: STPathSet.h:309
ripple::STPathElement::typeIssuer
@ typeIssuer
Definition: STPathSet.h:41
ripple::test::PathSet::PathSet
PathSet()=default
ripple::forEachItem
void forEachItem(ReadView const &view, AccountID const &id, std::function< void(std::shared_ptr< SLE const > const &)> f)
Iterate all items in an account's owner directory.
Definition: View.cpp:254
ripple::test::Path::addHelper
void addHelper(First &&first, Rest &&... rest)
Definition: PathSet.h:110
ripple::test::PathSet::PathSet
PathSet(First &&first, Rest &&... rest)
Definition: PathSet.h:137
ripple::JsonOptions::none
@ none
ripple::test::PathSet
Definition: PathSet.h:123
ripple::test::jtx::paths
Set Paths, SendMax on a JTx.
Definition: paths.h:32
ripple::STAmount
Definition: STAmount.h:42
ripple::test::Path::Path
Path(First &&first, Rest &&... rest)
Definition: PathSet.h:64
ripple::test::isOffer
bool isOffer(jtx::Env &env, jtx::Account const &account, STAmount const &takerPays, STAmount const &takerGets)
An offer exists.
Definition: PathSet.h:33
ripple::test::jtx::path
Add a path.
Definition: paths.h:55
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::PathSet::addHelper
void addHelper(First first, Rest... rest)
Definition: PathSet.h:152
ripple::STPathElement
Definition: STPathSet.h:33
ripple::test::PathSet::paths
STPathSet paths
Definition: PathSet.h:126
ripple::test::PathSet::json
Json::Value json() const
Definition: PathSet.h:142
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
ripple::STPath
Definition: STPathSet.h:212
ripple::test::jtx::Env::current
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition: Env.h:299
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:115
ripple::Issue::account
AccountID account
Definition: Issue.h:38
ripple::test::Path::operator=
Path & operator=(Path const &)=default
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::test::Path::path
STPath path
Definition: PathSet.h:53