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 template <std::size_t Bits>
29 class STBitString final : public STBase
30 {
31 public:
33 
34 private:
36 
37 public:
38  STBitString() = default;
39 
40  STBitString(SField const& n);
41  STBitString(const value_type& v);
42  STBitString(SField const& n, const value_type& v);
43  STBitString(SerialIter& sit, SField const& name);
44 
46  getSType() const override;
47 
49  getText() const override;
50 
51  bool
52  isEquivalent(const STBase& t) const override;
53 
54  void
55  add(Serializer& s) const override;
56 
57  bool
58  isDefault() const override;
59 
60  template <typename Tag>
61  void
63 
64  value_type const&
65  value() const;
66 
67  operator value_type() const;
68 
69 private:
70  STBase*
71  copy(std::size_t n, void* buf) const override;
72  STBase*
73  move(std::size_t n, void* buf) override;
74 
75  friend class detail::STVar;
76 };
77 
81 
82 template <std::size_t Bits>
84 {
85 }
86 
87 template <std::size_t Bits>
88 inline STBitString<Bits>::STBitString(const value_type& v) : value_(v)
89 {
90 }
91 
92 template <std::size_t Bits>
94  : STBase(n), value_(v)
95 {
96 }
97 
98 template <std::size_t Bits>
100  : STBitString(name, sit.getBitString<Bits>())
101 {
102 }
103 
104 template <std::size_t Bits>
105 STBase*
107 {
108  return emplace(n, buf, *this);
109 }
110 
111 template <std::size_t Bits>
112 STBase*
114 {
115  return emplace(n, buf, std::move(*this));
116 }
117 
118 template <>
119 inline SerializedTypeID
121 {
122  return STI_HASH128;
123 }
124 
125 template <>
126 inline SerializedTypeID
127 STHash160::getSType() const
128 {
129  return STI_HASH160;
130 }
131 
132 template <>
133 inline SerializedTypeID
134 STHash256::getSType() const
135 {
136  return STI_HASH256;
137 }
138 
139 template <std::size_t Bits>
142 {
143  return to_string(value_);
144 }
145 
146 template <std::size_t Bits>
147 bool
149 {
150  const STBitString* v = dynamic_cast<const STBitString*>(&t);
151  return v && (value_ == v->value_);
152 }
153 
154 template <std::size_t Bits>
155 void
157 {
158  assert(getFName().isBinary());
159  assert(getFName().fieldType == getSType());
160  s.addBitString<Bits>(value_);
161 }
162 
163 template <std::size_t Bits>
164 template <typename Tag>
165 void
167 {
168  value_ = v;
169 }
170 
171 template <std::size_t Bits>
172 typename STBitString<Bits>::value_type const&
174 {
175  return value_;
176 }
177 
178 template <std::size_t Bits>
180 {
181  return value_;
182 }
183 
184 template <std::size_t Bits>
185 bool
187 {
188  return value_ == beast::zero;
189 }
190 
191 } // namespace ripple
192 
193 #endif
std::string
STL class.
ripple::STBitString::value
value_type const & value() const
Definition: STBitString.h:173
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STBitString::value_
value_type value_
Definition: STBitString.h:35
ripple::STI_HASH160
@ STI_HASH160
Definition: SField.h:73
ripple::STBitString::setValue
void setValue(base_uint< Bits, Tag > const &v)
Definition: STBitString.h:166
ripple::STBitString::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STBitString.h:106
ripple::STBitString
Definition: SField.h:47
ripple::STBitString::move
STBase * move(std::size_t n, void *buf) override
Definition: STBitString.h:113
ripple::STBitString::add
void add(Serializer &s) const override
Definition: STBitString.h:156
ripple::base_uint< Bits >
ripple::STBitString::value_type
base_uint< Bits > value_type
Definition: STBitString.h:32
ripple::STBitString::isDefault
bool isDefault() const override
Definition: STBitString.h:186
ripple::STBitString::STBitString
STBitString()=default
ripple::STI_HASH256
@ STI_HASH256
Definition: SField.h:63
ripple::SerialIter
Definition: Serializer.h:310
ripple::STBitString::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STBitString.h:148
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:141
ripple::SField
Identifies fields.
Definition: SField.h:109
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:66
ripple::STI_HASH128
@ STI_HASH128
Definition: SField.h:62
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:38
ripple::STBitString::getSType
SerializedTypeID getSType() const override
Definition: STBitString.h:120
ripple::detail::STVar
Definition: STVar.h:49