JsonCpp project page Classes Namespace JsonCpp home page

json_valueiterator.inl
Go to the documentation of this file.
1 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
2 // Distributed under MIT license, or public domain if desired and
3 // recognized in your jurisdiction.
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5 
6 // included by json_value.cpp
7 
8 namespace Json {
9 
10 // //////////////////////////////////////////////////////////////////
11 // //////////////////////////////////////////////////////////////////
12 // //////////////////////////////////////////////////////////////////
13 // class ValueIteratorBase
14 // //////////////////////////////////////////////////////////////////
15 // //////////////////////////////////////////////////////////////////
16 // //////////////////////////////////////////////////////////////////
17 
19 
21  const Value::ObjectValues::iterator& current)
22  : current_(current), isNull_(false) {}
23 
24 Value& ValueIteratorBase::deref() { return current_->second; }
25 const Value& ValueIteratorBase::deref() const { return current_->second; }
26 
27 void ValueIteratorBase::increment() { ++current_; }
28 
29 void ValueIteratorBase::decrement() { --current_; }
30 
33  // Iterator for null value are initialized using the default
34  // constructor, which initialize current_ to the default
35  // std::map::iterator. As begin() and end() are two instance
36  // of the default std::map::iterator, they can not be compared.
37  // To allow this, we handle this comparison specifically.
38  if (isNull_ && other.isNull_) {
39  return 0;
40  }
41 
42  // Usage of std::distance is not portable (does not compile with Sun Studio 12
43  // RogueWave STL,
44  // which is the one used by default).
45  // Using a portable hand-made version for non random iterator instead:
46  // return difference_type( std::distance( current_, other.current_ ) );
47  difference_type myDistance = 0;
48  for (Value::ObjectValues::iterator it = current_; it != other.current_;
49  ++it) {
50  ++myDistance;
51  }
52  return myDistance;
53 }
54 
55 bool ValueIteratorBase::isEqual(const SelfType& other) const {
56  if (isNull_) {
57  return other.isNull_;
58  }
59  return current_ == other.current_;
60 }
61 
62 void ValueIteratorBase::copy(const SelfType& other) {
63  current_ = other.current_;
64  isNull_ = other.isNull_;
65 }
66 
68  const Value::CZString czstring = (*current_).first;
69  if (czstring.data()) {
70  if (czstring.isStaticString())
71  return Value(StaticString(czstring.data()));
72  return Value(czstring.data(), czstring.data() + czstring.length());
73  }
74  return Value(czstring.index());
75 }
76 
78  const Value::CZString czstring = (*current_).first;
79  if (!czstring.data())
80  return czstring.index();
81  return Value::UInt(-1);
82 }
83 
85  char const* keey;
86  char const* end;
87  keey = memberName(&end);
88  if (!keey)
89  return String();
90  return String(keey, end);
91 }
92 
93 char const* ValueIteratorBase::memberName() const {
94  const char* cname = (*current_).first.data();
95  return cname ? cname : "";
96 }
97 
98 char const* ValueIteratorBase::memberName(char const** end) const {
99  const char* cname = (*current_).first.data();
100  if (!cname) {
101  *end = nullptr;
102  return nullptr;
103  }
104  *end = cname + (*current_).first.length();
105  return cname;
106 }
107 
108 // //////////////////////////////////////////////////////////////////
109 // //////////////////////////////////////////////////////////////////
110 // //////////////////////////////////////////////////////////////////
111 // class ValueConstIterator
112 // //////////////////////////////////////////////////////////////////
113 // //////////////////////////////////////////////////////////////////
114 // //////////////////////////////////////////////////////////////////
115 
117 
119  const Value::ObjectValues::iterator& current)
120  : ValueIteratorBase(current) {}
121 
123  : ValueIteratorBase(other) {}
124 
127  copy(other);
128  return *this;
129 }
130 
131 // //////////////////////////////////////////////////////////////////
132 // //////////////////////////////////////////////////////////////////
133 // //////////////////////////////////////////////////////////////////
134 // class ValueIterator
135 // //////////////////////////////////////////////////////////////////
136 // //////////////////////////////////////////////////////////////////
137 // //////////////////////////////////////////////////////////////////
138 
139 ValueIterator::ValueIterator() = default;
140 
141 ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
142  : ValueIteratorBase(current) {}
143 
145  : ValueIteratorBase(other) {
146  throwRuntimeError("ConstIterator to Iterator should never be allowed.");
147 }
148 
149 ValueIterator::ValueIterator(const ValueIterator& other) = default;
150 
152  copy(other);
153  return *this;
154 }
155 
156 } // namespace Json
difference_type computeDistance(const SelfType &other) const
bool isEqual(const SelfType &other) const
base class for Value iterators.
Definition: value.h:745
const Value & deref() const
Json::UInt UInt
Definition: value.h:200
Lightweight wrapper to tag static string.
Definition: value.h:147
void copy(const SelfType &other)
const iterator for object and array value.
Definition: value.h:819
UInt index() const
Return the index of the referenced Value, or -1 if it is not an arrayValue.
Value key() const
Return either the index or the member name of the referenced value as a Value.
SelfType & operator=(const SelfType &other)
JSON (JavaScript Object Notation).
Definition: allocator.h:14
SelfType & operator=(const ValueIteratorBase &other)
unsigned int UInt
Definition: config.h:109
Represents a JSON value.
Definition: value.h:193
Iterator for object and array value.
Definition: value.h:870
char const * memberName(char const **end) const
Return the member name of the referenced Value, or NULL if it is not an objectValue.
String name() const
Return the member name of the referenced Value, or "" if it is not an objectValue.
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition: config.h:132