xrpld
Loading...
Searching...
No Matches
SField.h
1#pragma once
2
3#include <xrpl/basics/safe_cast.h>
4#include <xrpl/json/json_value.h>
5
6#include <cstdint>
7#include <map>
8#include <string>
9#include <unordered_map>
10
11namespace xrpl {
12
13/*
14
15Some fields have a different meaning for their
16 default value versus not present.
17 Example:
18 QualityIn on a TrustLine
19
20*/
21
22//------------------------------------------------------------------------------
23
24// Forwards
25class STAccount;
26class STAmount;
27class STIssue;
28class STBlob;
29template <int>
30class STBitString;
31template <class>
32class STInteger;
33class STNumber;
34class STVector256;
35class STCurrency;
36
37// NOLINTBEGIN(readability-identifier-naming)
38#pragma push_macro("XMACRO")
39#undef XMACRO
40
41#define XMACRO(STYPE) \
42 /* special types */ \
43 STYPE(STI_UNKNOWN, -2) \
44 STYPE(STI_NOTPRESENT, 0) \
45 STYPE(STI_UINT16, 1) \
46 \
47 /* types (common) */ \
48 STYPE(STI_UINT32, 2) \
49 STYPE(STI_UINT64, 3) \
50 STYPE(STI_UINT128, 4) \
51 STYPE(STI_UINT256, 5) \
52 STYPE(STI_AMOUNT, 6) \
53 STYPE(STI_VL, 7) \
54 STYPE(STI_ACCOUNT, 8) \
55 STYPE(STI_NUMBER, 9) \
56 STYPE(STI_INT32, 10) \
57 STYPE(STI_INT64, 11) \
58 \
59 /* 12-13 are reserved */ \
60 STYPE(STI_OBJECT, 14) \
61 STYPE(STI_ARRAY, 15) \
62 \
63 /* types (uncommon) */ \
64 STYPE(STI_UINT8, 16) \
65 STYPE(STI_UINT160, 17) \
66 STYPE(STI_PATHSET, 18) \
67 STYPE(STI_VECTOR256, 19) \
68 STYPE(STI_UINT96, 20) \
69 STYPE(STI_UINT192, 21) \
70 STYPE(STI_UINT384, 22) \
71 STYPE(STI_UINT512, 23) \
72 STYPE(STI_ISSUE, 24) \
73 /* 25 is reserved */ \
74 STYPE(STI_CURRENCY, 26) \
75 \
76 /* high-level types */ \
77 /* cannot be serialized inside other types */ \
78 STYPE(STI_TRANSACTION, 10001) \
79 STYPE(STI_LEDGERENTRY, 10002) \
80 STYPE(STI_VALIDATION, 10003) \
81 STYPE(STI_METADATA, 10004)
82
83#pragma push_macro("TO_ENUM")
84#undef TO_ENUM
85#pragma push_macro("TO_MAP")
86#undef TO_MAP
87
88#define TO_ENUM(name, value) name = (value),
89#define TO_MAP(name, value) {#name, value},
90
91// Protocol infrastructure, 39+ files
92// NOLINTNEXTLINE(cppcoreguidelines-use-enum-class)
93enum SerializedTypeID { XMACRO(TO_ENUM) };
94
96
97#undef XMACRO
98#undef TO_ENUM
99
100#pragma pop_macro("XMACRO")
101#pragma pop_macro("TO_ENUM")
102#pragma pop_macro("TO_MAP")
103// NOLINTEND(readability-identifier-naming)
104
105// constexpr
106inline int
108{
109 return (safeCast<int>(id) << 16) | index;
110}
111
112// constexpr
113inline int
114fieldCode(int id, int index)
115{
116 return (id << 16) | index;
117}
118
131{
132public:
133 static constexpr auto kSmdNever = 0x00;
134 static constexpr auto kSmdChangeOrig = 0x01; // original value when it changes
135 static constexpr auto kSmdChangeNew = 0x02; // new value when it changes
136 static constexpr auto kSmdDeleteFinal = 0x04; // final value when it is deleted
137 static constexpr auto kSmdCreate = 0x08; // value when it's created
138 static constexpr auto kSmdAlways = 0x10; // value when node containing it is affected at all
139 static constexpr auto kSmdBaseTen = 0x20; // value is treated as base 10, overriding behavior
140 static constexpr auto kSmdPseudoAccount = 0x40; // if this field is set in an ACCOUNT_ROOT
141 // _only_, then it is a pseudo-account
142 static constexpr auto kSmdNeedsAsset = 0x80; // This field needs to be associated with an
143 // asset before it is serialized as a ledger
144 // object. Intended for STNumber.
145 static constexpr auto kSmdDefault =
147
148 enum class IsSigning : unsigned char { No, Yes };
150
151 int const fieldCodeMem; // (type<<16)|index // TODO: rename, clashes with function
153 int const fieldValue; // Code number for protocol
155 int const fieldMeta;
156 int const fieldNum;
159
160 SField(SField const&) = delete;
161 SField&
162 operator=(SField const&) = delete;
163 SField(SField&&) = delete;
164 SField&
165 operator=(SField&&) = delete;
166
167public:
168 struct PrivateAccessTagT; // public, but still an implementation detail
169
170 // These constructors can only be called from SField.cpp
171 SField(
174 int fv,
175 char const* fn,
176 int meta = kSmdDefault,
177 IsSigning signing = IsSigning::Yes);
178 explicit SField(PrivateAccessTagT, int fc, char const* fn);
179
180 static SField const&
181 getField(int fieldCode);
182 static SField const&
184 static SField const&
185 getField(int type, int value)
186 {
187 return getField(fieldCode(type, value));
188 }
189
190 static SField const&
191 getField(SerializedTypeID type, int value)
192 {
193 return getField(fieldCode(type, value));
194 }
195
196 [[nodiscard]] std::string const&
197 getName() const
198 {
199 return fieldName;
200 }
201
202 [[nodiscard]] bool
203 hasName() const
204 {
205 return fieldCodeMem > 0;
206 }
207
208 [[nodiscard]] json::StaticString const&
210 {
211 return jsonName;
212 }
213
214 operator json::StaticString const&() const
215 {
216 return jsonName;
217 }
218
219 [[nodiscard]] bool
220 isInvalid() const
221 {
222 return fieldCodeMem == -1;
223 }
224
225 [[nodiscard]] bool
226 isUseful() const
227 {
228 return fieldCodeMem > 0;
229 }
230
231 [[nodiscard]] bool
232 isBinary() const
233 {
234 return fieldValue < 256;
235 }
236
237 // A discardable field is one that cannot be serialized, and
238 // should be discarded during serialization,like 'hash'.
239 // You cannot serialize an object's hash inside that object,
240 // but you can have it in the JSON representation.
241 [[nodiscard]] bool
243 {
244 return fieldValue > 256;
245 }
246
247 [[nodiscard]] int
248 getCode() const
249 {
250 return fieldCodeMem;
251 }
252 [[nodiscard]] int
253 getNum() const
254 {
255 return fieldNum;
256 }
257 static int
259 {
260 return num;
261 }
262
263 [[nodiscard]] bool
264 shouldMeta(int c) const
265 {
266 return (fieldMeta & c) != 0;
267 }
268
269 [[nodiscard]] bool
270 shouldInclude(bool withSigningField) const
271 {
272 return (fieldValue < 256) && (withSigningField || (signingField == IsSigning::Yes));
273 }
274
275 bool
276 operator==(SField const& f) const
277 {
278 return fieldCodeMem == f.fieldCodeMem;
279 }
280
281 bool
282 operator!=(SField const& f) const
283 {
284 return fieldCodeMem != f.fieldCodeMem;
285 }
286
287 static int
288 compare(SField const& f1, SField const& f2);
289
292 {
293 return knownCodeToField;
294 }
295
296private:
297 static int num;
300};
301
305template <class T>
307{
308 using type = T;
309
310 template <class... Args>
311 explicit TypedField(PrivateAccessTagT pat, Args&&... args);
312};
313
317template <class T>
319{
321
322 explicit OptionaledField(TypedField<T> const& f) : f(&f)
323 {
324 }
325};
326
327template <class T>
328inline OptionaledField<T>
330{
331 return OptionaledField<T>(f);
332}
333
334//------------------------------------------------------------------------------
335
336//------------------------------------------------------------------------------
337
338// TYPED_SFIELD below pastes these names together as SF_##stiSuffix, so the
339// spelling is fixed by the macro and cannot be CamelCase.
340// NOLINTBEGIN(readability-identifier-naming)
352
355
363// NOLINTEND(readability-identifier-naming)
364
365//------------------------------------------------------------------------------
366
367// Use macros for most SField construction to enforce naming conventions.
368#pragma push_macro("UNTYPED_SFIELD")
369#undef UNTYPED_SFIELD
370#pragma push_macro("TYPED_SFIELD")
371#undef TYPED_SFIELD
372
373#define UNTYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) extern SField const sfName;
374#define TYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) extern SF_##stiSuffix const sfName;
375
376extern SField const sfInvalid; // NOLINT(readability-identifier-naming)
377extern SField const sfGeneric; // NOLINT(readability-identifier-naming)
378
379#include <xrpl/protocol/detail/sfields.macro>
380
381#undef TYPED_SFIELD
382#pragma pop_macro("TYPED_SFIELD")
383#undef UNTYPED_SFIELD
384#pragma pop_macro("UNTYPED_SFIELD")
385
386} // namespace xrpl
Lightweight wrapper to tag static string.
Definition json_value.h:52
Identifies fields.
Definition SField.h:131
int const fieldValue
Definition SField.h:153
static constexpr auto kSmdChangeOrig
Definition SField.h:134
bool isUseful() const
Definition SField.h:226
static constexpr auto kSmdNeedsAsset
Definition SField.h:142
SField(SField &&)=delete
static int num
Definition SField.h:297
bool hasName() const
Definition SField.h:203
bool isInvalid() const
Definition SField.h:220
static SField const & getField(int fieldCode)
Definition SField.cpp:108
static IsSigning const kNotSigning
Definition SField.h:149
int const fieldCodeMem
Definition SField.h:151
int getCode() const
Definition SField.h:248
static constexpr auto kSmdDeleteFinal
Definition SField.h:136
static constexpr auto kSmdCreate
Definition SField.h:137
SField & operator=(SField const &)=delete
int const fieldMeta
Definition SField.h:155
bool shouldInclude(bool withSigningField) const
Definition SField.h:270
std::string const fieldName
Definition SField.h:154
static constexpr auto kSmdAlways
Definition SField.h:138
static SField const & getField(SerializedTypeID type, int value)
Definition SField.h:191
static std::unordered_map< std::string, SField const * > knownNameToField
Definition SField.h:299
bool isDiscardable() const
Definition SField.h:242
int const fieldNum
Definition SField.h:156
static constexpr auto kSmdNever
Definition SField.h:133
json::StaticString const jsonName
Definition SField.h:158
static std::unordered_map< int, SField const * > const & getKnownCodeToField()
Definition SField.h:291
static constexpr auto kSmdDefault
Definition SField.h:145
bool operator!=(SField const &f) const
Definition SField.h:282
bool isBinary() const
Definition SField.h:232
SerializedTypeID const fieldType
Definition SField.h:152
static constexpr auto kSmdChangeNew
Definition SField.h:135
static std::unordered_map< int, SField const * > knownCodeToField
Definition SField.h:298
int getNum() const
Definition SField.h:253
bool shouldMeta(int c) const
Definition SField.h:264
json::StaticString const & getJsonName() const
Definition SField.h:209
static int compare(SField const &f1, SField const &f2)
Definition SField.cpp:120
static constexpr auto kSmdPseudoAccount
Definition SField.h:140
std::string const & getName() const
Definition SField.h:197
static int getNumFields()
Definition SField.h:258
SField(SField const &)=delete
SField & operator=(SField &&)=delete
bool operator==(SField const &f) const
Definition SField.h:276
static constexpr auto kSmdBaseTen
Definition SField.h:139
static SField const & getField(int type, int value)
Definition SField.h:185
IsSigning const signingField
Definition SField.h:157
A serializable number.
Definition STNumber.h:42
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TypedField< STInteger< std::uint64_t > > SF_UINT64
Definition SField.h:344
TypedField< STIssue > SF_ISSUE
Definition SField.h:358
static std::map< std::string, int > const kSTypeMap
Definition SField.h:95
SField const sfGeneric
TypedField< STInteger< std::uint8_t > > SF_UINT8
Definition SField.h:341
TypedField< STInteger< std::uint32_t > > SF_UINT32
Definition SField.h:343
constexpr Dest safeCast(Src s) noexcept
Definition safe_cast.h:21
int fieldCode(SerializedTypeID id, int index)
Definition SField.h:107
TypedField< STAmount > SF_AMOUNT
Definition SField.h:357
TypedField< STBitString< 128 > > SF_UINT128
Definition SField.h:346
TypedField< STInteger< std::int32_t > > SF_INT32
Definition SField.h:353
TypedField< STBitString< 384 > > SF_UINT384
Definition SField.h:350
SerializedTypeID
Definition SField.h:93
TypedField< STBitString< 96 > > SF_UINT96
Definition SField.h:345
TypedField< STBlob > SF_VL
Definition SField.h:361
TypedField< STBitString< 512 > > SF_UINT512
Definition SField.h:351
TypedField< STInteger< std::int64_t > > SF_INT64
Definition SField.h:354
TypedField< STNumber > SF_NUMBER
Definition SField.h:360
SField const sfInvalid
TypedField< STInteger< std::uint16_t > > SF_UINT16
Definition SField.h:342
TypedField< STAccount > SF_ACCOUNT
Definition SField.h:356
TypedField< STCurrency > SF_CURRENCY
Definition SField.h:359
constexpr HashRouterFlags operator~(HashRouterFlags flags)
Definition HashRouter.h:74
TypedField< STVector256 > SF_VECTOR256
Definition SField.h:362
TypedField< STBitString< 256 > > SF_UINT256
Definition SField.h:349
TypedField< STBitString< 160 > > SF_UINT160
Definition SField.h:347
TypedField< STBitString< 192 > > SF_UINT192
Definition SField.h:348
Indicate std::optional field semantics.
Definition SField.h:319
TypedField< T > const * f
Definition SField.h:320
OptionaledField(TypedField< T > const &f)
Definition SField.h:322
A field with a type known at compile time.
Definition SField.h:307
TypedField(PrivateAccessTagT pat, Args &&... args)
Definition SField.cpp:26