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