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
26#include <cstdint>
27#include <map>
28#include <utility>
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
156 };
157
158 enum class IsSigning : unsigned char { no, yes };
160
161 int const fieldCode; // (type<<16)|index
163 int const fieldValue; // Code number for protocol
165 int const fieldMeta;
166 int const fieldNum;
169
170 SField(SField const&) = delete;
171 SField&
172 operator=(SField const&) = delete;
173 SField(SField&&) = delete;
174 SField&
175 operator=(SField&&) = delete;
176
177public:
178 struct private_access_tag_t; // public, but still an implementation detail
179
180 // These constructors can only be called from SField.cpp
181 SField(
184 int fv,
185 const char* fn,
186 int meta = sMD_Default,
187 IsSigning signing = IsSigning::yes);
188 explicit SField(private_access_tag_t, int fc);
189
190 static const SField&
191 getField(int fieldCode);
192 static const SField&
194 static const SField&
195 getField(int type, int value)
196 {
197 return getField(field_code(type, value));
198 }
199
200 static const SField&
201 getField(SerializedTypeID type, int value)
202 {
203 return getField(field_code(type, value));
204 }
205
206 std::string const&
207 getName() const
208 {
209 return fieldName;
210 }
211
212 bool
213 hasName() const
214 {
215 return fieldCode > 0;
216 }
217
218 Json::StaticString const&
220 {
221 return jsonName;
222 }
223
224 operator Json::StaticString const&() const
225 {
226 return jsonName;
227 }
228
229 bool
230 isInvalid() const
231 {
232 return fieldCode == -1;
233 }
234
235 bool
236 isUseful() const
237 {
238 return fieldCode > 0;
239 }
240
241 bool
242 isBinary() const
243 {
244 return fieldValue < 256;
245 }
246
247 // A discardable field is one that cannot be serialized, and
248 // should be discarded during serialization,like 'hash'.
249 // You cannot serialize an object's hash inside that object,
250 // but you can have it in the JSON representation.
251 bool
253 {
254 return fieldValue > 256;
255 }
256
257 int
258 getCode() const
259 {
260 return fieldCode;
261 }
262 int
263 getNum() const
264 {
265 return fieldNum;
266 }
267 static int
269 {
270 return num;
271 }
272
273 bool
274 shouldMeta(int c) const
275 {
276 return (fieldMeta & c) != 0;
277 }
278
279 bool
280 shouldInclude(bool withSigningField) const
281 {
282 return (fieldValue < 256) &&
283 (withSigningField || (signingField == IsSigning::yes));
284 }
285
286 bool
287 operator==(const SField& f) const
288 {
289 return fieldCode == f.fieldCode;
290 }
291
292 bool
293 operator!=(const SField& f) const
294 {
295 return fieldCode != f.fieldCode;
296 }
297
298 static int
299 compare(const SField& f1, const SField& f2);
300
301 static std::map<int, SField const*> const&
303 {
304 return knownCodeToField;
305 }
306
307private:
308 static int num;
310};
311
313template <class T>
315{
316 using type = T;
317
318 template <class... Args>
319 explicit TypedField(private_access_tag_t pat, Args&&... args);
320};
321
323template <class T>
325{
327
328 explicit OptionaledField(TypedField<T> const& f_) : f(&f_)
329 {
330 }
331};
332
333template <class T>
334inline OptionaledField<T>
336{
337 return OptionaledField<T>(f);
338}
339
340//------------------------------------------------------------------------------
341
342//------------------------------------------------------------------------------
343
355
364
365//------------------------------------------------------------------------------
366
367// Use macros for most SField construction to enforce naming conventions.
368#pragma push_macro("UNTYPED_SFIELD")
369#undef UNTYPED_SFIELD
370#pragma push_macro("TYPED_SFIELD")
371#undef TYPED_SFIELD
372
373#define UNTYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
374 extern SField const sfName;
375#define TYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
376 extern SF_##stiSuffix const sfName;
377
378extern SField const sfInvalid;
379extern SField const sfGeneric;
380
381#include <xrpl/protocol/detail/sfields.macro>
382
383#undef TYPED_SFIELD
384#pragma pop_macro("TYPED_SFIELD")
385#undef UNTYPED_SFIELD
386#pragma pop_macro("UNTYPED_SFIELD")
387
388} // namespace ripple
389
390#endif
Lightweight wrapper to tag static string.
Definition: json_value.h:62
Identifies fields.
Definition: SField.h:144
int getCode() const
Definition: SField.h:258
Json::StaticString const & getJsonName() const
Definition: SField.h:219
int const fieldMeta
Definition: SField.h:165
SField(SField const &)=delete
static std::map< int, SField const * > knownCodeToField
Definition: SField.h:309
static const SField & getField(SerializedTypeID type, int value)
Definition: SField.h:201
bool isInvalid() const
Definition: SField.h:230
bool hasName() const
Definition: SField.h:213
std::string const fieldName
Definition: SField.h:164
int const fieldCode
Definition: SField.h:161
static int num
Definition: SField.h:308
bool operator!=(const SField &f) const
Definition: SField.h:293
static int getNumFields()
Definition: SField.h:268
SField(SField &&)=delete
bool isDiscardable() const
Definition: SField.h:252
std::string const & getName() const
Definition: SField.h:207
SField & operator=(SField const &)=delete
bool shouldInclude(bool withSigningField) const
Definition: SField.h:280
SField & operator=(SField &&)=delete
int const fieldNum
Definition: SField.h:166
bool isBinary() const
Definition: SField.h:242
static IsSigning const notSigning
Definition: SField.h:159
int const fieldValue
Definition: SField.h:163
static const SField & getField(int type, int value)
Definition: SField.h:195
Json::StaticString const jsonName
Definition: SField.h:168
int getNum() const
Definition: SField.h:263
bool shouldMeta(int c) const
Definition: SField.h:274
IsSigning const signingField
Definition: SField.h:167
static const SField & getField(int fieldCode)
Definition: SField.cpp:118
static std::map< int, SField const * > const & getKnownCodeToField()
Definition: SField.h:302
bool isUseful() const
Definition: SField.h:236
SerializedTypeID const fieldType
Definition: SField.h:162
bool operator==(const SField &f) const
Definition: SField.h:287
@ sMD_DeleteFinal
Definition: SField.h:150
@ sMD_ChangeOrig
Definition: SField.h:148
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
static std::map< std::string, int > const sTypeMap
Definition: SField.h:110
OptionaledField< T > operator~(TypedField< T > const &f)
Definition: SField.h:335
SerializedTypeID
Definition: SField.h:108
@ XMACRO
Definition: SField.h:108
SField const sfInvalid
SField const sfGeneric
int field_code(SerializedTypeID id, int index)
Definition: SField.h:121
Indicate std::optional field semantics.
Definition: SField.h:325
OptionaledField(TypedField< T > const &f_)
Definition: SField.h:328
TypedField< T > const * f
Definition: SField.h:326
A field with a type known at compile time.
Definition: SField.h:315