17
17
*/
18
18
// ==============================================================================
19
19
20
+ #include < xrpl/beast/utility/instrumentation.h>
20
21
#include < xrpl/protocol/SField.h>
21
22
22
23
#include < map>
@@ -28,6 +29,7 @@ namespace ripple {
28
29
SField::IsSigning const SField::notSigning;
29
30
int SField::num = 0 ;
30
31
std::map<int , SField const *> SField::knownCodeToField;
32
+ std::map<std::string, SField const *> SField::knownNameToField;
31
33
32
34
// Give only this translation unit permission to construct SFields
33
35
struct SField ::private_access_tag_t
@@ -45,7 +47,7 @@ TypedField<T>::TypedField(private_access_tag_t pat, Args&&... args)
45
47
}
46
48
47
49
// Construct all compile-time SFields, and register them in the knownCodeToField
48
- // database :
50
+ // and knownNameToField databases :
49
51
50
52
// Use macros for most SField construction to enforce naming conventions.
51
53
#pragma push_macro("UNTYPED_SFIELD")
@@ -99,7 +101,14 @@ SField::SField(
99
101
, signingField(signing)
100
102
, jsonName(fieldName.c_str())
101
103
{
104
+ XRPL_ASSERT (
105
+ !knownCodeToField.contains (fieldCode),
106
+ " ripple::SField::SField(tid,fv,fn,meta,signing) : fieldCode is unique" );
107
+ XRPL_ASSERT (
108
+ !knownNameToField.contains (fieldName),
109
+ " ripple::SField::SField(tid,fv,fn,meta,signing) : fieldName is unique" );
102
110
knownCodeToField[fieldCode] = this ;
111
+ knownNameToField[fieldName] = this ;
103
112
}
104
113
105
114
SField::SField (private_access_tag_t , int fc)
@@ -111,6 +120,9 @@ SField::SField(private_access_tag_t, int fc)
111
120
, signingField(IsSigning::yes)
112
121
, jsonName(fieldName.c_str())
113
122
{
123
+ XRPL_ASSERT (
124
+ !knownCodeToField.contains (fieldCode),
125
+ " ripple::SField::SField(fc) : fieldCode is unique" );
114
126
knownCodeToField[fieldCode] = this ;
115
127
}
116
128
@@ -145,11 +157,11 @@ SField::compare(SField const& f1, SField const& f2)
145
157
SField const &
146
158
SField::getField (std::string const & fieldName)
147
159
{
148
- for (auto const & [_, f] : knownCodeToField)
160
+ auto it = knownNameToField.find (fieldName);
161
+
162
+ if (it != knownNameToField.end ())
149
163
{
150
- (void )_;
151
- if (f->fieldName == fieldName)
152
- return *f;
164
+ return *(it->second );
153
165
}
154
166
return sfInvalid;
155
167
}
0 commit comments