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