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 {
29  // VFALCO NOTE We need to determine if this is
30  // the right thing to do, and consider
31  // making it optional.
32  // v_.reserve(reserveSize);
33 }
34 
36  : STBase(other.getFName()), v_(std::move(other.v_))
37 {
38 }
39 
40 STArray&
42 {
43  setFName(other.getFName());
44  v_ = std::move(other.v_);
45  return *this;
46 }
47 
49 {
50  v_.reserve(n);
51 }
52 
54 {
56 }
57 
58 STArray::STArray(SField const& f, int n) : STBase(f)
59 {
60  v_.reserve(n);
61 }
62 
63 STArray::STArray(SerialIter& sit, SField const& f, int depth) : STBase(f)
64 {
65  while (!sit.empty())
66  {
67  int type, field;
68  sit.getFieldID(type, field);
69 
70  if ((type == STI_ARRAY) && (field == 1))
71  break;
72 
73  if ((type == STI_OBJECT) && (field == 1))
74  {
75  JLOG(debugLog().error())
76  << "Encountered array with end of object marker";
77  Throw<std::runtime_error>("Illegal terminator in array");
78  }
79 
80  auto const& fn = SField::getField(type, field);
81 
82  if (fn.isInvalid())
83  {
84  JLOG(debugLog().error())
85  << "Unknown field: " << type << "/" << field;
86  Throw<std::runtime_error>("Unknown field");
87  }
88 
89  if (fn.fieldType != STI_OBJECT)
90  {
91  JLOG(debugLog().error()) << "Array contains non-object";
92  Throw<std::runtime_error>("Non-object in array");
93  }
94 
95  v_.emplace_back(sit, fn, depth + 1);
96 
97  v_.back().applyTemplateFromSField(fn); // May throw
98  }
99 }
100 
103 {
104  std::string r = "[";
105 
106  bool first = true;
107  for (auto const& obj : v_)
108  {
109  if (!first)
110  r += ",";
111 
112  r += obj.getFullText();
113  first = false;
114  }
115 
116  r += "]";
117  return r;
118 }
119 
122 {
123  std::string r = "[";
124 
125  bool first = true;
126  for (STObject const& o : v_)
127  {
128  if (!first)
129  r += ",";
130 
131  r += o.getText();
132  first = false;
133  }
134 
135  r += "]";
136  return r;
137 }
138 
141 {
143  for (auto const& object : v_)
144  {
145  if (object.getSType() != STI_NOTPRESENT)
146  {
148  inner[object.getFName().getJsonName()] = object.getJson(p);
149  }
150  }
151  return v;
152 }
153 
154 void
156 {
157  for (STObject const& object : v_)
158  {
159  object.addFieldID(s);
160  object.add(s);
161  s.addFieldID(STI_OBJECT, 1);
162  }
163 }
164 
165 bool
167 {
168  auto v = dynamic_cast<const STArray*>(&t);
169  return v != nullptr && v_ == v->v_;
170 }
171 
172 void
173 STArray::sort(bool (*compare)(const STObject&, const STObject&))
174 {
175  std::sort(v_.begin(), v_.end(), compare);
176 }
177 
178 } // 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:161
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
std::vector::reserve
T reserve(T... args)
ripple::STArray::isEquivalent
virtual bool isEquivalent(const STBase &t) const override
Definition: STArray.cpp:166
ripple::STArray::getJson
virtual Json::Value getJson(JsonOptions index) const override
Definition: STArray.cpp:140
ripple::SerialIter::getFieldID
void getFieldID(int &type, int &name)
Definition: Serializer.cpp:558
ripple::SField::getField
static const SField & getField(int fieldCode)
Definition: SField.cpp:318
ripple::STI_ARRAY
@ STI_ARRAY
Definition: SField.h:69
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
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
ripple::STArray::STArray
STArray()
Definition: STArray.cpp:27
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::SerialIter::empty
std::size_t empty() const noexcept
Definition: Serializer.h:390
ripple::compare
int compare(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:497
ripple::STArray
Definition: STArray.h:28
ripple::STArray::operator=
STArray & operator=(STArray const &)=default
ripple::STArray::getSType
virtual SerializedTypeID getSType() const override
Definition: STArray.h:194
ripple::STBase::setFName
void setFName(SField const &n)
A STBase is a field.
Definition: STBase.cpp:128
ripple::SerialIter
Definition: Serializer.h:368
ripple::STArray::getFullText
virtual std::string getFullText() const override
Definition: STArray.cpp:102
ripple::STArray::sort
void sort(bool(*compare)(const STObject &o1, const STObject &o2))
Definition: STArray.cpp:173
ripple::STArray::getText
virtual std::string getText() const override
Definition: STArray.cpp:121
ripple::Serializer
Definition: Serializer.h:43
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
std::vector::begin
T begin(T... args)
std
STL namespace.
std::vector::end
T end(T... args)
ripple::STI_OBJECT
@ STI_OBJECT
Definition: SField.h:68
ripple::STArray::reserveSize
@ reserveSize
Definition: STArray.h:33
ripple::STArray::v_
list_type v_
Definition: STArray.h:35
ripple::STArray::add
virtual void add(Serializer &s) const override
Definition: STArray.cpp:155
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STI_NOTPRESENT
@ STI_NOTPRESENT
Definition: SField.h:56