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