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 STYPE(STI_INT32, 10) \
76 STYPE(STI_INT64, 11) \
77 \
78 /* 12-13 are reserved */ \
79 STYPE(STI_OBJECT, 14) \
80 STYPE(STI_ARRAY, 15) \
81 \
82 /* types (uncommon) */ \
83 STYPE(STI_UINT8, 16) \
84 STYPE(STI_UINT160, 17) \
85 STYPE(STI_PATHSET, 18) \
86 STYPE(STI_VECTOR256, 19) \
87 STYPE(STI_UINT96, 20) \
88 STYPE(STI_UINT192, 21) \
89 STYPE(STI_UINT384, 22) \
90 STYPE(STI_UINT512, 23) \
91 STYPE(STI_ISSUE, 24) \
92 STYPE(STI_XCHAIN_BRIDGE, 25) \
93 STYPE(STI_CURRENCY, 26) \
94 \
95 /* high-level types */ \
96 /* cannot be serialized inside other types */ \
97 STYPE(STI_TRANSACTION, 10001) \
98 STYPE(STI_LEDGERENTRY, 10002) \
99 STYPE(STI_VALIDATION, 10003) \
100 STYPE(STI_METADATA, 10004)
101
102#pragma push_macro("TO_ENUM")
103#undef TO_ENUM
104#pragma push_macro("TO_MAP")
105#undef TO_MAP
106
107#define TO_ENUM(name, value) name = value,
108#define TO_MAP(name, value) {#name, value},
109
110enum SerializedTypeID { XMACRO(TO_ENUM) };
111
113
114#undef XMACRO
115#undef TO_ENUM
116
117#pragma pop_macro("XMACRO")
118#pragma pop_macro("TO_ENUM")
119#pragma pop_macro("TO_MAP")
120
121// constexpr
122inline int
124{
125 return (safe_cast<int>(id) << 16) | index;
126}
127
128// constexpr
129inline int
130field_code(int id, int index)
131{
132 return (id << 16) | index;
133}
134
146{
147public:
148 enum {
149 sMD_Never = 0x00,
150 sMD_ChangeOrig = 0x01, // original value when it changes
151 sMD_ChangeNew = 0x02, // new value when it changes
152 sMD_DeleteFinal = 0x04, // final value when it is deleted
153 sMD_Create = 0x08, // value when it's created
154 sMD_Always = 0x10, // value when node containing it is affected at all
155 sMD_BaseTen = 0x20, // value is treated as base 10, overriding behavior
156 sMD_PseudoAccount = 0x40, // if this field is set in an ACCOUNT_ROOT
157 // _only_, then it is a pseudo-account
160 };
161
162 enum class IsSigning : unsigned char { no, yes };
164
165 int const fieldCode; // (type<<16)|index
167 int const fieldValue; // Code number for protocol
169 int const fieldMeta;
170 int const fieldNum;
173
174 SField(SField const&) = delete;
175 SField&
176 operator=(SField const&) = delete;
177 SField(SField&&) = delete;
178 SField&
179 operator=(SField&&) = delete;
180
181public:
182 struct private_access_tag_t; // public, but still an implementation detail
183
184 // These constructors can only be called from SField.cpp
185 SField(
188 int fv,
189 char const* fn,
190 int meta = sMD_Default,
191 IsSigning signing = IsSigning::yes);
192 explicit SField(private_access_tag_t, int fc, char const* fn);
193
194 static SField const&
195 getField(int fieldCode);
196 static SField const&
198 static SField const&
199 getField(int type, int value)
200 {
201 return getField(field_code(type, value));
202 }
203
204 static SField const&
205 getField(SerializedTypeID type, int value)
206 {
207 return getField(field_code(type, value));
208 }
209
210 std::string const&
211 getName() const
212 {
213 return fieldName;
214 }
215
216 bool
217 hasName() const
218 {
219 return fieldCode > 0;
220 }
221
222 Json::StaticString const&
224 {
225 return jsonName;
226 }
227
228 operator Json::StaticString const&() const
229 {
230 return jsonName;
231 }
232
233 bool
234 isInvalid() const
235 {
236 return fieldCode == -1;
237 }
238
239 bool
240 isUseful() const
241 {
242 return fieldCode > 0;
243 }
244
245 bool
246 isBinary() const
247 {
248 return fieldValue < 256;
249 }
250
251 // A discardable field is one that cannot be serialized, and
252 // should be discarded during serialization,like 'hash'.
253 // You cannot serialize an object's hash inside that object,
254 // but you can have it in the JSON representation.
255 bool
257 {
258 return fieldValue > 256;
259 }
260
261 int
262 getCode() const
263 {
264 return fieldCode;
265 }
266 int
267 getNum() const
268 {
269 return fieldNum;
270 }
271 static int
273 {
274 return num;
275 }
276
277 bool
278 shouldMeta(int c) const
279 {
280 return (fieldMeta & c) != 0;
281 }
282
283 bool
284 shouldInclude(bool withSigningField) const
285 {
286 return (fieldValue < 256) &&
287 (withSigningField || (signingField == IsSigning::yes));
288 }
289
290 bool
291 operator==(SField const& f) const
292 {
293 return fieldCode == f.fieldCode;
294 }
295
296 bool
297 operator!=(SField const& f) const
298 {
299 return fieldCode != f.fieldCode;
300 }
301
302 static int
303 compare(SField const& f1, SField const& f2);
304
307 {
308 return knownCodeToField;
309 }
310
311private:
312 static int num;
315};
316
318template <class T>
320{
321 using type = T;
322
323 template <class... Args>
324 explicit TypedField(private_access_tag_t pat, Args&&... args);
325};
326
328template <class T>
330{
332
333 explicit OptionaledField(TypedField<T> const& f_) : f(&f_)
334 {
335 }
336};
337
338template <class T>
339inline OptionaledField<T>
341{
342 return OptionaledField<T>(f);
343}
344
345//------------------------------------------------------------------------------
346
347//------------------------------------------------------------------------------
348
360
363
372
373//------------------------------------------------------------------------------
374
375// Use macros for most SField construction to enforce naming conventions.
376#pragma push_macro("UNTYPED_SFIELD")
377#undef UNTYPED_SFIELD
378#pragma push_macro("TYPED_SFIELD")
379#undef TYPED_SFIELD
380
381#define UNTYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
382 extern SField const sfName;
383#define TYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
384 extern SF_##stiSuffix const sfName;
385
386extern SField const sfInvalid;
387extern SField const sfGeneric;
388
389#include <xrpl/protocol/detail/sfields.macro>
390
391#undef TYPED_SFIELD
392#pragma pop_macro("TYPED_SFIELD")
393#undef UNTYPED_SFIELD
394#pragma pop_macro("UNTYPED_SFIELD")
395
396} // namespace ripple
397
398#endif
Lightweight wrapper to tag static string.
Definition json_value.h:63
Identifies fields.
Definition SField.h:146
int getCode() const
Definition SField.h:262
Json::StaticString const & getJsonName() const
Definition SField.h:223
int const fieldMeta
Definition SField.h:169
SField(SField const &)=delete
bool isInvalid() const
Definition SField.h:234
bool hasName() const
Definition SField.h:217
std::string const fieldName
Definition SField.h:168
@ sMD_PseudoAccount
Definition SField.h:156
int const fieldCode
Definition SField.h:165
static int num
Definition SField.h:312
static std::unordered_map< int, SField const * > knownCodeToField
Definition SField.h:313
bool operator==(SField const &f) const
Definition SField.h:291
static int getNumFields()
Definition SField.h:272
SField(SField &&)=delete
bool isDiscardable() const
Definition SField.h:256
std::string const & getName() const
Definition SField.h:211
SField & operator=(SField const &)=delete
bool shouldInclude(bool withSigningField) const
Definition SField.h:284
SField & operator=(SField &&)=delete
int const fieldNum
Definition SField.h:170
bool isBinary() const
Definition SField.h:246
static IsSigning const notSigning
Definition SField.h:163
int const fieldValue
Definition SField.h:167
static SField const & getField(int type, int value)
Definition SField.h:199
static SField const & getField(SerializedTypeID type, int value)
Definition SField.h:205
Json::StaticString const jsonName
Definition SField.h:172
int getNum() const
Definition SField.h:267
bool shouldMeta(int c) const
Definition SField.h:278
IsSigning const signingField
Definition SField.h:171
static SField const & getField(int fieldCode)
Definition SField.cpp:135
static std::unordered_map< int, SField const * > const & getKnownCodeToField()
Definition SField.h:306
bool isUseful() const
Definition SField.h:240
SerializedTypeID const fieldType
Definition SField.h:166
static std::unordered_map< std::string, SField const * > knownNameToField
Definition SField.h:314
bool operator!=(SField const &f) const
Definition SField.h:297
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:112
SerializedTypeID
Definition SField.h:110
@ XMACRO
Definition SField.h:110
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition ApplyView.h:79
int field_code(SerializedTypeID id, int index)
Definition SField.h:123
SField const sfGeneric
SField const sfInvalid
Indicate std::optional field semantics.
Definition SField.h:330
OptionaledField(TypedField< T > const &f_)
Definition SField.h:333
TypedField< T > const * f
Definition SField.h:331
A field with a type known at compile time.
Definition SField.h:320