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/contract.h>
21 #include <ripple/basics/Log.h>
22 #include <ripple/protocol/STBase.h>
23 #include <ripple/protocol/STArray.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())
37  , v_(std::move(other.v_))
38 {
39 }
40 
42 {
43  setFName(other.getFName());
44  v_ = std::move(other.v_);
45  return *this;
46 }
47 
49 {
50  v_.reserve(n);
51 }
52 
54  : STBase (f)
55 {
57 }
58 
59 STArray::STArray (SField const& f, int n)
60  : STBase (f)
61 {
62  v_.reserve(n);
63 }
64 
65 STArray::STArray (SerialIter& sit, SField const& f, int depth)
66  : STBase(f)
67 {
68  while (!sit.empty ())
69  {
70  int type, field;
71  sit.getFieldID (type, field);
72 
73  if ((type == STI_ARRAY) && (field == 1))
74  break;
75 
76  if ((type == STI_OBJECT) && (field == 1))
77  {
78  JLOG (debugLog().error()) <<
79  "Encountered array with end of object marker";
80  Throw<std::runtime_error> ("Illegal terminator in array");
81  }
82 
83  auto const& fn = SField::getField (type, field);
84 
85  if (fn.isInvalid ())
86  {
87  JLOG (debugLog().error()) <<
88  "Unknown field: " << type << "/" << field;
89  Throw<std::runtime_error> ("Unknown field");
90  }
91 
92  if (fn.fieldType != STI_OBJECT)
93  {
94  JLOG (debugLog().error()) <<
95  "Array contains non-object";
96  Throw<std::runtime_error> ("Non-object in array");
97  }
98 
99  v_.emplace_back(sit, fn, depth+1);
100 
101  v_.back().applyTemplateFromSField (fn); // May throw
102  }
103 }
104 
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 
124 {
125  std::string r = "[";
126 
127  bool first = true;
128  for (STObject const& o : v_)
129  {
130  if (!first)
131  r += ",";
132 
133  r += o.getText ();
134  first = false;
135  }
136 
137  r += "]";
138  return r;
139 }
140 
142 {
144  for (auto const& object: v_)
145  {
146  if (object.getSType () != STI_NOTPRESENT)
147  {
148  Json::Value& inner = v.append (Json::objectValue);
149  inner [object.getFName().getJsonName()] = object.getJson (p);
150  }
151  }
152  return v;
153 }
154 
155 void STArray::add (Serializer& s) const
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 STArray::isEquivalent (const STBase& t) const
166 {
167  auto v = dynamic_cast<const STArray*> (&t);
168  return v != nullptr && v_ == v->v_;
169 }
170 
171 void STArray::sort (bool (*compare) (const STObject&, const STObject&))
172 {
173  std::sort(v_.begin(), v_.end(), compare);
174 }
175 
176 } // ripple
std::string
STL class.
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::Serializer::addFieldID
int addFieldID(int type, int name)
Definition: Serializer.cpp:129
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:44
std::vector::reserve
T reserve(T... args)
ripple::STArray::isEquivalent
virtual bool isEquivalent(const STBase &t) const override
Definition: STArray.cpp:165
ripple::STArray::getJson
virtual Json::Value getJson(JsonOptions index) const override
Definition: STArray.cpp:141
ripple::SerialIter::getFieldID
void getFieldID(int &type, int &name)
Definition: Serializer.cpp:496
ripple::SField::getField
static const SField & getField(int fieldCode)
Definition: SField.cpp:269
ripple::STI_ARRAY
@ STI_ARRAY
Definition: SField.h:70
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:417
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:907
ripple::STArray::STArray
STArray()
Definition: STArray.cpp:27
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:45
ripple::SerialIter::empty
std::size_t empty() const noexcept
Definition: Serializer.h:336
ripple::compare
int compare(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:439
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:164
ripple::STBase::setFName
void setFName(SField const &n)
A STBase is a field.
Definition: STBase.cpp:128
ripple::SerialIter
Definition: Serializer.h:311
ripple::STArray::getFullText
virtual 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:171
ripple::STArray::getText
virtual std::string getText() const override
Definition: STArray.cpp:123
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:112
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:65
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:69
ripple::STArray::reserveSize
@ reserveSize
Definition: STArray.h:37
ripple::STArray::v_
list_type v_
Definition: STArray.h:40
ripple::STArray::add
virtual void add(Serializer &s) const override
Definition: STArray.cpp:155
Json::Value
Represents a JSON value.
Definition: json_value.h:141
ripple::STI_NOTPRESENT
@ STI_NOTPRESENT
Definition: SField.h:57