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#include <xrpl/protocol/Units.h>
6
7#include <cstdint>
8#include <map>
9
10namespace xrpl {
11
12/*
13
14Some fields have a different meaning for their
15 default value versus not present.
16 Example:
17 QualityIn on a TrustLine
18
19*/
20
21//------------------------------------------------------------------------------
22
23// Forwards
24class STAccount;
25class STAmount;
26class STIssue;
27class STBlob;
28template <int>
29class STBitString;
30template <class>
31class STInteger;
32class STNumber;
33class STXChainBridge;
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 STYPE(STI_XCHAIN_BRIDGE, 25) \
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
130{
131public:
132 static constexpr auto kSmdNever = 0x00;
133 static constexpr auto kSmdChangeOrig = 0x01; // original value when it changes
134 static constexpr auto kSmdChangeNew = 0x02; // new value when it changes
135 static constexpr auto kSmdDeleteFinal = 0x04; // final value when it is deleted
136 static constexpr auto kSmdCreate = 0x08; // value when it's created
137 static constexpr auto kSmdAlways = 0x10; // value when node containing it is affected at all
138 static constexpr auto kSmdBaseTen = 0x20; // value is treated as base 10, overriding behavior
139 static constexpr auto kSmdPseudoAccount = 0x40; // if this field is set in an ACCOUNT_ROOT
140 // _only_, then it is a pseudo-account
141 static constexpr auto kSmdNeedsAsset = 0x80; // This field needs to be associated with an
142 // asset before it is serialized as a ledger
143 // object. Intended for STNumber.
144 static constexpr auto kSmdDefault =
146
147 enum class IsSigning : unsigned char { No, Yes };
149
150 int const fieldCodeMem; // (type<<16)|index // TODO: rename, clashes with function
152 int const fieldValue; // Code number for protocol
154 int const fieldMeta;
155 int const fieldNum;
158
159 SField(SField const&) = delete;
160 SField&
161 operator=(SField const&) = delete;
162 SField(SField&&) = delete;
163 SField&
164 operator=(SField&&) = delete;
165
166public:
167 struct PrivateAccessTagT; // public, but still an implementation detail
168
169 // These constructors can only be called from SField.cpp
170 SField(
173 int fv,
174 char const* fn,
175 int meta = kSmdDefault,
176 IsSigning signing = IsSigning::Yes);
177 explicit SField(PrivateAccessTagT, int fc, char const* fn);
178
179 static SField const&
180 getField(int fieldCode);
181 static SField const&
183 static SField const&
184 getField(int type, int value)
185 {
186 return getField(fieldCode(type, value));
187 }
188
189 static SField const&
190 getField(SerializedTypeID type, int value)
191 {
192 return getField(fieldCode(type, value));
193 }
194
195 [[nodiscard]] std::string const&
196 getName() const
197 {
198 return fieldName;
199 }
200
201 [[nodiscard]] bool
202 hasName() const
203 {
204 return fieldCodeMem > 0;
205 }
206
207 [[nodiscard]] json::StaticString const&
209 {
210 return jsonName;
211 }
212
213 operator json::StaticString const&() const
214 {
215 return jsonName;
216 }
217
218 [[nodiscard]] bool
219 isInvalid() const
220 {
221 return fieldCodeMem == -1;
222 }
223
224 [[nodiscard]] bool
225 isUseful() const
226 {
227 return fieldCodeMem > 0;
228 }
229
230 [[nodiscard]] bool
231 isBinary() const
232 {
233 return fieldValue < 256;
234 }
235
236 // A discardable field is one that cannot be serialized, and
237 // should be discarded during serialization,like 'hash'.
238 // You cannot serialize an object's hash inside that object,
239 // but you can have it in the JSON representation.
240 [[nodiscard]] bool
242 {
243 return fieldValue > 256;
244 }
245
246 [[nodiscard]] int
247 getCode() const
248 {
249 return fieldCodeMem;
250 }
251 [[nodiscard]] int
252 getNum() const
253 {
254 return fieldNum;
255 }
256 static int
258 {
259 return num;
260 }
261
262 [[nodiscard]] bool
263 shouldMeta(int c) const
264 {
265 return (fieldMeta & c) != 0;
266 }
267
268 [[nodiscard]] bool
269 shouldInclude(bool withSigningField) const
270 {
271 return (fieldValue < 256) && (withSigningField || (signingField == IsSigning::Yes));
272 }
273
274 bool
275 operator==(SField const& f) const
276 {
277 return fieldCodeMem == f.fieldCodeMem;
278 }
279
280 bool
281 operator!=(SField const& f) const
282 {
283 return fieldCodeMem != f.fieldCodeMem;
284 }
285
286 static int
287 compare(SField const& f1, SField const& f2);
288
291 {
292 return knownCodeToField;
293 }
294
295private:
296 static int num;
299};
300
302template <class T>
304{
305 using type = T;
306
307 template <class... Args>
308 explicit TypedField(PrivateAccessTagT pat, Args&&... args);
309};
310
312template <class T>
314{
316
317 explicit OptionaledField(TypedField<T> const& f) : f(&f)
318 {
319 }
320};
321
322template <class T>
323inline OptionaledField<T>
325{
326 return OptionaledField<T>(f);
327}
328
329//------------------------------------------------------------------------------
330
331//------------------------------------------------------------------------------
332
344
347
356
357//------------------------------------------------------------------------------
358
359// Use macros for most SField construction to enforce naming conventions.
360#pragma push_macro("UNTYPED_SFIELD")
361#undef UNTYPED_SFIELD
362#pragma push_macro("TYPED_SFIELD")
363#undef TYPED_SFIELD
364
365#define UNTYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) extern SField const sfName;
366#define TYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) extern SF_##stiSuffix const sfName;
367
368extern SField const sfInvalid; // NOLINT(readability-identifier-naming)
369extern SField const sfGeneric; // NOLINT(readability-identifier-naming)
370
371#include <xrpl/protocol/detail/sfields.macro>
372
373#undef TYPED_SFIELD
374#pragma pop_macro("TYPED_SFIELD")
375#undef UNTYPED_SFIELD
376#pragma pop_macro("UNTYPED_SFIELD")
377
378} // namespace xrpl
Lightweight wrapper to tag static string.
Definition json_value.h:44
Identifies fields.
Definition SField.h:130
int const fieldValue
Definition SField.h:152
static constexpr auto kSmdChangeOrig
Definition SField.h:133
bool isUseful() const
Definition SField.h:225
static constexpr auto kSmdNeedsAsset
Definition SField.h:141
SField(SField &&)=delete
static int num
Definition SField.h:296
bool hasName() const
Definition SField.h:202
bool isInvalid() const
Definition SField.h:219
static SField const & getField(int fieldCode)
Definition SField.cpp:116
static IsSigning const kNotSigning
Definition SField.h:148
int const fieldCodeMem
Definition SField.h:150
int getCode() const
Definition SField.h:247
static constexpr auto kSmdDeleteFinal
Definition SField.h:135
static constexpr auto kSmdCreate
Definition SField.h:136
SField & operator=(SField const &)=delete
int const fieldMeta
Definition SField.h:154
bool shouldInclude(bool withSigningField) const
Definition SField.h:269
std::string const fieldName
Definition SField.h:153
static constexpr auto kSmdAlways
Definition SField.h:137
static SField const & getField(SerializedTypeID type, int value)
Definition SField.h:190
static std::unordered_map< std::string, SField const * > knownNameToField
Definition SField.h:298
bool isDiscardable() const
Definition SField.h:241
int const fieldNum
Definition SField.h:155
static constexpr auto kSmdNever
Definition SField.h:132
json::StaticString const jsonName
Definition SField.h:157
static std::unordered_map< int, SField const * > const & getKnownCodeToField()
Definition SField.h:290
static constexpr auto kSmdDefault
Definition SField.h:144
bool operator!=(SField const &f) const
Definition SField.h:281
bool isBinary() const
Definition SField.h:231
SerializedTypeID const fieldType
Definition SField.h:151
static constexpr auto kSmdChangeNew
Definition SField.h:134
static std::unordered_map< int, SField const * > knownCodeToField
Definition SField.h:297
int getNum() const
Definition SField.h:252
bool shouldMeta(int c) const
Definition SField.h:263
json::StaticString const & getJsonName() const
Definition SField.h:208
static int compare(SField const &f1, SField const &f2)
Definition SField.cpp:128
static constexpr auto kSmdPseudoAccount
Definition SField.h:139
std::string const & getName() const
Definition SField.h:196
static int getNumFields()
Definition SField.h:257
SField(SField const &)=delete
SField & operator=(SField &&)=delete
bool operator==(SField const &f) const
Definition SField.h:275
static constexpr auto kSmdBaseTen
Definition SField.h:138
static SField const & getField(int type, int value)
Definition SField.h:184
IsSigning const signingField
Definition SField.h:156
A serializable number.
Definition STNumber.h:35
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:57
TypedField< STInteger< std::uint64_t > > SF_UINT64
Definition SField.h:336
TypedField< STIssue > SF_ISSUE
Definition SField.h:350
static std::map< std::string, int > const kSTypeMap
Definition SField.h:95
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safeCast(Src s) noexcept
Definition safe_cast.h:21
SField const sfGeneric
TypedField< STInteger< std::uint8_t > > SF_UINT8
Definition SField.h:333
TypedField< STInteger< std::uint32_t > > SF_UINT32
Definition SField.h:335
int fieldCode(SerializedTypeID id, int index)
Definition SField.h:107
TypedField< STAmount > SF_AMOUNT
Definition SField.h:349
TypedField< STBitString< 128 > > SF_UINT128
Definition SField.h:338
TypedField< STInteger< std::int32_t > > SF_INT32
Definition SField.h:345
TypedField< STBitString< 384 > > SF_UINT384
Definition SField.h:342
SerializedTypeID
Definition SField.h:93
TypedField< STBitString< 96 > > SF_UINT96
Definition SField.h:337
TypedField< STBlob > SF_VL
Definition SField.h:353
TypedField< STBitString< 512 > > SF_UINT512
Definition SField.h:343
TypedField< STInteger< std::int64_t > > SF_INT64
Definition SField.h:346
TypedField< STNumber > SF_NUMBER
Definition SField.h:352
TypedField< STXChainBridge > SF_XCHAIN_BRIDGE
Definition SField.h:355
SField const sfInvalid
TypedField< STInteger< std::uint16_t > > SF_UINT16
Definition SField.h:334
TypedField< STAccount > SF_ACCOUNT
Definition SField.h:348
TypedField< STCurrency > SF_CURRENCY
Definition SField.h:351
TypedField< STVector256 > SF_VECTOR256
Definition SField.h:354
TypedField< STBitString< 256 > > SF_UINT256
Definition SField.h:341
TypedField< STBitString< 160 > > SF_UINT160
Definition SField.h:339
TypedField< STBitString< 192 > > SF_UINT192
Definition SField.h:340
Indicate std::optional field semantics.
Definition SField.h:314
TypedField< T > const * f
Definition SField.h:315
OptionaledField(TypedField< T > const &f)
Definition SField.h:317
A field with a type known at compile time.
Definition SField.h:304
TypedField(PrivateAccessTagT pat, Args &&... args)
Definition SField.cpp:26