rippled
STArray.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_STARRAY_H_INCLUDED
21 #define RIPPLE_PROTOCOL_STARRAY_H_INCLUDED
22 
23 #include <ripple/basics/CountedObject.h>
24 #include <ripple/protocol/STObject.h>
25 
26 namespace ripple {
27 
28 class STArray final : public STBase, public CountedObject<STArray>
29 {
30 private:
32 
34 
35 public:
36  using size_type = list_type::size_type;
37  using iterator = list_type::iterator;
38  using const_iterator = list_type::const_iterator;
39 
40  STArray() = default;
41  STArray(STArray&&);
42  STArray(STArray const&) = default;
43  STArray(SField const& f, int n);
44  STArray(SerialIter& sit, SField const& f, int depth = 0);
45  explicit STArray(int n);
46  explicit STArray(SField const& f);
47  STArray&
48  operator=(STArray const&) = default;
49  STArray&
50  operator=(STArray&&);
51 
52  STBase*
53  copy(std::size_t n, void* buf) const override
54  {
55  return emplace(n, buf, *this);
56  }
57 
58  STBase*
59  move(std::size_t n, void* buf) override
60  {
61  return emplace(n, buf, std::move(*this));
62  }
63 
64  STObject&
66  {
67  return v_[j];
68  }
69 
70  STObject const&
72  {
73  return v_[j];
74  }
75 
76  STObject&
77  back()
78  {
79  return v_.back();
80  }
81 
82  STObject const&
83  back() const
84  {
85  return v_.back();
86  }
87 
88  template <class... Args>
89  void
90  emplace_back(Args&&... args)
91  {
92  v_.emplace_back(std::forward<Args>(args)...);
93  }
94 
95  void
96  push_back(STObject const& object)
97  {
98  v_.push_back(object);
99  }
100 
101  void
102  push_back(STObject&& object)
103  {
104  v_.push_back(std::move(object));
105  }
106 
107  iterator
109  {
110  return v_.begin();
111  }
112 
113  iterator
114  end()
115  {
116  return v_.end();
117  }
118 
120  begin() const
121  {
122  return v_.begin();
123  }
124 
126  end() const
127  {
128  return v_.end();
129  }
130 
131  size_type
132  size() const
133  {
134  return v_.size();
135  }
136 
137  bool
138  empty() const
139  {
140  return v_.empty();
141  }
142  void
144  {
145  v_.clear();
146  }
147  void
149  {
150  v_.reserve(n);
151  }
152  void
153  swap(STArray& a) noexcept
154  {
155  v_.swap(a.v_);
156  }
157 
158  virtual std::string
159  getFullText() const override;
160  virtual std::string
161  getText() const override;
162 
163  virtual Json::Value
164  getJson(JsonOptions index) const override;
165  virtual void
166  add(Serializer& s) const override;
167 
168  void
169  sort(bool (*compare)(const STObject& o1, const STObject& o2));
170 
171  bool
172  operator==(const STArray& s) const
173  {
174  return v_ == s.v_;
175  }
176  bool
177  operator!=(const STArray& s) const
178  {
179  return v_ != s.v_;
180  }
181 
182  virtual SerializedTypeID
183  getSType() const override
184  {
185  return STI_ARRAY;
186  }
187  virtual bool
188  isEquivalent(const STBase& t) const override;
189  virtual bool
190  isDefault() const override
191  {
192  return v_.empty();
193  }
194 };
195 
196 } // namespace ripple
197 
198 #endif
ripple::STArray::empty
bool empty() const
Definition: STArray.h:138
ripple::STArray::operator==
bool operator==(const STArray &s) const
Definition: STArray.h:172
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::STArray::move
STBase * move(std::size_t n, void *buf) override
Definition: STArray.h:59
ripple::Dir::const_iterator
Definition: Directory.h:49
std::string
STL class.
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::STArray::back
STObject const & back() const
Definition: STArray.h:83
ripple::STArray::iterator
list_type::iterator iterator
Definition: STArray.h:37
std::vector::reserve
T reserve(T... args)
std::vector< STObject >
std::vector::size
T size(T... args)
ripple::STArray::isEquivalent
virtual bool isEquivalent(const STBase &t) const override
Definition: STArray.cpp:157
ripple::STArray::getJson
virtual Json::Value getJson(JsonOptions index) const override
Definition: STArray.cpp:131
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STArray::push_back
void push_back(STObject const &object)
Definition: STArray.h:96
ripple::STArray::STArray
STArray()=default
ripple::STI_ARRAY
@ STI_ARRAY
Definition: SField.h:69
std::vector::back
T back(T... args)
ripple::STArray::const_iterator
list_type::const_iterator const_iterator
Definition: STArray.h:38
ripple::STArray::end
iterator end()
Definition: STArray.h:114
std::vector::clear
T clear(T... args)
ripple::STArray::begin
const_iterator begin() const
Definition: STArray.h:120
std::vector::push_back
T push_back(T... args)
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:149
ripple::STArray::begin
iterator begin()
Definition: STArray.h:108
ripple::STArray::clear
void clear()
Definition: STArray.h:143
ripple::STArray::size_type
list_type::size_type size_type
Definition: STArray.h:36
ripple::STArray::push_back
void push_back(STObject &&object)
Definition: STArray.h:102
ripple::compare
int compare(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:530
ripple::STArray::operator!=
bool operator!=(const STArray &s) const
Definition: STArray.h:177
ripple::STArray::isDefault
virtual bool isDefault() const override
Definition: STArray.h:190
ripple::STArray
Definition: STArray.h:28
ripple::STArray::operator=
STArray & operator=(STArray const &)=default
ripple::STArray::swap
void swap(STArray &a) noexcept
Definition: STArray.h:153
ripple::STArray::getSType
virtual SerializedTypeID getSType() const override
Definition: STArray.h:183
ripple::SerialIter
Definition: Serializer.h:310
ripple::STArray::operator[]
STObject const & operator[](std::size_t j) const
Definition: STArray.h:71
ripple::STArray::getFullText
virtual std::string getFullText() const override
Definition: STArray.cpp:93
ripple::STArray::sort
void sort(bool(*compare)(const STObject &o1, const STObject &o2))
Definition: STArray.cpp:164
ripple::STArray::end
const_iterator end() const
Definition: STArray.h:126
ripple::STArray::back
STObject & back()
Definition: STArray.h:77
std::vector::swap
T swap(T... args)
ripple::STArray::getText
virtual std::string getText() const override
Definition: STArray.cpp:112
ripple::Serializer
Definition: Serializer.h:39
ripple::STObject
Definition: STObject.h:51
std::vector::emplace_back
T emplace_back(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
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::STArray::emplace_back
void emplace_back(Args &&... args)
Definition: STArray.h:90
std::vector::begin
T begin(T... args)
ripple::STArray::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STArray.h:53
std::vector::empty
T empty(T... args)
std::size_t
std::vector::end
T end(T... args)
ripple::STArray::size
size_type size() const
Definition: STArray.h:132
ripple::STArray::operator[]
STObject & operator[](std::size_t j)
Definition: STArray.h:65
ripple::STArray::v_
list_type v_
Definition: STArray.h:33
ripple::STArray::add
virtual void add(Serializer &s) const override
Definition: STArray.cpp:146
ripple::STArray::reserve
void reserve(std::size_t n)
Definition: STArray.h:148
Json::Value
Represents a JSON value.
Definition: json_value.h:145