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