rippled
STArray.cpp
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 #include <ripple/basics/Log.h>
21 #include <ripple/basics/contract.h>
22 #include <ripple/protocol/STArray.h>
23 #include <ripple/protocol/STBase.h>
24 
25 namespace ripple {
26 
28  : STBase(other.getFName()), v_(std::move(other.v_))
29 {
30 }
31 
32 STArray&
34 {
35  setFName(other.getFName());
36  v_ = std::move(other.v_);
37  return *this;
38 }
39 
41 {
42  v_.reserve(n);
43 }
44 
46 {
47 }
48 
49 STArray::STArray(SField const& f, int n) : STBase(f)
50 {
51  v_.reserve(n);
52 }
53 
54 STArray::STArray(SerialIter& sit, SField const& f, int depth) : STBase(f)
55 {
56  while (!sit.empty())
57  {
58  int type, field;
59  sit.getFieldID(type, field);
60 
61  if ((type == STI_ARRAY) && (field == 1))
62  break;
63 
64  if ((type == STI_OBJECT) && (field == 1))
65  {
66  JLOG(debugLog().error())
67  << "Encountered array with end of object marker";
68  Throw<std::runtime_error>("Illegal terminator in array");
69  }
70 
71  auto const& fn = SField::getField(type, field);
72 
73  if (fn.isInvalid())
74  {
75  JLOG(debugLog().error())
76  << "Unknown field: " << type << "/" << field;
77  Throw<std::runtime_error>("Unknown field");
78  }
79 
80  if (fn.fieldType != STI_OBJECT)
81  {
82  JLOG(debugLog().error()) << "Array contains non-object";
83  Throw<std::runtime_error>("Non-object in array");
84  }
85 
86  v_.emplace_back(sit, fn, depth + 1);
87 
88  v_.back().applyTemplateFromSField(fn); // May throw
89  }
90 }
91 
92 STBase*
93 STArray::copy(std::size_t n, void* buf) const
94 {
95  return emplace(n, buf, *this);
96 }
97 
98 STBase*
100 {
101  return emplace(n, buf, std::move(*this));
102 }
103 
106 {
107  std::string r = "[";
108 
109  bool first = true;
110  for (auto const& obj : v_)
111  {
112  if (!first)
113  r += ",";
114 
115  r += obj.getFullText();
116  first = false;
117  }
118 
119  r += "]";
120  return r;
121 }
122 
125 {
126  std::string r = "[";
127 
128  bool first = true;
129  for (STObject const& o : v_)
130  {
131  if (!first)
132  r += ",";
133 
134  r += o.getText();
135  first = false;
136  }
137 
138  r += "]";
139  return r;
140 }
141 
144 {
146  for (auto const& object : v_)
147  {
148  if (object.getSType() != STI_NOTPRESENT)
149  {
151  inner[object.getFName().getJsonName()] = object.getJson(p);
152  }
153  }
154  return v;
155 }
156 
157 void
159 {
160  for (STObject const& object : v_)
161  {
162  object.addFieldID(s);
163  object.add(s);
164  s.addFieldID(STI_OBJECT, 1);
165  }
166 }
167 
170 {
171  return STI_ARRAY;
172 }
173 
174 bool
176 {
177  auto v = dynamic_cast<const STArray*>(&t);
178  return v != nullptr && v_ == v->v_;
179 }
180 
181 bool
183 {
184  return v_.empty();
185 }
186 
187 void
188 STArray::sort(bool (*compare)(const STObject&, const STObject&))
189 {
190  std::sort(v_.begin(), v_.end(), compare);
191 }
192 
193 } // namespace ripple
std::string
STL class.
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::Serializer::addFieldID
int addFieldID(int type, int name)
Definition: Serializer.cpp:132
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
std::vector::reserve
T reserve(T... args)
ripple::STArray::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STArray.cpp:175
ripple::STArray::getJson
Json::Value getJson(JsonOptions index) const override
Definition: STArray.cpp:143
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::SerialIter::getFieldID
void getFieldID(int &type, int &name)
Definition: Serializer.cpp:414
ripple::STArray::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STArray.cpp:93
ripple::SField::getField
static const SField & getField(int fieldCode)
Definition: SField.cpp:387
ripple::STArray::STArray
STArray()=default
ripple::STI_ARRAY
@ STI_ARRAY
Definition: SField.h:68
std::vector::back
T back(T... args)
std::sort
T sort(T... args)
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
ripple::STArray::getSType
SerializedTypeID getSType() const override
Definition: STArray.cpp:169
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:165
ripple::SerialIter::empty
std::size_t empty() const noexcept
Definition: Serializer.h:332
ripple::STArray::isDefault
bool isDefault() const override
Definition: STArray.cpp:182
ripple::STArray
Definition: STArray.h:28
ripple::STArray::operator=
STArray & operator=(STArray const &)=default
ripple::STBase::setFName
void setFName(SField const &n)
A STBase is a field.
Definition: STBase.cpp:125
ripple::SerialIter
Definition: Serializer.h:310
ripple::STArray::getFullText
std::string getFullText() const override
Definition: STArray.cpp:105
ripple::STArray::sort
void sort(bool(*compare)(const STObject &o1, const STObject &o2))
Definition: STArray.cpp:188
ripple::STArray::getText
std::string getText() const override
Definition: STArray.cpp:124
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:112
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:66
std::vector::begin
T begin(T... args)
std
STL namespace.
std::vector::empty
T empty(T... args)
std::size_t
std::vector::end
T end(T... args)
ripple::STI_OBJECT
@ STI_OBJECT
Definition: SField.h:67
ripple::STArray::move
STBase * move(std::size_t n, void *buf) override
Definition: STArray.cpp:99
ripple::STArray::v_
list_type v_
Definition: STArray.h:33
ripple::STArray::add
void add(Serializer &s) const override
Definition: STArray.cpp:158
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STI_NOTPRESENT
@ STI_NOTPRESENT
Definition: SField.h:55