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 xrpl {
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>
25TypedField<T>::TypedField(private_access_tag_t pat, Args&&... args) : SField(pat, std::forward<Args>(args)...)
26{
27}
28
29// Construct all compile-time SFields, and register them in the knownCodeToField
30// and knownNameToField databases:
31
32// Use macros for most SField construction to enforce naming conventions.
33#pragma push_macro("UNTYPED_SFIELD")
34#undef UNTYPED_SFIELD
35#pragma push_macro("TYPED_SFIELD")
36#undef TYPED_SFIELD
37
38#define UNTYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
39 SField const sfName(access, STI_##stiSuffix, fieldValue, std::string_view(#sfName).substr(2).data(), ##__VA_ARGS__);
40#define TYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
41 SF_##stiSuffix const sfName( \
42 access, STI_##stiSuffix, fieldValue, std::string_view(#sfName).substr(2).data(), ##__VA_ARGS__);
43
44// SFields which, for historical reasons, do not follow naming conventions.
45SField const sfInvalid(access, -1, "");
46SField const sfGeneric(access, 0, "Generic");
47// The following two fields aren't used anywhere, but they break tests/have
48// downstream effects.
49SField const sfHash(access, STI_UINT256, 257, "hash");
50SField const sfIndex(access, STI_UINT256, 258, "index");
51
52#include <xrpl/protocol/detail/sfields.macro>
53
54#undef TYPED_SFIELD
55#pragma pop_macro("TYPED_SFIELD")
56#undef UNTYPED_SFIELD
57#pragma pop_macro("UNTYPED_SFIELD")
58
59SField::SField(private_access_tag_t, SerializedTypeID tid, int fv, char const* fn, int meta, IsSigning signing)
60 : fieldCode(field_code(tid, fv))
61 , fieldType(tid)
62 , fieldValue(fv)
63 , fieldName(fn)
64 , fieldMeta(meta)
65 , fieldNum(++num)
66 , signingField(signing)
67 , jsonName(fieldName.c_str())
68{
69 XRPL_ASSERT(
70 !knownCodeToField.contains(fieldCode), "xrpl::SField::SField(tid,fv,fn,meta,signing) : fieldCode is unique");
71 XRPL_ASSERT(
72 !knownNameToField.contains(fieldName), "xrpl::SField::SField(tid,fv,fn,meta,signing) : fieldName is unique");
75}
76
77SField::SField(private_access_tag_t, int fc, char const* fn)
78 : fieldCode(fc)
79 , fieldType(STI_UNKNOWN)
80 , fieldValue(0)
81 , fieldName(fn)
82 , fieldMeta(sMD_Never)
83 , fieldNum(++num)
84 , signingField(IsSigning::yes)
85 , jsonName(fieldName.c_str())
86{
87 XRPL_ASSERT(!knownCodeToField.contains(fieldCode), "xrpl::SField::SField(fc,fn) : fieldCode is unique");
88 XRPL_ASSERT(!knownNameToField.contains(fieldName), "xrpl::SField::SField(fc,fn) : fieldName is unique");
91}
92
93SField const&
95{
96 auto it = knownCodeToField.find(code);
97
98 if (it != knownCodeToField.end())
99 {
100 return *(it->second);
101 }
102 return sfInvalid;
103}
104
105int
106SField::compare(SField const& f1, SField const& f2)
107{
108 // -1 = f1 comes before f2, 0 = illegal combination, 1 = f1 comes after f2
109 if ((f1.fieldCode <= 0) || (f2.fieldCode <= 0))
110 return 0;
111
112 if (f1.fieldCode < f2.fieldCode)
113 return -1;
114
115 if (f2.fieldCode < f1.fieldCode)
116 return 1;
117
118 return 0;
119}
120
121SField const&
123{
124 auto it = knownNameToField.find(fieldName);
125
126 if (it != knownNameToField.end())
127 {
128 return *(it->second);
129 }
130 return sfInvalid;
131}
132
133} // namespace xrpl
Identifies fields.
Definition SField.h:127
int const fieldCode
Definition SField.h:148
static int num
Definition SField.h:294
static SField const & getField(int fieldCode)
Definition SField.cpp:94
std::string const fieldName
Definition SField.h:151
static std::unordered_map< std::string, SField const * > knownNameToField
Definition SField.h:296
static std::unordered_map< int, SField const * > knownCodeToField
Definition SField.h:295
static IsSigning const notSigning
Definition SField.h:146
static int compare(SField const &f1, SField const &f2)
Definition SField.cpp:106
SField(SField const &)=delete
STL namespace.
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")
SField const sfGeneric
static SField::private_access_tag_t access
Definition SField.cpp:21
SerializedTypeID
Definition SField.h:91
int field_code(SerializedTypeID id, int index)
Definition SField.h:104
SField const sfHash(access, STI_UINT256, 257, "hash")
SField const sfInvalid
@ yes
Definition Steps.h:26
TypedField(private_access_tag_t pat, Args &&... args)
Definition SField.cpp:25