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  STBitString() = default;
35 
36  STBitString(SField const& n) : STBase(n)
37  {
38  }
39 
40  STBitString(const value_type& v) : value_(v)
41  {
42  }
43 
44  STBitString(SField const& n, const value_type& v) : STBase(n), value_(v)
45  {
46  }
47 
48  STBitString(SField const& n, const char* v) : STBase(n)
49  {
50  value_.SetHex(v);
51  }
52 
53  STBitString(SField const& n, std::string const& v) : STBase(n)
54  {
55  value_.SetHex(v);
56  }
57 
58  STBitString(SerialIter& sit, SField const& name)
59  : STBitString(name, sit.getBitString<Bits>())
60  {
61  }
62 
63  STBase*
64  copy(std::size_t n, void* buf) const override
65  {
66  return emplace(n, buf, *this);
67  }
68 
69  STBase*
70  move(std::size_t n, void* buf) override
71  {
72  return emplace(n, buf, std::move(*this));
73  }
74 
76  getSType() const override;
77 
79  getText() const override
80  {
81  return to_string(value_);
82  }
83 
84  bool
85  isEquivalent(const STBase& t) const override
86  {
87  const STBitString* v = dynamic_cast<const STBitString*>(&t);
88  return v && (value_ == v->value_);
89  }
90 
91  void
92  add(Serializer& s) const override
93  {
94  assert(fName->isBinary());
95  assert(fName->fieldType == getSType());
96  s.addBitString<Bits>(value_);
97  }
98 
99  template <typename Tag>
100  void
102  {
103  value_ = v;
104  }
105 
106  value_type const&
107  value() const
108  {
109  return value_;
110  }
111 
112  operator value_type() const
113  {
114  return value_;
115  }
116 
117  bool
118  isDefault() const override
119  {
120  return value_ == beast::zero;
121  }
122 
123 private:
125 };
126 
130 
131 template <>
132 inline SerializedTypeID
134 {
135  return STI_HASH128;
136 }
137 
138 template <>
139 inline SerializedTypeID
140 STHash160::getSType() const
141 {
142  return STI_HASH160;
143 }
144 
145 template <>
146 inline SerializedTypeID
147 STHash256::getSType() const
148 {
149  return STI_HASH256;
150 }
151 
152 } // namespace ripple
153 
154 #endif
std::string
STL class.
ripple::STBitString::STBitString
STBitString(SField const &n, std::string const &v)
Definition: STBitString.h:53
ripple::STBitString::STBitString
STBitString(const value_type &v)
Definition: STBitString.h:40
ripple::STBitString::STBitString
STBitString(SField const &n)
Definition: STBitString.h:36
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STBitString::STBitString
STBitString(SerialIter &sit, SField const &name)
Definition: STBitString.h:58
ripple::STBitString::value_
value_type value_
Definition: STBitString.h:124
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:42
ripple::STI_HASH160
@ STI_HASH160
Definition: SField.h:73
ripple::STBitString::setValue
void setValue(base_uint< Bits, Tag > const &v)
Definition: STBitString.h:101
ripple::STBitString
Definition: SField.h:47
ripple::STBitString::add
void add(Serializer &s) const override
Definition: STBitString.h:92
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:118
ripple::STBitString::STBitString
STBitString()=default
ripple::STI_HASH256
@ STI_HASH256
Definition: SField.h:63
ripple::base_uint::SetHex
bool SetHex(const char *psz, bool bStrict=false)
Parse a hex string into a base_uint The input can be:
Definition: base_uint.h:406
ripple::STBitString::STBitString
STBitString(SField const &n, const value_type &v)
Definition: STBitString.h:44
ripple::SerialIter
Definition: Serializer.h:308
ripple::STBitString::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STBitString.h:85
ripple::STBitString::move
STBase * move(std::size_t n, void *buf) override
Definition: STBitString.h:70
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::value
value_type const & value() const
Definition: STBitString.h:107
ripple::STBitString::getText
std::string getText() const override
Definition: STBitString.h:79
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:62
ripple::STI_HASH128
@ STI_HASH128
Definition: SField.h:62
std::size_t
ripple::STBitString::getSType
SerializedTypeID getSType() const override
Definition: STBitString.h:133
ripple::STBitString::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STBitString.h:64
ripple::STBitString::STBitString
STBitString(SField const &n, const char *v)
Definition: STBitString.h:48