rippled
Loading...
Searching...
No Matches
SField.cpp
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#include <xrpl/beast/utility/instrumentation.h>
21#include <xrpl/protocol/SField.h>
22#include <string>
23#include <string_view>
24#include <utility>
25
26namespace ripple {
27
28// Storage for static const members.
30int SField::num = 0;
32
33// Give only this translation unit permission to construct SFields
35{
36 explicit private_access_tag_t() = default;
37};
38
40
41template <class T>
42template <class... Args>
44 : SField(pat, std::forward<Args>(args)...)
45{
46}
47
48// Construct all compile-time SFields, and register them in the knownCodeToField
49// database:
50
51// Use macros for most SField construction to enforce naming conventions.
52#pragma push_macro("UNTYPED_SFIELD")
53#undef UNTYPED_SFIELD
54#pragma push_macro("TYPED_SFIELD")
55#undef TYPED_SFIELD
56
57#define UNTYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
58 SField const sfName( \
59 access, \
60 STI_##stiSuffix, \
61 fieldValue, \
62 std::string_view(#sfName).substr(2).data(), \
63 ##__VA_ARGS__);
64#define TYPED_SFIELD(sfName, stiSuffix, fieldValue, ...) \
65 SF_##stiSuffix const sfName( \
66 access, \
67 STI_##stiSuffix, \
68 fieldValue, \
69 std::string_view(#sfName).substr(2).data(), \
70 ##__VA_ARGS__);
71
72// SFields which, for historical reasons, do not follow naming conventions.
75// The following two fields aren't used anywhere, but they break tests/have
76// downstream effects.
77SField const sfHash(access, STI_UINT256, 257, "hash");
78SField const sfIndex(access, STI_UINT256, 258, "index");
79
80#include <xrpl/protocol/detail/sfields.macro>
81
82#undef TYPED_SFIELD
83#pragma pop_macro("TYPED_SFIELD")
84#undef UNTYPED_SFIELD
85#pragma pop_macro("UNTYPED_SFIELD")
86
90 int fv,
91 const char* fn,
92 int meta,
93 IsSigning signing)
94 : fieldCode(field_code(tid, fv))
95 , fieldType(tid)
96 , fieldValue(fv)
97 , fieldName(fn)
98 , fieldMeta(meta)
99 , fieldNum(++num)
100 , signingField(signing)
101 , jsonName(fieldName.c_str())
102{
104}
105
107 : fieldCode(fc)
108 , fieldType(STI_UNKNOWN)
109 , fieldValue(0)
110 , fieldMeta(sMD_Never)
111 , fieldNum(++num)
112 , signingField(IsSigning::yes)
113 , jsonName(fieldName.c_str())
114{
116}
117
118SField const&
120{
121 auto it = knownCodeToField.find(code);
122
123 if (it != knownCodeToField.end())
124 {
125 return *(it->second);
126 }
127 return sfInvalid;
128}
129
130int
131SField::compare(SField const& f1, SField const& f2)
132{
133 // -1 = f1 comes before f2, 0 = illegal combination, 1 = f1 comes after f2
134 if ((f1.fieldCode <= 0) || (f2.fieldCode <= 0))
135 return 0;
136
137 if (f1.fieldCode < f2.fieldCode)
138 return -1;
139
140 if (f2.fieldCode < f1.fieldCode)
141 return 1;
142
143 return 0;
144}
145
146SField const&
148{
149 for (auto const& [_, f] : knownCodeToField)
150 {
151 (void)_;
152 if (f->fieldName == fieldName)
153 return *f;
154 }
155 return sfInvalid;
156}
157
158} // namespace ripple
Identifies fields.
Definition: SField.h:144
SField(SField const &)=delete
static std::map< int, SField const * > knownCodeToField
Definition: SField.h:309
std::string const fieldName
Definition: SField.h:164
int const fieldCode
Definition: SField.h:161
static int num
Definition: SField.h:308
static IsSigning const notSigning
Definition: SField.h:159
static int compare(const SField &f1, const SField &f2)
Definition: SField.cpp:131
static const SField & getField(int fieldCode)
Definition: SField.cpp:119
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
SField const sfIndex(access, STI_UINT256, 258, "index")
SerializedTypeID
Definition: SField.h:108
SField const sfInvalid
SField const sfGeneric
@ yes
Definition: Steps.h:42
static SField::private_access_tag_t access
Definition: SField.cpp:39
int field_code(SerializedTypeID id, int index)
Definition: SField.h:121
SField const sfHash(access, STI_UINT256, 257, "hash")
STL namespace.
TypedField(private_access_tag_t pat, Args &&... args)
Definition: SField.cpp:43