rippled
STAccount.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_STACCOUNT_H_INCLUDED
21 #define RIPPLE_PROTOCOL_STACCOUNT_H_INCLUDED
22 
23 #include <ripple/protocol/AccountID.h>
24 #include <ripple/protocol/STBase.h>
25 #include <string>
26 
27 namespace ripple {
28 
29 class STAccount final : public STBase
30 {
31 private:
32  // The original implementation of STAccount kept the value in an STBlob.
33  // But an STAccount is always 160 bits, so we can store it with less
34  // overhead in a ripple::uint160. However, so the serialized format of the
35  // STAccount stays unchanged, we serialize and deserialize like an STBlob.
37  bool default_;
38 
39 public:
41 
42  STAccount();
43  STAccount(SField const& n);
44  STAccount(SField const& n, Buffer&& v);
45  STAccount(SerialIter& sit, SField const& name);
46  STAccount(SField const& n, AccountID const& v);
47 
48  STBase*
49  copy(std::size_t n, void* buf) const override
50  {
51  return emplace(n, buf, *this);
52  }
53 
54  STBase*
55  move(std::size_t n, void* buf) override
56  {
57  return emplace(n, buf, std::move(*this));
58  }
59 
61  getSType() const override
62  {
63  return STI_ACCOUNT;
64  }
65 
67  getText() const override;
68 
69  void
70  add(Serializer& s) const override
71  {
72  assert(fName->isBinary());
73  assert(fName->fieldType == STI_ACCOUNT);
74 
75  // Preserve the serialization behavior of an STBlob:
76  // o If we are default (all zeros) serialize as an empty blob.
77  // o Otherwise serialize 160 bits.
78  int const size = isDefault() ? 0 : uint160::bytes;
79  s.addVL(value_.data(), size);
80  }
81 
82  bool
83  isEquivalent(const STBase& t) const override
84  {
85  auto const* const tPtr = dynamic_cast<STAccount const*>(&t);
86  return tPtr && (default_ == tPtr->default_) && (value_ == tPtr->value_);
87  }
88 
89  bool
90  isDefault() const override
91  {
92  return default_;
93  }
94 
95  STAccount&
97  {
98  setValue(value);
99  return *this;
100  }
101 
102  AccountID
103  value() const noexcept
104  {
105  return value_;
106  }
107 
108  void
109  setValue(AccountID const& v)
110  {
111  value_ = v;
112  default_ = false;
113  }
114 };
115 
116 } // namespace ripple
117 
118 #endif
std::string
STL class.
ripple::STAccount::getSType
SerializedTypeID getSType() const override
Definition: STAccount.h:61
ripple::SField::isBinary
bool isBinary() const
Definition: SField.h:210
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::Buffer
Like std::vector<char> but better.
Definition: Buffer.h:35
ripple::STI_ACCOUNT
@ STI_ACCOUNT
Definition: SField.h:66
ripple::STAccount::move
STBase * move(std::size_t n, void *buf) override
Definition: STAccount.h:55
ripple::STAccount::isDefault
bool isDefault() const override
Definition: STAccount.h:90
ripple::STAccount::STAccount
STAccount()
Definition: STAccount.cpp:26
ripple::base_uint::data
pointer data()
Definition: base_uint.h:113
ripple::STAccount::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STAccount.h:83
ripple::STAccount::default_
bool default_
Definition: STAccount.h:37
ripple::STAccount::setValue
void setValue(AccountID const &v)
Definition: STAccount.h:109
ripple::SField::fieldType
const SerializedTypeID fieldType
Definition: SField.h:127
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:149
ripple::base_uint< 160, detail::AccountIDTag >
ripple::base_uint::bytes
static constexpr std::size_t bytes
Definition: base_uint.h:96
ripple::STAccount::value_
AccountID value_
Definition: STAccount.h:36
ripple::SerialIter
Definition: Serializer.h:308
ripple::STAccount::add
void add(Serializer &s) const override
Definition: STAccount.h:70
ripple::STAccount::value
AccountID value() const noexcept
Definition: STAccount.h:103
ripple::Serializer
Definition: Serializer.h:39
ripple::STAccount
Definition: STAccount.h:29
ripple::STAccount::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STAccount.h:49
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::STAccount::getText
std::string getText() const override
Definition: STAccount.cpp:63
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::Serializer::addVL
int addVL(Blob const &vector)
Definition: Serializer.cpp:192
ripple::STBase::fName
SField const * fName
Definition: STBase.h:145
std::size_t
ripple::STAccount::operator=
STAccount & operator=(AccountID const &value)
Definition: STAccount.h:96
ripple::AccountID
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:47
string