xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
protocol
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
8
namespace
xrpl
{
9
10
// Storage for static const members.
11
SField::IsSigning
const
SField::kNotSigning
;
12
int
SField::num
= 0;
13
std::unordered_map<int, SField const*>
SField::knownCodeToField
;
14
std::unordered_map<std::string, SField const*>
SField::knownNameToField
;
15
16
// Give only this translation unit permission to construct SFields
17
struct
SField::PrivateAccessTagT
18
{
19
explicit
PrivateAccessTagT
() =
default
;
20
};
21
22
static
SField::PrivateAccessTagT
access
;
23
24
template
<
class
T>
25
template
<
class
... Args>
26
TypedField<T>::TypedField
(
PrivateAccessTagT
pat, Args&&... 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.
48
SField
const
sfInvalid
(
access
, -1,
""
);
49
SField
const
sfGeneric
(
access
, 0,
"Generic"
);
50
// The following two fields aren't used anywhere, but they break tests/have
51
// downstream effects.
52
SField
const
kSfHash
(
access
, STI_UINT256, 257,
"hash"
);
53
SField
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
62
SField::SField
(
63
PrivateAccessTagT
,
64
SerializedTypeID
tid,
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(
79
!
knownCodeToField
.contains(
fieldCodeMem
),
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"
);
84
knownCodeToField
[
fieldCodeMem
] =
this
;
85
knownNameToField
[
fieldName
] =
this
;
86
}
87
88
SField::SField
(
PrivateAccessTagT
,
int
fc,
char
const
* fn)
89
:
fieldCodeMem
(fc)
90
,
fieldType
(STI_UNKNOWN)
91
,
fieldValue
(0)
92
,
fieldName
(fn)
93
,
fieldMeta
(
kSmdNever
)
94
,
fieldNum
(++
num
)
95
,
signingField
(
IsSigning
::
Yes
)
96
,
jsonName
(
fieldName
.c_str())
97
{
98
XRPL_ASSERT(
99
!
knownCodeToField
.contains(
fieldCodeMem
),
100
"xrpl::SField::SField(fc,fn) : fieldCode is unique"
);
101
XRPL_ASSERT(
102
!
knownNameToField
.contains(
fieldName
),
"xrpl::SField::SField(fc,fn) : fieldName is unique"
);
103
knownCodeToField
[
fieldCodeMem
] =
this
;
104
knownNameToField
[
fieldName
] =
this
;
105
}
106
107
SField
const
&
108
SField::getField
(
int
code)
109
{
110
auto
it =
knownCodeToField
.find(code);
111
112
if
(it !=
knownCodeToField
.end())
113
{
114
return
*(it->second);
115
}
116
return
sfInvalid
;
117
}
118
119
int
120
SField::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
135
SField
const
&
136
SField::getField
(
std::string
const
&
fieldName
)
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
std::string
xrpl::SField
Identifies fields.
Definition
SField.h:132
xrpl::SField::fieldValue
int const fieldValue
Definition
SField.h:154
xrpl::SField::num
static int num
Definition
SField.h:298
xrpl::SField::getField
static SField const & getField(int fieldCode)
Definition
SField.cpp:108
xrpl::SField::kNotSigning
static IsSigning const kNotSigning
Definition
SField.h:150
xrpl::SField::fieldCodeMem
int const fieldCodeMem
Definition
SField.h:152
xrpl::SField::fieldMeta
int const fieldMeta
Definition
SField.h:156
xrpl::SField::fieldName
std::string const fieldName
Definition
SField.h:155
xrpl::SField::knownNameToField
static std::unordered_map< std::string, SField const * > knownNameToField
Definition
SField.h:300
xrpl::SField::fieldNum
int const fieldNum
Definition
SField.h:157
xrpl::SField::kSmdNever
static constexpr auto kSmdNever
Definition
SField.h:134
xrpl::SField::jsonName
json::StaticString const jsonName
Definition
SField.h:159
xrpl::SField::fieldType
SerializedTypeID const fieldType
Definition
SField.h:153
xrpl::SField::knownCodeToField
static std::unordered_map< int, SField const * > knownCodeToField
Definition
SField.h:299
xrpl::SField::compare
static int compare(SField const &f1, SField const &f2)
Definition
SField.cpp:120
xrpl::SField::SField
SField(SField const &)=delete
xrpl::SField::IsSigning
IsSigning
Definition
SField.h:149
xrpl::SField::signingField
IsSigning const signingField
Definition
SField.h:158
std
STL namespace.
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::kSfHash
SField const kSfHash(access, STI_UINT256, 257, "hash")
xrpl::access
static SField::PrivateAccessTagT access
Definition
SField.cpp:22
xrpl::sfGeneric
SField const sfGeneric
xrpl::kSfIndex
SField const kSfIndex(access, STI_UINT256, 258, "index")
xrpl::fieldCode
int fieldCode(SerializedTypeID id, int index)
Definition
SField.h:108
xrpl::ConsensusState::Yes
@ Yes
We have consensus along with the network.
Definition
ConsensusTypes.h:270
xrpl::SerializedTypeID
SerializedTypeID
Definition
SField.h:94
xrpl::sfInvalid
SField const sfInvalid
string
xrpl::SField::PrivateAccessTagT
Definition
SField.cpp:18
xrpl::SField::PrivateAccessTagT::PrivateAccessTagT
PrivateAccessTagT()=default
xrpl::TypedField::TypedField
TypedField(PrivateAccessTagT pat, Args &&... args)
Definition
SField.cpp:26
unordered_map
Generated by
1.17.0