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
29namespace ripple {
30
31/*
32
33Some fields have a different meaning for their
34 default value versus not present.
35 Example:
36 QualityIn on a TrustLine
37
38*/
39
40//------------------------------------------------------------------------------
41
42// Forwards
43class STAccount;
44class STAmount;
45class STIssue;
46class STBlob;
47template <int>
48class STBitString;
49template <class>
50class STInteger;
51class STNumber;
52class STXChainBridge;
53class STVector256;
54class STCurrency;
55
56#pragma push_macro("XMACRO")
57#undef XMACRO
58
59#define XMACRO(STYPE) \
60 /* special types */ \
61 STYPE(STI_UNKNOWN, -2) \
62 STYPE(STI_NOTPRESENT, 0) \
63 STYPE(STI_UINT16, 1) \
64 \
65 /* types (common) */ \
66 STYPE(STI_UINT32, 2) \
67 STYPE(STI_UINT64, 3) \
68 STYPE(STI_UINT128, 4) \
69 STYPE(STI_UINT256, 5) \
70 STYPE(STI_AMOUNT, 6) \
71 STYPE(STI_VL, 7) \
72 STYPE(STI_ACCOUNT, 8) \
73 STYPE(STI_NUMBER, 9) \
74 \
75 /* 10-13 are reserved */ \
76 STYPE(STI_OBJECT, 14) \
77 STYPE(STI_ARRAY, 15) \
78 \
79 /* types (uncommon) */ \
80 STYPE(STI_UINT8, 16) \
81 STYPE(STI_UINT160, 17) \
82 STYPE(STI_PATHSET, 18) \
83 STYPE(STI_VECTOR256, 19) \
84 STYPE(STI_UINT96, 20) \
85 STYPE(STI_UINT192, 21) \
86 STYPE(STI_UINT384, 22) \
87 STYPE(STI_UINT512, 23) \
88 STYPE(STI_ISSUE, 24) \
89 STYPE(STI_XCHAIN_BRIDGE, 25) \
90 STYPE(STI_CURRENCY, 26) \
91 \
92 /* high-level types */ \
93 /* cannot be serialized inside other types */ \
94 STYPE(STI_TRANSACTION, 10001) \
95 STYPE(STI_LEDGERENTRY, 10002) \
96 STYPE(STI_VALIDATION, 10003) \
97 STYPE(STI_METADATA, 10004)
98
99#pragma push_macro("TO_ENUM")
100#undef TO_ENUM
101#pragma push_macro("TO_MAP")
102#undef TO_MAP
103
104#define TO_ENUM(name, value) name = value,
105#define TO_MAP(name, value) {#name, value},
106
107enum SerializedTypeID { XMACRO(TO_ENUM) };
108
110
111#undef XMACRO
112#undef TO_ENUM
113
114#pragma pop_macro("XMACRO")
115#pragma pop_macro("TO_ENUM")
116#pragma pop_macro("TO_MAP")
117
118// constexpr
119inline int
121{
122 return (safe_cast<int>(id) << 16) | index;
123}
124
125// constexpr
126inline int
127field_code(int id, int index)
128{
129 return (id << 16) | index;
130}
131
143{
144public:
145 enum {
146 sMD_Never = 0x00,
147 sMD_ChangeOrig = 0x01, // original value when it changes
148 sMD_ChangeNew = 0x02, // new value when it changes
149 sMD_DeleteFinal = 0x04, // final value when it is deleted
150 sMD_Create = 0x08, // value when it's created
151 sMD_Always = 0x10, // value when node containing it is affected at all
155 };
156
157 enum class IsSigning : unsigned char { no, yes };
159
160 int const fieldCode; // (type<<16)|index
162 int const fieldValue; // Code number for protocol
164 int const fieldMeta;
165 int const fieldNum;
168
169 SField(SField const&) = delete;
170 SField&
171 operator=(SField const&) = delete;
172 SField(SField&&) = delete;
173 SField&
174 operator=(SField&&) = delete;
175
176public:
177 struct private_access_tag_t; // public, but still an implementation detail
178
179 // These constructors can only be called from SField.cpp
180 SField(
183 int fv,
184 char const* fn,
185 int meta = sMD_Default,
186 IsSigning signing = IsSigning::yes);
187 explicit SField(private_access_tag_t, int fc);
188
189 static SField const&
190 getField(int fieldCode);
191 static SField const&
193 static SField const&
194 getField(int type, int value)
195 {
196 return getField(field_code(type, value));
197 }
198
199 static SField const&
200 getField(SerializedTypeID type, int value)
201 {
202 return getField(field_code(type, value));
203 }
204
205 std::string const&
206 getName() const
207 {
208 return fieldName;
209 }
210
211 bool
212 hasName() const
213 {
214 return fieldCode > 0;
215 }
216
217 Json::StaticString const&
219 {
220 return jsonName;
221 }
222
223 operator Json::StaticString const&() const
224 {
225 return jsonName;
226 }
227
228 bool
229 isInvalid() const
230 {
231 return fieldCode == -1;
232 }
233
234 bool
235 isUseful() const
236 {
237 return fieldCode > 0;
238 }
239
240 bool
241 isBinary() const
242 {
243 return fieldValue < 256;
244 }
245
246 // A discardable field is one that cannot be serialized, and
247 // should be discarded during serialization,like 'hash'.
248 // You cannot serialize an object's hash inside that object,
249 // but you can have it in the JSON representation.
250 bool
252 {
253 return fieldValue > 256;
254 }
255
256 int
257 getCode() const
258 {
259 return fieldCode;
260 }
261 int
262 getNum() const
263 {
264 return fieldNum;
265 }
266 static int
268 {
269 return num;
270 }
271
272 bool
273 shouldMeta(int c) const
274 {
275 return (fieldMeta & c) != 0;
276 }
277
278 bool
279 shouldInclude(bool withSigningField) const
280 {
281 return (fieldValue < 256) &&
282 (withSigningField || (signingField == IsSigning::yes));
283 }
284
285 bool
286 operator==(SField const& f) const
287 {
288 return fieldCode == f.fieldCode;
289 }
290
291 bool
292 operator!=(SField const& f) const
293 {
294 return fieldCode != f.fieldCode;
295 }
296
297 static int
298 compare(SField const& f1, SField const& f2);
299
300 static std::map<int, SField const*> const&
302 {
303 return knownCodeToField;
304 }
305
306private:
307 static int num;
309};
310
312template <class T>
314{
315 using type = T;
316
317 template <class... Args>
318 explicit TypedField(private_access_tag_t pat, Args&&... args);
319};
320
322template <class T>
324{
326
327 explicit OptionaledField(TypedField<T> const& f_) : f(&f_)
328 {
329 }
330};
331
332template <class T>
333inline OptionaledField<T>
335{
336 return OptionaledField<T>(f);
337}
338
339//------------------------------------------------------------------------------
340
341//------------------------------------------------------------------------------
342
354
363
364//------------------------------------------------------------------------------
365
366// Use macros for most SField construction to enforce naming conventions.
367#pragma push_macro("UNTYPED_SFIELD")
368#undef UNTYPED_SFIELD
369#pragma push_macro("TYPED_SFIELD")
370#undef TYPED_SFIELD
371
372#define UNTYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
373 extern SField const sfName;
374#define TYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
375 extern SF_##stiSuffix const sfName;
376
377extern SField const sfInvalid;
378extern SField const sfGeneric;
379
380#include <xrpl/protocol/detail/sfields.macro>
381
382#undef TYPED_SFIELD
383#pragma pop_macro("TYPED_SFIELD")
384#undef UNTYPED_SFIELD
385#pragma pop_macro("UNTYPED_SFIELD")
386
387} // namespace ripple
388
389#endif
Lightweight wrapper to tag static string.
Definition: json_value.h:64
Identifies fields.
Definition: SField.h:143
int getCode() const
Definition: SField.h:257
Json::StaticString const & getJsonName() const
Definition: SField.h:218
int const fieldMeta
Definition: SField.h:164
SField(SField const &)=delete
static std::map< int, SField const * > knownCodeToField
Definition: SField.h:308
bool isInvalid() const
Definition: SField.h:229
bool hasName() const
Definition: SField.h:212
std::string const fieldName
Definition: SField.h:163
int const fieldCode
Definition: SField.h:160
static int num
Definition: SField.h:307
bool operator==(SField const &f) const
Definition: SField.h:286
static int getNumFields()
Definition: SField.h:267
SField(SField &&)=delete
bool isDiscardable() const
Definition: SField.h:251
std::string const & getName() const
Definition: SField.h:206
SField & operator=(SField const &)=delete
bool shouldInclude(bool withSigningField) const
Definition: SField.h:279
SField & operator=(SField &&)=delete
int const fieldNum
Definition: SField.h:165
bool isBinary() const
Definition: SField.h:241
static IsSigning const notSigning
Definition: SField.h:158
int const fieldValue
Definition: SField.h:162
static SField const & getField(int type, int value)
Definition: SField.h:194
static SField const & getField(SerializedTypeID type, int value)
Definition: SField.h:200
Json::StaticString const jsonName
Definition: SField.h:167
int getNum() const
Definition: SField.h:262
bool shouldMeta(int c) const
Definition: SField.h:273
IsSigning const signingField
Definition: SField.h:166
static SField const & getField(int fieldCode)
Definition: SField.cpp:118
static std::map< int, SField const * > const & getKnownCodeToField()
Definition: SField.h:301
bool isUseful() const
Definition: SField.h:235
SerializedTypeID const fieldType
Definition: SField.h:161
bool operator!=(SField const &f) const
Definition: SField.h:292
@ sMD_DeleteFinal
Definition: SField.h:149
@ sMD_ChangeOrig
Definition: SField.h:147
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:109
OptionaledField< T > operator~(TypedField< T > const &f)
Definition: SField.h:334
SerializedTypeID
Definition: SField.h:107
@ XMACRO
Definition: SField.h:107
SField const sfInvalid
SField const sfGeneric
int field_code(SerializedTypeID id, int index)
Definition: SField.h:120
Indicate std::optional field semantics.
Definition: SField.h:324
OptionaledField(TypedField< T > const &f_)
Definition: SField.h:327
TypedField< T > const * f
Definition: SField.h:325
A field with a type known at compile time.
Definition: SField.h:314