xrpld
Loading...
Searching...
No Matches
SField.cpp
1#include <xrpl/protocol/SField.h>
2
3#include <xrpl/beast/utility/instrumentation.h>
4
5#include <string>
6#include <unordered_map>
7
8namespace xrpl {
9
10// Storage for static const members.
12int SField::num = 0;
13std::unordered_map<int, SField const*> SField::knownCodeToField;
14std::unordered_map<std::string, SField const*> SField::knownNameToField;
15
16// Give only this translation unit permission to construct SFields
18{
19 explicit PrivateAccessTagT() = default;
20};
21
23
24template <class T>
25template <class... Args>
27 : SField(pat, std::forward<Args>(args)...)
28{
29}
30
31// Construct all compile-time SFields, and register them in the knownCodeToField
32// and knownNameToField databases:
33
34// Use macros for most SField construction to enforce naming conventions.
35#pragma push_macro("UNTYPED_SFIELD")
36#undef UNTYPED_SFIELD
37#pragma push_macro("TYPED_SFIELD")
38#undef TYPED_SFIELD
39
40#define UNTYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
41 SField const sfName( \
42 access, STI_##stiSuffix, fieldValue, std::string_view(#sfName).substr(2).data(), ##__VA_ARGS__);
43#define TYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
44 SF_##stiSuffix const sfName( \
45 access, STI_##stiSuffix, fieldValue, std::string_view(#sfName).substr(2).data(), ##__VA_ARGS__);
46
47// SFields which, for historical reasons, do not follow naming conventions.
48SField const sfInvalid(access, -1, "");
49SField const sfGeneric(access, 0, "Generic");
50// The following two fields aren't used anywhere, but they break tests/have
51// downstream effects.
52SField const kSfHash(access, STI_UINT256, 257, "hash");
53SField const kSfIndex(access, STI_UINT256, 258, "index");
54
55#include <xrpl/protocol/detail/sfields.macro>
56
57#undef TYPED_SFIELD
58#pragma pop_macro("TYPED_SFIELD")
59#undef UNTYPED_SFIELD
60#pragma pop_macro("UNTYPED_SFIELD")
61
65 int fv,
66 char const* fn,
67 int meta,
68 IsSigning signing)
69 : fieldCodeMem(fieldCode(tid, fv))
70 , fieldType(tid)
71 , fieldValue(fv)
72 , fieldName(fn)
73 , fieldMeta(meta)
74 , fieldNum(++num)
75 , signingField(signing)
76 , jsonName(fieldName.c_str())
77{
78 XRPL_ASSERT(
80 "xrpl::SField::SField(tid,fv,fn,meta,signing) : fieldCode is unique");
81 XRPL_ASSERT(
82 !knownNameToField.contains(fieldName),
83 "xrpl::SField::SField(tid,fv,fn,meta,signing) : fieldName is unique");
86}
87
88SField::SField(PrivateAccessTagT, int fc, char const* fn)
89 : fieldCodeMem(fc)
90 , fieldType(STI_UNKNOWN)
91 , fieldValue(0)
92 , fieldName(fn)
94 , fieldNum(++num)
96 , jsonName(fieldName.c_str())
97{
98 XRPL_ASSERT(
100 "xrpl::SField::SField(fc,fn) : fieldCode is unique");
101 XRPL_ASSERT(
102 !knownNameToField.contains(fieldName), "xrpl::SField::SField(fc,fn) : fieldName is unique");
105}
106
107SField const&
109{
110 auto it = knownCodeToField.find(code);
111
112 if (it != knownCodeToField.end())
113 {
114 return *(it->second);
115 }
116 return sfInvalid;
117}
118
119int
120SField::compare(SField const& f1, SField const& f2)
121{
122 // -1 = f1 comes before f2, 0 = illegal combination, 1 = f1 comes after f2
123 if ((f1.fieldCodeMem <= 0) || (f2.fieldCodeMem <= 0))
124 return 0;
125
126 if (f1.fieldCodeMem < f2.fieldCodeMem)
127 return -1;
128
129 if (f2.fieldCodeMem < f1.fieldCodeMem)
130 return 1;
131
132 return 0;
133}
134
135SField const&
137{
138 auto it = knownNameToField.find(fieldName);
139
140 if (it != knownNameToField.end())
141 {
142 return *(it->second);
143 }
144 return sfInvalid;
145}
146
147} // namespace xrpl
Identifies fields.
Definition SField.h:132
int const fieldValue
Definition SField.h:154
static int num
Definition SField.h:298
static SField const & getField(int fieldCode)
Definition SField.cpp:108
static IsSigning const kNotSigning
Definition SField.h:150
int const fieldCodeMem
Definition SField.h:152
int const fieldMeta
Definition SField.h:156
std::string const fieldName
Definition SField.h:155
static std::unordered_map< std::string, SField const * > knownNameToField
Definition SField.h:300
int const fieldNum
Definition SField.h:157
static constexpr auto kSmdNever
Definition SField.h:134
json::StaticString const jsonName
Definition SField.h:159
SerializedTypeID const fieldType
Definition SField.h:153
static std::unordered_map< int, SField const * > knownCodeToField
Definition SField.h:299
static int compare(SField const &f1, SField const &f2)
Definition SField.cpp:120
SField(SField const &)=delete
IsSigning const signingField
Definition SField.h:158
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
SField const kSfHash(access, STI_UINT256, 257, "hash")
static SField::PrivateAccessTagT access
Definition SField.cpp:22
SField const sfGeneric
SField const kSfIndex(access, STI_UINT256, 258, "index")
int fieldCode(SerializedTypeID id, int index)
Definition SField.h:108
@ Yes
We have consensus along with the network.
SerializedTypeID
Definition SField.h:94
SField const sfInvalid
TypedField(PrivateAccessTagT pat, Args &&... args)
Definition SField.cpp:26