xrpld
Loading...
Searching...
No Matches
STTakesAsset.cpp
1#include <xrpl/protocol/STTakesAsset.h>
2
3#include <xrpl/beast/utility/instrumentation.h>
4#include <xrpl/protocol/Asset.h>
5#include <xrpl/protocol/SField.h>
6#include <xrpl/protocol/SOTemplate.h>
7#include <xrpl/protocol/STBase.h>
8#include <xrpl/protocol/STLedgerEntry.h>
9
10namespace xrpl {
11
12void
13associateAsset(SLE& sle, Asset const& asset)
14{
15 // Iterating by offset is the only way to get non-const references
16 for (int i = 0; i < sle.getCount(); ++i)
17 {
18 STBase& entry = sle.getIndex(i);
19 SField const& field = entry.getFName();
20 if (field.shouldMeta(SField::kSmdNeedsAsset))
21 {
22 auto const type = entry.getSType();
23 // If the field is not set or present, skip it.
24 if (type == STI_NOTPRESENT)
25 continue;
26
27 // If the type doesn't downcast, then the flag shouldn't be on the
28 // SField
29 auto& ta = entry.downcast<STTakesAsset>();
30 auto const style = sle.getStyle(ta.getFName());
31 XRPL_ASSERT_PARTS(
32 style != SoeInvalid, "xrpl::associateAsset", "valid template element style");
33
34 XRPL_ASSERT_PARTS(
35 style != SoeDefault || !ta.isDefault(),
36 "xrpl::associateAsset",
37 "non-default value");
38 ta.associateAsset(asset);
39
40 // associateAsset in derived classes may change the underlying
41 // value, but it won't know anything about how the value relates to
42 // the SLE. If the template element is soeDEFAULT, and the value
43 // changed to the default value, remove the field.
44 if (style == SoeDefault && ta.isDefault())
45 sle.makeFieldAbsent(field);
46 }
47 }
48}
49
50} // namespace xrpl
Identifies fields.
Definition SField.h:130
static constexpr auto kSmdNeedsAsset
Definition SField.h:141
A type which can be exported to a well known binary format.
Definition STBase.h:117
SOEStyle getStyle(SField const &field) const
Definition STObject.cpp:573
int getCount() const
Definition STObject.h:1010
STBase & getIndex(int offset)
Definition STObject.h:1022
void makeFieldAbsent(SField const &field)
Definition STObject.cpp:540
Intermediate class for any STBase-derived class to store an Asset.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ SoeDefault
Definition SOTemplate.h:20
@ SoeInvalid
Definition SOTemplate.h:17
STLedgerEntry SLE
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.