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