rippled
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#pragma push_macro("XMACRO")
38#undef XMACRO
39
40#define XMACRO(STYPE) \
41 /* special types */ \
42 STYPE(STI_UNKNOWN, -2) \
43 STYPE(STI_NOTPRESENT, 0) \
44 STYPE(STI_UINT16, 1) \
45 \
46 /* types (common) */ \
47 STYPE(STI_UINT32, 2) \
48 STYPE(STI_UINT64, 3) \
49 STYPE(STI_UINT128, 4) \
50 STYPE(STI_UINT256, 5) \
51 STYPE(STI_AMOUNT, 6) \
52 STYPE(STI_VL, 7) \
53 STYPE(STI_ACCOUNT, 8) \
54 STYPE(STI_NUMBER, 9) \
55 STYPE(STI_INT32, 10) \
56 STYPE(STI_INT64, 11) \
57 \
58 /* 12-13 are reserved */ \
59 STYPE(STI_OBJECT, 14) \
60 STYPE(STI_ARRAY, 15) \
61 \
62 /* types (uncommon) */ \
63 STYPE(STI_UINT8, 16) \
64 STYPE(STI_UINT160, 17) \
65 STYPE(STI_PATHSET, 18) \
66 STYPE(STI_VECTOR256, 19) \
67 STYPE(STI_UINT96, 20) \
68 STYPE(STI_UINT192, 21) \
69 STYPE(STI_UINT384, 22) \
70 STYPE(STI_UINT512, 23) \
71 STYPE(STI_ISSUE, 24) \
72 STYPE(STI_XCHAIN_BRIDGE, 25) \
73 STYPE(STI_CURRENCY, 26) \
74 \
75 /* high-level types */ \
76 /* cannot be serialized inside other types */ \
77 STYPE(STI_TRANSACTION, 10001) \
78 STYPE(STI_LEDGERENTRY, 10002) \
79 STYPE(STI_VALIDATION, 10003) \
80 STYPE(STI_METADATA, 10004)
81
82#pragma push_macro("TO_ENUM")
83#undef TO_ENUM
84#pragma push_macro("TO_MAP")
85#undef TO_MAP
86
87#define TO_ENUM(name, value) name = value,
88#define TO_MAP(name, value) {#name, value},
89
90enum SerializedTypeID { XMACRO(TO_ENUM) };
91
93
94#undef XMACRO
95#undef TO_ENUM
96
97#pragma pop_macro("XMACRO")
98#pragma pop_macro("TO_ENUM")
99#pragma pop_macro("TO_MAP")
100
101// constexpr
102inline int
104{
105 return (safe_cast<int>(id) << 16) | index;
106}
107
108// constexpr
109inline int
110field_code(int id, int index)
111{
112 return (id << 16) | index;
113}
114
126{
127public:
128 enum {
129 sMD_Never = 0x00,
130 sMD_ChangeOrig = 0x01, // original value when it changes
131 sMD_ChangeNew = 0x02, // new value when it changes
132 sMD_DeleteFinal = 0x04, // final value when it is deleted
133 sMD_Create = 0x08, // value when it's created
134 sMD_Always = 0x10, // value when node containing it is affected at all
135 sMD_BaseTen = 0x20, // value is treated as base 10, overriding behavior
136 sMD_PseudoAccount = 0x40, // if this field is set in an ACCOUNT_ROOT
137 // _only_, then it is a pseudo-account
138 sMD_NeedsAsset = 0x80, // This field needs to be associated with an
139 // asset before it is serialized as a ledger
140 // object. Intended for STNumber.
142 };
143
144 enum class IsSigning : unsigned char { no, yes };
146
147 int const fieldCode; // (type<<16)|index
149 int const fieldValue; // Code number for protocol
151 int const fieldMeta;
152 int const fieldNum;
155
156 SField(SField const&) = delete;
157 SField&
158 operator=(SField const&) = delete;
159 SField(SField&&) = delete;
160 SField&
161 operator=(SField&&) = delete;
162
163public:
164 struct private_access_tag_t; // public, but still an implementation detail
165
166 // These constructors can only be called from SField.cpp
167 SField(
170 int fv,
171 char const* fn,
172 int meta = sMD_Default,
173 IsSigning signing = IsSigning::yes);
174 explicit SField(private_access_tag_t, int fc, char const* fn);
175
176 static SField const&
177 getField(int fieldCode);
178 static SField const&
180 static SField const&
181 getField(int type, int value)
182 {
183 return getField(field_code(type, value));
184 }
185
186 static SField const&
187 getField(SerializedTypeID type, int value)
188 {
189 return getField(field_code(type, value));
190 }
191
192 std::string const&
193 getName() const
194 {
195 return fieldName;
196 }
197
198 bool
199 hasName() const
200 {
201 return fieldCode > 0;
202 }
203
204 Json::StaticString const&
206 {
207 return jsonName;
208 }
209
210 operator Json::StaticString const&() const
211 {
212 return jsonName;
213 }
214
215 bool
216 isInvalid() const
217 {
218 return fieldCode == -1;
219 }
220
221 bool
222 isUseful() const
223 {
224 return fieldCode > 0;
225 }
226
227 bool
228 isBinary() const
229 {
230 return fieldValue < 256;
231 }
232
233 // A discardable field is one that cannot be serialized, and
234 // should be discarded during serialization,like 'hash'.
235 // You cannot serialize an object's hash inside that object,
236 // but you can have it in the JSON representation.
237 bool
239 {
240 return fieldValue > 256;
241 }
242
243 int
244 getCode() const
245 {
246 return fieldCode;
247 }
248 int
249 getNum() const
250 {
251 return fieldNum;
252 }
253 static int
255 {
256 return num;
257 }
258
259 bool
260 shouldMeta(int c) const
261 {
262 return (fieldMeta & c) != 0;
263 }
264
265 bool
266 shouldInclude(bool withSigningField) const
267 {
268 return (fieldValue < 256) && (withSigningField || (signingField == IsSigning::yes));
269 }
270
271 bool
272 operator==(SField const& f) const
273 {
274 return fieldCode == f.fieldCode;
275 }
276
277 bool
278 operator!=(SField const& f) const
279 {
280 return fieldCode != f.fieldCode;
281 }
282
283 static int
284 compare(SField const& f1, SField const& f2);
285
288 {
289 return knownCodeToField;
290 }
291
292private:
293 static int num;
296};
297
299template <class T>
301{
302 using type = T;
303
304 template <class... Args>
305 explicit TypedField(private_access_tag_t pat, Args&&... args);
306};
307
309template <class T>
311{
313
314 explicit OptionaledField(TypedField<T> const& f_) : f(&f_)
315 {
316 }
317};
318
319template <class T>
320inline OptionaledField<T>
322{
323 return OptionaledField<T>(f);
324}
325
326//------------------------------------------------------------------------------
327
328//------------------------------------------------------------------------------
329
341
344
353
354//------------------------------------------------------------------------------
355
356// Use macros for most SField construction to enforce naming conventions.
357#pragma push_macro("UNTYPED_SFIELD")
358#undef UNTYPED_SFIELD
359#pragma push_macro("TYPED_SFIELD")
360#undef TYPED_SFIELD
361
362#define UNTYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) extern SField const sfName;
363#define TYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) extern SF_##stiSuffix const sfName;
364
365extern SField const sfInvalid;
366extern SField const sfGeneric;
367
368#include <xrpl/protocol/detail/sfields.macro>
369
370#undef TYPED_SFIELD
371#pragma pop_macro("TYPED_SFIELD")
372#undef UNTYPED_SFIELD
373#pragma pop_macro("UNTYPED_SFIELD")
374
375} // namespace xrpl
Lightweight wrapper to tag static string.
Definition json_value.h:44
Identifies fields.
Definition SField.h:126
int const fieldValue
Definition SField.h:149
@ sMD_DeleteFinal
Definition SField.h:132
@ sMD_ChangeOrig
Definition SField.h:130
@ sMD_ChangeNew
Definition SField.h:131
@ sMD_PseudoAccount
Definition SField.h:136
@ sMD_NeedsAsset
Definition SField.h:138
bool isUseful() const
Definition SField.h:222
int const fieldCode
Definition SField.h:147
SField(SField &&)=delete
static int num
Definition SField.h:293
bool hasName() const
Definition SField.h:199
bool isInvalid() const
Definition SField.h:216
static SField const & getField(int fieldCode)
Definition SField.cpp:94
int getCode() const
Definition SField.h:244
SField & operator=(SField const &)=delete
int const fieldMeta
Definition SField.h:151
bool shouldInclude(bool withSigningField) const
Definition SField.h:266
std::string const fieldName
Definition SField.h:150
static SField const & getField(SerializedTypeID type, int value)
Definition SField.h:187
Json::StaticString const & getJsonName() const
Definition SField.h:205
static std::unordered_map< std::string, SField const * > knownNameToField
Definition SField.h:295
bool isDiscardable() const
Definition SField.h:238
int const fieldNum
Definition SField.h:152
static std::unordered_map< int, SField const * > const & getKnownCodeToField()
Definition SField.h:287
bool operator!=(SField const &f) const
Definition SField.h:278
bool isBinary() const
Definition SField.h:228
SerializedTypeID const fieldType
Definition SField.h:148
Json::StaticString const jsonName
Definition SField.h:154
static std::unordered_map< int, SField const * > knownCodeToField
Definition SField.h:294
int getNum() const
Definition SField.h:249
bool shouldMeta(int c) const
Definition SField.h:260
static IsSigning const notSigning
Definition SField.h:145
std::string const & getName() const
Definition SField.h:193
static int getNumFields()
Definition SField.h:254
SField(SField const &)=delete
SField & operator=(SField &&)=delete
bool operator==(SField const &f) const
Definition SField.h:272
static SField const & getField(int type, int value)
Definition SField.h:181
IsSigning const signingField
Definition SField.h:153
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:53
SField const sfGeneric
SerializedTypeID
Definition SField.h:90
@ XMACRO
Definition SField.h:90
int field_code(SerializedTypeID id, int index)
Definition SField.h:103
SField const sfInvalid
static std::map< std::string, int > const sTypeMap
Definition SField.h:92
Indicate std::optional field semantics.
Definition SField.h:311
TypedField< T > const * f
Definition SField.h:312
OptionaledField(TypedField< T > const &f_)
Definition SField.h:314
A field with a type known at compile time.
Definition SField.h:301