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 
94 {
95  std::string r = "[";
96 
97  bool first = true;
98  for (auto const& obj : v_)
99  {
100  if (!first)
101  r += ",";
102 
103  r += obj.getFullText();
104  first = false;
105  }
106 
107  r += "]";
108  return r;
109 }
110 
113 {
114  std::string r = "[";
115 
116  bool first = true;
117  for (STObject const& o : v_)
118  {
119  if (!first)
120  r += ",";
121 
122  r += o.getText();
123  first = false;
124  }
125 
126  r += "]";
127  return r;
128 }
129 
132 {
134  for (auto const& object : v_)
135  {
136  if (object.getSType() != STI_NOTPRESENT)
137  {
139  inner[object.getFName().getJsonName()] = object.getJson(p);
140  }
141  }
142  return v;
143 }
144 
145 void
147 {
148  for (STObject const& object : v_)
149  {
150  object.addFieldID(s);
151  object.add(s);
152  s.addFieldID(STI_OBJECT, 1);
153  }
154 }
155 
156 bool
158 {
159  auto v = dynamic_cast<const STArray*>(&t);
160  return v != nullptr && v_ == v->v_;
161 }
162 
163 void
164 STArray::sort(bool (*compare)(const STObject&, const STObject&))
165 {
166  std::sort(v_.begin(), v_.end(), compare);
167 }
168 
169 } // 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
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::SerialIter::getFieldID
void getFieldID(int &type, int &name)
Definition: Serializer.cpp:414
ripple::SField::getField
static const SField & getField(int fieldCode)
Definition: SField.cpp:321
ripple::STArray::STArray
STArray()=default
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
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:332
ripple::compare
int compare(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:533
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:183
ripple::STBase::setFName
void setFName(SField const &n)
A STBase is a field.
Definition: STBase.cpp:113
ripple::SerialIter
Definition: Serializer.h:310
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::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
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::v_
list_type v_
Definition: STArray.h:33
ripple::STArray::add
virtual void add(Serializer &s) const override
Definition: STArray.cpp:146
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STI_NOTPRESENT
@ STI_NOTPRESENT
Definition: SField.h:56