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
33 bool
35  jtx::Account const& account,
36  STAmount const& takerPays,
37  STAmount const& takerGets)
38 {
39  bool exists = false;
40  forEachItem (*env.current(), account,
41  [&](std::shared_ptr<SLE const> const& sle)
42  {
43  if (sle->getType () == ltOFFER &&
44  sle->getFieldAmount (sfTakerPays) == takerPays &&
45  sle->getFieldAmount (sfTakerGets) == takerGets)
46  exists = true;
47  });
48  return exists;
49 }
50 
51 class Path
52 {
53 public:
55 
56  Path () = default;
57  Path (Path const&) = default;
58  Path& operator=(Path const&) = default;
59  Path (Path&&) = default;
60  Path& operator=(Path&&) = default;
61 
62  template <class First, class... Rest>
63  explicit Path (First&& first, Rest&&... rest)
64  {
65  addHelper (std::forward<First> (first), std::forward<Rest> (rest)...);
66  }
67  Path& push_back (Issue const& iss);
68  Path& push_back (jtx::Account const& acc);
69  Path& push_back (STPathElement const& pe);
70  Json::Value json () const;
71  private:
72  template <class First, class... Rest>
73  void addHelper (First&& first, Rest&&... rest);
74 };
75 
76 inline Path& Path::push_back (STPathElement const& pe)
77 {
78  path.emplace_back (pe);
79  return *this;
80 }
81 
82 inline Path& Path::push_back (Issue const& iss)
83 {
85  beast::zero, iss.currency, iss.account);
86  return *this;
87 }
88 
89 inline
91 {
92  path.emplace_back (account.id (), beast::zero, beast::zero);
93  return *this;
94 }
95 
96 template <class First, class... Rest>
97 void Path::addHelper (First&& first, Rest&&... rest)
98 {
99  push_back (std::forward<First> (first));
100  if constexpr (sizeof...(rest) > 0)
101  addHelper(std::forward<Rest>(rest)...);
102 }
103 
104 inline
106 {
107  return path.getJson (JsonOptions::none);
108 }
109 
110 class PathSet
111 {
112 public:
114 
115  PathSet () = default;
116  PathSet (PathSet const&) = default;
117  PathSet& operator=(PathSet const&) = default;
118  PathSet (PathSet&&) = default;
119  PathSet& operator=(PathSet&&) = default;
120 
121  template <class First, class... Rest>
122  explicit PathSet (First&& first, Rest&&... rest)
123  {
124  addHelper (std::forward<First> (first), std::forward<Rest> (rest)...);
125  }
126  Json::Value json () const
127  {
128  Json::Value v;
129  v["Paths"] = paths.getJson (JsonOptions::none);
130  return v;
131  }
132 private:
133  template <class First, class... Rest>
134  void addHelper (First first, Rest... rest)
135  {
136  paths.emplace_back (std::move (first.path));
137  if constexpr (sizeof...(rest) > 0)
138  addHelper(std::move(rest)...);
139  }
140 };
141 
142 } // test
143 } // ripple
144 
145 #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:105
ripple::Issue::currency
Currency currency
Definition: Issue.h:37
ripple::test::Path::push_back
Path & push_back(Issue const &iss)
Definition: PathSet.h:82
ripple::test::Path
Definition: PathSet.h:51
ripple::test::Path::Path
Path()=default
ripple::STPathElement::typeCurrency
@ typeCurrency
Definition: STPathSet.h:40
ripple::STPathSet
Definition: STPathSet.h:297
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:244
ripple::test::Path::addHelper
void addHelper(First &&first, Rest &&... rest)
Definition: PathSet.h:97
ripple::test::PathSet::PathSet
PathSet(First &&first, Rest &&... rest)
Definition: PathSet.h:122
ripple::JsonOptions::none
@ none
ripple::test::PathSet
Definition: PathSet.h:110
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:63
ripple::test::isOffer
bool isOffer(jtx::Env &env, jtx::Account const &account, STAmount const &takerPays, STAmount const &takerGets)
An offer exists.
Definition: PathSet.h:34
ripple::test::jtx::path
Add a path.
Definition: paths.h:58
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:134
ripple::STPathElement
Definition: STPathSet.h:33
ripple::test::PathSet::paths
STPathSet paths
Definition: PathSet.h:113
ripple::test::PathSet::json
Json::Value json() const
Definition: PathSet.h:126
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
ripple::STPath
Definition: STPathSet.h:205
ripple::test::jtx::Env::current
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition: Env.h:296
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:117
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:141
ripple::test::Path::path
STPath path
Definition: PathSet.h:54