rippled
Loading...
Searching...
No Matches
SField.h
1#ifndef XRPL_PROTOCOL_SFIELD_H_INCLUDED
2#define XRPL_PROTOCOL_SFIELD_H_INCLUDED
3
4#include <xrpl/basics/safe_cast.h>
5#include <xrpl/json/json_value.h>
6#include <xrpl/protocol/Units.h>
7
8#include <cstdint>
9#include <map>
10
11namespace ripple {
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#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
91enum SerializedTypeID { XMACRO(TO_ENUM) };
92
94
95#undef XMACRO
96#undef TO_ENUM
97
98#pragma pop_macro("XMACRO")
99#pragma pop_macro("TO_ENUM")
100#pragma pop_macro("TO_MAP")
101
102// constexpr
103inline int
105{
106 return (safe_cast<int>(id) << 16) | index;
107}
108
109// constexpr
110inline int
111field_code(int id, int index)
112{
113 return (id << 16) | index;
114}
115
127{
128public:
129 enum {
130 sMD_Never = 0x00,
131 sMD_ChangeOrig = 0x01, // original value when it changes
132 sMD_ChangeNew = 0x02, // new value when it changes
133 sMD_DeleteFinal = 0x04, // final value when it is deleted
134 sMD_Create = 0x08, // value when it's created
135 sMD_Always = 0x10, // value when node containing it is affected at all
136 sMD_BaseTen = 0x20, // value is treated as base 10, overriding behavior
137 sMD_PseudoAccount = 0x40, // if this field is set in an ACCOUNT_ROOT
138 // _only_, then it is a pseudo-account
141 };
142
143 enum class IsSigning : unsigned char { no, yes };
145
146 int const fieldCode; // (type<<16)|index
148 int const fieldValue; // Code number for protocol
150 int const fieldMeta;
151 int const fieldNum;
154
155 SField(SField const&) = delete;
156 SField&
157 operator=(SField const&) = delete;
158 SField(SField&&) = delete;
159 SField&
160 operator=(SField&&) = delete;
161
162public:
163 struct private_access_tag_t; // public, but still an implementation detail
164
165 // These constructors can only be called from SField.cpp
166 SField(
169 int fv,
170 char const* fn,
171 int meta = sMD_Default,
172 IsSigning signing = IsSigning::yes);
173 explicit SField(private_access_tag_t, int fc, char const* fn);
174
175 static SField const&
176 getField(int fieldCode);
177 static SField const&
179 static SField const&
180 getField(int type, int value)
181 {
182 return getField(field_code(type, value));
183 }
184
185 static SField const&
186 getField(SerializedTypeID type, int value)
187 {
188 return getField(field_code(type, value));
189 }
190
191 std::string const&
192 getName() const
193 {
194 return fieldName;
195 }
196
197 bool
198 hasName() const
199 {
200 return fieldCode > 0;
201 }
202
203 Json::StaticString const&
205 {
206 return jsonName;
207 }
208
209 operator Json::StaticString const&() const
210 {
211 return jsonName;
212 }
213
214 bool
215 isInvalid() const
216 {
217 return fieldCode == -1;
218 }
219
220 bool
221 isUseful() const
222 {
223 return fieldCode > 0;
224 }
225
226 bool
227 isBinary() const
228 {
229 return fieldValue < 256;
230 }
231
232 // A discardable field is one that cannot be serialized, and
233 // should be discarded during serialization,like 'hash'.
234 // You cannot serialize an object's hash inside that object,
235 // but you can have it in the JSON representation.
236 bool
238 {
239 return fieldValue > 256;
240 }
241
242 int
243 getCode() const
244 {
245 return fieldCode;
246 }
247 int
248 getNum() const
249 {
250 return fieldNum;
251 }
252 static int
254 {
255 return num;
256 }
257
258 bool
259 shouldMeta(int c) const
260 {
261 return (fieldMeta & c) != 0;
262 }
263
264 bool
265 shouldInclude(bool withSigningField) const
266 {
267 return (fieldValue < 256) &&
268 (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, ...) \
363 extern SField const sfName;
364#define TYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
365 extern SF_##stiSuffix const sfName;
366
367extern SField const sfInvalid;
368extern SField const sfGeneric;
369
370#include <xrpl/protocol/detail/sfields.macro>
371
372#undef TYPED_SFIELD
373#pragma pop_macro("TYPED_SFIELD")
374#undef UNTYPED_SFIELD
375#pragma pop_macro("UNTYPED_SFIELD")
376
377} // namespace ripple
378
379#endif
Lightweight wrapper to tag static string.
Definition json_value.h:45
Identifies fields.
Definition SField.h:127
int getCode() const
Definition SField.h:243
Json::StaticString const & getJsonName() const
Definition SField.h:204
int const fieldMeta
Definition SField.h:150
SField(SField const &)=delete
bool isInvalid() const
Definition SField.h:215
bool hasName() const
Definition SField.h:198
std::string const fieldName
Definition SField.h:149
@ sMD_PseudoAccount
Definition SField.h:137
int const fieldCode
Definition SField.h:146
static int num
Definition SField.h:293
static std::unordered_map< int, SField const * > knownCodeToField
Definition SField.h:294
bool operator==(SField const &f) const
Definition SField.h:272
static int getNumFields()
Definition SField.h:253
SField(SField &&)=delete
bool isDiscardable() const
Definition SField.h:237
std::string const & getName() const
Definition SField.h:192
SField & operator=(SField const &)=delete
bool shouldInclude(bool withSigningField) const
Definition SField.h:265
SField & operator=(SField &&)=delete
int const fieldNum
Definition SField.h:151
bool isBinary() const
Definition SField.h:227
static IsSigning const notSigning
Definition SField.h:144
int const fieldValue
Definition SField.h:148
static SField const & getField(int type, int value)
Definition SField.h:180
static SField const & getField(SerializedTypeID type, int value)
Definition SField.h:186
Json::StaticString const jsonName
Definition SField.h:153
int getNum() const
Definition SField.h:248
bool shouldMeta(int c) const
Definition SField.h:259
IsSigning const signingField
Definition SField.h:152
static SField const & getField(int fieldCode)
Definition SField.cpp:116
static std::unordered_map< int, SField const * > const & getKnownCodeToField()
Definition SField.h:287
bool isUseful() const
Definition SField.h:221
SerializedTypeID const fieldType
Definition SField.h:147
static std::unordered_map< std::string, SField const * > knownNameToField
Definition SField.h:295
bool operator!=(SField const &f) const
Definition SField.h:278
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
static std::map< std::string, int > const sTypeMap
Definition SField.h:93
SerializedTypeID
Definition SField.h:91
@ XMACRO
Definition SField.h:91
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition ApplyView.h:60
int field_code(SerializedTypeID id, int index)
Definition SField.h:104
SField const sfGeneric
SField const sfInvalid
Indicate std::optional field semantics.
Definition SField.h:311
OptionaledField(TypedField< T > const &f_)
Definition SField.h:314
TypedField< T > const * f
Definition SField.h:312
A field with a type known at compile time.
Definition SField.h:301