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