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