rippled
Loading...
Searching...
No Matches
SField.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_SFIELD_H_INCLUDED
21#define RIPPLE_PROTOCOL_SFIELD_H_INCLUDED
22
23#include <xrpl/basics/safe_cast.h>
24#include <xrpl/json/json_value.h>
25#include <xrpl/protocol/Units.h>
26
27#include <cstdint>
28#include <map>
29
30namespace ripple {
31
32/*
33
34Some fields have a different meaning for their
35 default value versus not present.
36 Example:
37 QualityIn on a TrustLine
38
39*/
40
41//------------------------------------------------------------------------------
42
43// Forwards
44class STAccount;
45class STAmount;
46class STIssue;
47class STBlob;
48template <int>
49class STBitString;
50template <class>
51class STInteger;
52class STNumber;
53class STXChainBridge;
54class STVector256;
55class STCurrency;
56
57#pragma push_macro("XMACRO")
58#undef XMACRO
59
60#define XMACRO(STYPE) \
61 /* special types */ \
62 STYPE(STI_UNKNOWN, -2) \
63 STYPE(STI_NOTPRESENT, 0) \
64 STYPE(STI_UINT16, 1) \
65 \
66 /* types (common) */ \
67 STYPE(STI_UINT32, 2) \
68 STYPE(STI_UINT64, 3) \
69 STYPE(STI_UINT128, 4) \
70 STYPE(STI_UINT256, 5) \
71 STYPE(STI_AMOUNT, 6) \
72 STYPE(STI_VL, 7) \
73 STYPE(STI_ACCOUNT, 8) \
74 STYPE(STI_NUMBER, 9) \
75 \
76 /* 10-13 are reserved */ \
77 STYPE(STI_OBJECT, 14) \
78 STYPE(STI_ARRAY, 15) \
79 \
80 /* types (uncommon) */ \
81 STYPE(STI_UINT8, 16) \
82 STYPE(STI_UINT160, 17) \
83 STYPE(STI_PATHSET, 18) \
84 STYPE(STI_VECTOR256, 19) \
85 STYPE(STI_UINT96, 20) \
86 STYPE(STI_UINT192, 21) \
87 STYPE(STI_UINT384, 22) \
88 STYPE(STI_UINT512, 23) \
89 STYPE(STI_ISSUE, 24) \
90 STYPE(STI_XCHAIN_BRIDGE, 25) \
91 STYPE(STI_CURRENCY, 26) \
92 \
93 /* high-level types */ \
94 /* cannot be serialized inside other types */ \
95 STYPE(STI_TRANSACTION, 10001) \
96 STYPE(STI_LEDGERENTRY, 10002) \
97 STYPE(STI_VALIDATION, 10003) \
98 STYPE(STI_METADATA, 10004)
99
100#pragma push_macro("TO_ENUM")
101#undef TO_ENUM
102#pragma push_macro("TO_MAP")
103#undef TO_MAP
104
105#define TO_ENUM(name, value) name = value,
106#define TO_MAP(name, value) {#name, value},
107
108enum SerializedTypeID { XMACRO(TO_ENUM) };
109
111
112#undef XMACRO
113#undef TO_ENUM
114
115#pragma pop_macro("XMACRO")
116#pragma pop_macro("TO_ENUM")
117#pragma pop_macro("TO_MAP")
118
119// constexpr
120inline int
122{
123 return (safe_cast<int>(id) << 16) | index;
124}
125
126// constexpr
127inline int
128field_code(int id, int index)
129{
130 return (id << 16) | index;
131}
132
144{
145public:
146 enum {
147 sMD_Never = 0x00,
148 sMD_ChangeOrig = 0x01, // original value when it changes
149 sMD_ChangeNew = 0x02, // new value when it changes
150 sMD_DeleteFinal = 0x04, // final value when it is deleted
151 sMD_Create = 0x08, // value when it's created
152 sMD_Always = 0x10, // value when node containing it is affected at all
153 sMD_BaseTen = 0x20, // value is treated as base 10, overriding behavior
154 sMD_PseudoAccount = 0x40, // if this field is set in an ACCOUNT_ROOT
155 // _only_, then it is a pseudo-account
158 };
159
160 enum class IsSigning : unsigned char { no, yes };
162
163 int const fieldCode; // (type<<16)|index
165 int const fieldValue; // Code number for protocol
167 int const fieldMeta;
168 int const fieldNum;
171
172 SField(SField const&) = delete;
173 SField&
174 operator=(SField const&) = delete;
175 SField(SField&&) = delete;
176 SField&
177 operator=(SField&&) = delete;
178
179public:
180 struct private_access_tag_t; // public, but still an implementation detail
181
182 // These constructors can only be called from SField.cpp
183 SField(
186 int fv,
187 char const* fn,
188 int meta = sMD_Default,
189 IsSigning signing = IsSigning::yes);
190 explicit SField(private_access_tag_t, int fc, char const* fn);
191
192 static SField const&
193 getField(int fieldCode);
194 static SField const&
196 static SField const&
197 getField(int type, int value)
198 {
199 return getField(field_code(type, value));
200 }
201
202 static SField const&
203 getField(SerializedTypeID type, int value)
204 {
205 return getField(field_code(type, value));
206 }
207
208 std::string const&
209 getName() const
210 {
211 return fieldName;
212 }
213
214 bool
215 hasName() const
216 {
217 return fieldCode > 0;
218 }
219
220 Json::StaticString const&
222 {
223 return jsonName;
224 }
225
226 operator Json::StaticString const&() const
227 {
228 return jsonName;
229 }
230
231 bool
232 isInvalid() const
233 {
234 return fieldCode == -1;
235 }
236
237 bool
238 isUseful() const
239 {
240 return fieldCode > 0;
241 }
242
243 bool
244 isBinary() const
245 {
246 return fieldValue < 256;
247 }
248
249 // A discardable field is one that cannot be serialized, and
250 // should be discarded during serialization,like 'hash'.
251 // You cannot serialize an object's hash inside that object,
252 // but you can have it in the JSON representation.
253 bool
255 {
256 return fieldValue > 256;
257 }
258
259 int
260 getCode() const
261 {
262 return fieldCode;
263 }
264 int
265 getNum() const
266 {
267 return fieldNum;
268 }
269 static int
271 {
272 return num;
273 }
274
275 bool
276 shouldMeta(int c) const
277 {
278 return (fieldMeta & c) != 0;
279 }
280
281 bool
282 shouldInclude(bool withSigningField) const
283 {
284 return (fieldValue < 256) &&
285 (withSigningField || (signingField == IsSigning::yes));
286 }
287
288 bool
289 operator==(SField const& f) const
290 {
291 return fieldCode == f.fieldCode;
292 }
293
294 bool
295 operator!=(SField const& f) const
296 {
297 return fieldCode != f.fieldCode;
298 }
299
300 static int
301 compare(SField const& f1, SField const& f2);
302
305 {
306 return knownCodeToField;
307 }
308
309private:
310 static int num;
313};
314
316template <class T>
318{
319 using type = T;
320
321 template <class... Args>
322 explicit TypedField(private_access_tag_t pat, Args&&... args);
323};
324
326template <class T>
328{
330
331 explicit OptionaledField(TypedField<T> const& f_) : f(&f_)
332 {
333 }
334};
335
336template <class T>
337inline OptionaledField<T>
339{
340 return OptionaledField<T>(f);
341}
342
343//------------------------------------------------------------------------------
344
345//------------------------------------------------------------------------------
346
358
367
368//------------------------------------------------------------------------------
369
370// Use macros for most SField construction to enforce naming conventions.
371#pragma push_macro("UNTYPED_SFIELD")
372#undef UNTYPED_SFIELD
373#pragma push_macro("TYPED_SFIELD")
374#undef TYPED_SFIELD
375
376#define UNTYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
377 extern SField const sfName;
378#define TYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
379 extern SF_##stiSuffix const sfName;
380
381extern SField const sfInvalid;
382extern SField const sfGeneric;
383
384#include <xrpl/protocol/detail/sfields.macro>
385
386#undef TYPED_SFIELD
387#pragma pop_macro("TYPED_SFIELD")
388#undef UNTYPED_SFIELD
389#pragma pop_macro("UNTYPED_SFIELD")
390
391} // namespace ripple
392
393#endif
Lightweight wrapper to tag static string.
Definition json_value.h:63
Identifies fields.
Definition SField.h:144
int getCode() const
Definition SField.h:260
Json::StaticString const & getJsonName() const
Definition SField.h:221
int const fieldMeta
Definition SField.h:167
SField(SField const &)=delete
bool isInvalid() const
Definition SField.h:232
bool hasName() const
Definition SField.h:215
std::string const fieldName
Definition SField.h:166
int const fieldCode
Definition SField.h:163
static int num
Definition SField.h:310
static std::unordered_map< int, SField const * > knownCodeToField
Definition SField.h:311
bool operator==(SField const &f) const
Definition SField.h:289
static int getNumFields()
Definition SField.h:270
SField(SField &&)=delete
bool isDiscardable() const
Definition SField.h:254
std::string const & getName() const
Definition SField.h:209
SField & operator=(SField const &)=delete
bool shouldInclude(bool withSigningField) const
Definition SField.h:282
SField & operator=(SField &&)=delete
int const fieldNum
Definition SField.h:168
bool isBinary() const
Definition SField.h:244
static IsSigning const notSigning
Definition SField.h:161
int const fieldValue
Definition SField.h:165
static SField const & getField(int type, int value)
Definition SField.h:197
static SField const & getField(SerializedTypeID type, int value)
Definition SField.h:203
Json::StaticString const jsonName
Definition SField.h:170
int getNum() const
Definition SField.h:265
bool shouldMeta(int c) const
Definition SField.h:276
IsSigning const signingField
Definition SField.h:169
static SField const & getField(int fieldCode)
Definition SField.cpp:135
static std::unordered_map< int, SField const * > const & getKnownCodeToField()
Definition SField.h:304
bool isUseful() const
Definition SField.h:238
SerializedTypeID const fieldType
Definition SField.h:164
static std::unordered_map< std::string, SField const * > knownNameToField
Definition SField.h:312
bool operator!=(SField const &f) const
Definition SField.h:295
@ sMD_PseudoAccount
Definition SField.h:154
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
static std::map< std::string, int > const sTypeMap
Definition SField.h:110
SerializedTypeID
Definition SField.h:108
@ XMACRO
Definition SField.h:108
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition ApplyView.h:79
int field_code(SerializedTypeID id, int index)
Definition SField.h:121
SField const sfGeneric
SField const sfInvalid
Indicate std::optional field semantics.
Definition SField.h:328
OptionaledField(TypedField< T > const &f_)
Definition SField.h:331
TypedField< T > const * f
Definition SField.h:329
A field with a type known at compile time.
Definition SField.h:318