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(SerialIter& sit, SField const& name)
49  : STBitString(name, sit.getBitString<Bits>())
50  {
51  }
52 
53  STBase*
54  copy(std::size_t n, void* buf) const override
55  {
56  return emplace(n, buf, *this);
57  }
58 
59  STBase*
60  move(std::size_t n, void* buf) override
61  {
62  return emplace(n, buf, std::move(*this));
63  }
64 
66  getSType() const override;
67 
69  getText() const override
70  {
71  return to_string(value_);
72  }
73 
74  bool
75  isEquivalent(const STBase& t) const override
76  {
77  const STBitString* v = dynamic_cast<const STBitString*>(&t);
78  return v && (value_ == v->value_);
79  }
80 
81  void
82  add(Serializer& s) const override
83  {
84  assert(fName->isBinary());
85  assert(fName->fieldType == getSType());
86  s.addBitString<Bits>(value_);
87  }
88 
89  template <typename Tag>
90  void
92  {
93  value_ = v;
94  }
95 
96  value_type const&
97  value() const
98  {
99  return value_;
100  }
101 
102  operator value_type() const
103  {
104  return value_;
105  }
106 
107  bool
108  isDefault() const override
109  {
110  return value_ == beast::zero;
111  }
112 
113 private:
115 };
116 
120 
121 template <>
122 inline SerializedTypeID
124 {
125  return STI_HASH128;
126 }
127 
128 template <>
129 inline SerializedTypeID
130 STHash160::getSType() const
131 {
132  return STI_HASH160;
133 }
134 
135 template <>
136 inline SerializedTypeID
137 STHash256::getSType() const
138 {
139  return STI_HASH256;
140 }
141 
142 } // namespace ripple
143 
144 #endif
std::string
STL class.
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:48
ripple::STBitString::value_
value_type value_
Definition: STBitString.h:114
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:45
ripple::STI_HASH160
@ STI_HASH160
Definition: SField.h:73
ripple::STBitString::setValue
void setValue(base_uint< Bits, Tag > const &v)
Definition: STBitString.h:91
ripple::STBitString
Definition: SField.h:47
ripple::STBitString::add
void add(Serializer &s) const override
Definition: STBitString.h:82
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:108
ripple::STBitString::STBitString
STBitString()=default
ripple::STI_HASH256
@ STI_HASH256
Definition: SField.h:63
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:75
ripple::STBitString::move
STBase * move(std::size_t n, void *buf) override
Definition: STBitString.h:60
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:97
ripple::STBitString::getText
std::string getText() const override
Definition: STBitString.h:69
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:123
ripple::STBitString::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STBitString.h:54