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() const { return current_->second; }
25 
26 void ValueIteratorBase::increment() { ++current_; }
27 
28 void ValueIteratorBase::decrement() { --current_; }
29 
32 #ifdef JSON_USE_CPPTL_SMALLMAP
33  return other.current_ - current_;
34 #else
35  // Iterator for null value are initialized using the default
36  // constructor, which initialize current_ to the default
37  // std::map::iterator. As begin() and end() are two instance
38  // of the default std::map::iterator, they can not be compared.
39  // To allow this, we handle this comparison specifically.
40  if (isNull_ && other.isNull_) {
41  return 0;
42  }
43 
44  // Usage of std::distance is not portable (does not compile with Sun Studio 12
45  // RogueWave STL,
46  // which is the one used by default).
47  // Using a portable hand-made version for non random iterator instead:
48  // return difference_type( std::distance( current_, other.current_ ) );
49  difference_type myDistance = 0;
50  for (Value::ObjectValues::iterator it = current_; it != other.current_;
51  ++it) {
52  ++myDistance;
53  }
54  return myDistance;
55 #endif
56 }
57 
58 bool ValueIteratorBase::isEqual(const SelfType& other) const {
59  if (isNull_) {
60  return other.isNull_;
61  }
62  return current_ == other.current_;
63 }
64 
65 void ValueIteratorBase::copy(const SelfType& other) {
66  current_ = other.current_;
67  isNull_ = other.isNull_;
68 }
69 
71  const Value::CZString czstring = (*current_).first;
72  if (czstring.data()) {
73  if (czstring.isStaticString())
74  return Value(StaticString(czstring.data()));
75  return Value(czstring.data(), czstring.data() + czstring.length());
76  }
77  return Value(czstring.index());
78 }
79 
81  const Value::CZString czstring = (*current_).first;
82  if (!czstring.data())
83  return czstring.index();
84  return Value::UInt(-1);
85 }
86 
88  char const* keey;
89  char const* end;
90  keey = memberName(&end);
91  if (!keey)
92  return String();
93  return String(keey, end);
94 }
95 
96 char const* ValueIteratorBase::memberName() const {
97  const char* cname = (*current_).first.data();
98  return cname ? cname : "";
99 }
100 
101 char const* ValueIteratorBase::memberName(char const** end) const {
102  const char* cname = (*current_).first.data();
103  if (!cname) {
104  *end = nullptr;
105  return nullptr;
106  }
107  *end = cname + (*current_).first.length();
108  return cname;
109 }
110 
111 // //////////////////////////////////////////////////////////////////
112 // //////////////////////////////////////////////////////////////////
113 // //////////////////////////////////////////////////////////////////
114 // class ValueConstIterator
115 // //////////////////////////////////////////////////////////////////
116 // //////////////////////////////////////////////////////////////////
117 // //////////////////////////////////////////////////////////////////
118 
120 
122  const Value::ObjectValues::iterator& current)
123  : ValueIteratorBase(current) {}
124 
126  : ValueIteratorBase(other) {}
127 
130  copy(other);
131  return *this;
132 }
133 
134 // //////////////////////////////////////////////////////////////////
135 // //////////////////////////////////////////////////////////////////
136 // //////////////////////////////////////////////////////////////////
137 // class ValueIterator
138 // //////////////////////////////////////////////////////////////////
139 // //////////////////////////////////////////////////////////////////
140 // //////////////////////////////////////////////////////////////////
141 
142 ValueIterator::ValueIterator() = default;
143 
144 ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
145  : ValueIteratorBase(current) {}
146 
148  : ValueIteratorBase(other) {
149  throwRuntimeError("ConstIterator to Iterator should never be allowed.");
150 }
151 
152 ValueIterator::ValueIterator(const ValueIterator& other) = default;
153 
155  copy(other);
156  return *this;
157 }
158 
159 } // namespace Json
difference_type computeDistance(const SelfType &other) const
bool isEqual(const SelfType &other) const
base class for Value iterators.
Definition: value.h:739
Lightweight wrapper to tag static string.
Definition: value.h:143
void copy(const SelfType &other)
const iterator for object and array value.
Definition: value.h:806
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
Json::UInt UInt
Definition: value.h:196
SelfType & operator=(const ValueIteratorBase &other)
Represents a JSON value.
Definition: value.h:189
unsigned int UInt
Definition: config.h:134
Iterator for object and array value.
Definition: value.h:857
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:157