rippled
STBitString.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_STBITSTRING_H_INCLUDED
21 #define RIPPLE_PROTOCOL_STBITSTRING_H_INCLUDED
22 
23 #include <ripple/beast/utility/Zero.h>
24 #include <ripple/protocol/STBase.h>
25 
26 namespace ripple {
27 
28 // The template parameter could be an unsigned type, however there's a bug in
29 // gdb (last checked in gdb 12.1) that prevents gdb from finding the RTTI
30 // information of a template parameterized by an unsigned type. This RTTI
31 // information is needed to write gdb pretty printers.
32 template <int Bits>
33 class STBitString final : public STBase
34 {
35  static_assert(Bits > 0, "Number of bits must be positive");
36 
37 public:
39 
40 private:
42 
43 public:
44  STBitString() = default;
45 
46  STBitString(SField const& n);
47  STBitString(const value_type& v);
48  STBitString(SField const& n, const value_type& v);
49  STBitString(SerialIter& sit, SField const& name);
50 
52  getSType() const override;
53 
55  getText() const override;
56 
57  bool
58  isEquivalent(const STBase& t) const override;
59 
60  void
61  add(Serializer& s) const override;
62 
63  bool
64  isDefault() const override;
65 
66  template <typename Tag>
67  void
69 
70  value_type const&
71  value() const;
72 
73  operator value_type() const;
74 
75 private:
76  STBase*
77  copy(std::size_t n, void* buf) const override;
78  STBase*
79  move(std::size_t n, void* buf) override;
80 
81  friend class detail::STVar;
82 };
83 
87 
88 template <int Bits>
90 {
91 }
92 
93 template <int Bits>
94 inline STBitString<Bits>::STBitString(const value_type& v) : value_(v)
95 {
96 }
97 
98 template <int Bits>
100  : STBase(n), value_(v)
101 {
102 }
103 
104 template <int Bits>
106  : STBitString(name, sit.getBitString<Bits>())
107 {
108 }
109 
110 template <int Bits>
111 STBase*
113 {
114  return emplace(n, buf, *this);
115 }
116 
117 template <int Bits>
118 STBase*
120 {
121  return emplace(n, buf, std::move(*this));
122 }
123 
124 template <>
125 inline SerializedTypeID
127 {
128  return STI_UINT128;
129 }
130 
131 template <>
132 inline SerializedTypeID
133 STUInt160::getSType() const
134 {
135  return STI_UINT160;
136 }
137 
138 template <>
139 inline SerializedTypeID
140 STUInt256::getSType() const
141 {
142  return STI_UINT256;
143 }
144 
145 template <int Bits>
148 {
149  return to_string(value_);
150 }
151 
152 template <int Bits>
153 bool
155 {
156  const STBitString* v = dynamic_cast<const STBitString*>(&t);
157  return v && (value_ == v->value_);
158 }
159 
160 template <int Bits>
161 void
163 {
164  assert(getFName().isBinary());
165  assert(getFName().fieldType == getSType());
166  s.addBitString<Bits>(value_);
167 }
168 
169 template <int Bits>
170 template <typename Tag>
171 void
173 {
174  value_ = v;
175 }
176 
177 template <int Bits>
178 typename STBitString<Bits>::value_type const&
180 {
181  return value_;
182 }
183 
184 template <int Bits>
186 {
187  return value_;
188 }
189 
190 template <int Bits>
191 bool
193 {
194  return value_ == beast::zero;
195 }
196 
197 } // namespace ripple
198 
199 #endif
std::string
STL class.
ripple::STI_UINT128
@ STI_UINT128
Definition: SField.h:61
ripple::STBitString::value
value_type const & value() const
Definition: STBitString.h:179
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STI_UINT160
@ STI_UINT160
Definition: SField.h:72
ripple::STBitString::value_
value_type value_
Definition: STBitString.h:41
ripple::STBitString::setValue
void setValue(base_uint< Bits, Tag > const &v)
Definition: STBitString.h:172
ripple::STBitString::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STBitString.h:112
ripple::STBitString
Definition: SField.h:47
ripple::STBitString::move
STBase * move(std::size_t n, void *buf) override
Definition: STBitString.h:119
ripple::STBitString::add
void add(Serializer &s) const override
Definition: STBitString.h:162
ripple::base_uint< Bits >
ripple::STBitString::value_type
base_uint< Bits > value_type
Definition: STBitString.h:38
ripple::STBitString::isDefault
bool isDefault() const override
Definition: STBitString.h:192
ripple::STBitString::getSType
SerializedTypeID getSType() const override
Definition: STBitString.h:126
ripple::STBitString::STBitString
STBitString()=default
ripple::SerialIter
Definition: Serializer.h:310
ripple::STBitString::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STBitString.h:154
ripple::Serializer
Definition: Serializer.h:39
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Serializer::addBitString
int addBitString(base_uint< Bits, Tag > const &v)
Definition: Serializer.h:97
ripple::STBitString::getText
std::string getText() const override
Definition: STBitString.h:147
ripple::SField
Identifies fields.
Definition: SField.h:112
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:66
std::size_t
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::STI_UINT256
@ STI_UINT256
Definition: SField.h:62
ripple::detail::STVar
Definition: STVar.h:49