rippled
json_valueiterator.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 // included by json_value.cpp
21 
22 #include <ripple/json/json_value.h>
23 
24 namespace Json {
25 
26 // //////////////////////////////////////////////////////////////////
27 // //////////////////////////////////////////////////////////////////
28 // //////////////////////////////////////////////////////////////////
29 // class ValueIteratorBase
30 // //////////////////////////////////////////////////////////////////
31 // //////////////////////////////////////////////////////////////////
32 // //////////////////////////////////////////////////////////////////
33 
35  : current_ ()
36  , isNull_ ( true )
37 {
38 }
39 
40 ValueIteratorBase::ValueIteratorBase ( const Value::ObjectValues::iterator& current )
41  : current_ ( current )
42  , isNull_ ( false )
43 {
44 }
45 
46 Value&
48 {
49  return current_->second;
50 }
51 
52 
53 void
55 {
56  ++current_;
57 }
58 
59 
60 void
62 {
63  --current_;
64 }
65 
66 
69 {
70  // Iterator for null value are initialized using the default
71  // constructor, which initialize current_ to the default
72  // std::map::iterator. As begin() and end() are two instance
73  // of the default std::map::iterator, they can not be compared.
74  // To allow this, we handle this comparison specifically.
75  if ( isNull_ && other.isNull_ )
76  {
77  return 0;
78  }
79 
80 
81  // Usage of std::distance is not portable (does not compile with Sun Studio 12 RogueWave STL,
82  // which is the one used by default).
83  // Using a portable hand-made version for non random iterator instead:
84  // return difference_type( std::distance( current_, other.current_ ) );
85  difference_type myDistance = 0;
86 
87  for ( Value::ObjectValues::iterator it = current_; it != other.current_; ++it )
88  {
89  ++myDistance;
90  }
91 
92  return myDistance;
93 }
94 
95 
96 bool
97 ValueIteratorBase::isEqual ( const SelfType& other ) const
98 {
99  if ( isNull_ )
100  {
101  return other.isNull_;
102  }
103 
104  return current_ == other.current_;
105 }
106 
107 
108 void
110 {
111  current_ = other.current_;
112 }
113 
114 
115 Value
117 {
118  const Value::CZString czstring = (*current_).first;
119 
120  if ( czstring.c_str () )
121  {
122  if ( czstring.isStaticString () )
123  return Value ( StaticString ( czstring.c_str () ) );
124 
125  return Value ( czstring.c_str () );
126  }
127 
128  return Value ( czstring.index () );
129 }
130 
131 
132 UInt
134 {
135  const Value::CZString czstring = (*current_).first;
136 
137  if ( !czstring.c_str () )
138  return czstring.index ();
139 
140  return Value::UInt ( -1 );
141 }
142 
143 
144 const char*
146 {
147  const char* name = (*current_).first.c_str ();
148  return name ? name : "";
149 }
150 
151 
152 // //////////////////////////////////////////////////////////////////
153 // //////////////////////////////////////////////////////////////////
154 // //////////////////////////////////////////////////////////////////
155 // class ValueConstIterator
156 // //////////////////////////////////////////////////////////////////
157 // //////////////////////////////////////////////////////////////////
158 // //////////////////////////////////////////////////////////////////
159 
160 
161 ValueConstIterator::ValueConstIterator ( const Value::ObjectValues::iterator& current )
162  : ValueIteratorBase ( current )
163 {
164 }
165 
168 {
169  copy ( other );
170  return *this;
171 }
172 
173 
174 // //////////////////////////////////////////////////////////////////
175 // //////////////////////////////////////////////////////////////////
176 // //////////////////////////////////////////////////////////////////
177 // class ValueIterator
178 // //////////////////////////////////////////////////////////////////
179 // //////////////////////////////////////////////////////////////////
180 // //////////////////////////////////////////////////////////////////
181 
182 
183 ValueIterator::ValueIterator ( const Value::ObjectValues::iterator& current )
184  : ValueIteratorBase ( current )
185 {
186 }
187 
189  : ValueIteratorBase ( other )
190 {
191 }
192 
194  : ValueIteratorBase ( other )
195 {
196 }
197 
200 {
201  copy ( other );
202  return *this;
203 }
204 
205 } // Json
Json::ValueIterator
Iterator for object and array value.
Definition: json_value.h:530
Json::ValueIteratorBase::copy
void copy(const SelfType &other)
Definition: json_valueiterator.cpp:109
Json::Value::CZString::index
int index() const
Definition: json_value.cpp:145
Json::ValueConstIterator::operator=
SelfType & operator=(const ValueIteratorBase &other)
Definition: json_valueiterator.cpp:167
Json::UInt
unsigned int UInt
Definition: json_forwards.h:28
Json::ValueIteratorBase::ValueIteratorBase
ValueIteratorBase()
Definition: json_valueiterator.cpp:34
Json::Value::CZString::c_str
const char * c_str() const
Definition: json_value.cpp:152
Json::ValueIteratorBase::index
UInt index() const
Return the index of the referenced Value. -1 if it is not an arrayValue.
Definition: json_valueiterator.cpp:133
Json::ValueIteratorBase::decrement
void decrement()
Definition: json_valueiterator.cpp:61
Json::ValueIteratorBase::isEqual
bool isEqual(const SelfType &other) const
Definition: json_valueiterator.cpp:97
Json::ValueIteratorBase::key
Value key() const
Return either the index or the member name of the referenced value as a Value.
Definition: json_valueiterator.cpp:116
Json::ValueIteratorBase::current_
Value::ObjectValues::iterator current_
Definition: json_value.h:469
Json::ValueIteratorBase::isNull_
bool isNull_
Definition: json_value.h:471
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:26
Json::ValueConstIterator
const iterator for object and array value.
Definition: json_value.h:477
Json::ValueIteratorBase
base class for Value iterators.
Definition: json_value.h:425
Json::ValueIterator::ValueIterator
ValueIterator()=default
Json::Value::UInt
Json::UInt UInt
Definition: json_value.h:149
Json::ValueIteratorBase::computeDistance
difference_type computeDistance(const SelfType &other) const
Definition: json_valueiterator.cpp:68
Json::ValueIterator::operator=
SelfType & operator=(const SelfType &other)
Definition: json_valueiterator.cpp:199
Json::Value::CZString
Definition: json_value.h:159
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:62
Json::ValueIteratorBase::increment
void increment()
Definition: json_valueiterator.cpp:54
Json::Value::CZString::isStaticString
bool isStaticString() const
Definition: json_value.cpp:158
Json::ValueIteratorBase::difference_type
int difference_type
Definition: json_value.h:429
Json::Value
Represents a JSON value.
Definition: json_value.h:141
Json::ValueIteratorBase::deref
Value & deref() const
Definition: json_valueiterator.cpp:47
Json::ValueConstIterator::ValueConstIterator
ValueConstIterator()=default
Json::ValueIteratorBase::memberName
const char * memberName() const
Return the member name of the referenced Value. "" if it is not an objectValue.
Definition: json_valueiterator.cpp:145