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