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  : current_(), isNull_(true) {
20 }
21 
23  const Value::ObjectValues::iterator& current)
24  : current_(current), isNull_(false) {}
25 
27  return current_->second;
28 }
29 
31  ++current_;
32 }
33 
35  --current_;
36 }
37 
40 #ifdef JSON_USE_CPPTL_SMALLMAP
41  return other.current_ - current_;
42 #else
43  // Iterator for null value are initialized using the default
44  // constructor, which initialize current_ to the default
45  // std::map::iterator. As begin() and end() are two instance
46  // of the default std::map::iterator, they can not be compared.
47  // To allow this, we handle this comparison specifically.
48  if (isNull_ && other.isNull_) {
49  return 0;
50  }
51 
52  // Usage of std::distance is not portable (does not compile with Sun Studio 12
53  // RogueWave STL,
54  // which is the one used by default).
55  // Using a portable hand-made version for non random iterator instead:
56  // return difference_type( std::distance( current_, other.current_ ) );
57  difference_type myDistance = 0;
58  for (Value::ObjectValues::iterator it = current_; it != other.current_;
59  ++it) {
60  ++myDistance;
61  }
62  return myDistance;
63 #endif
64 }
65 
66 bool ValueIteratorBase::isEqual(const SelfType& other) const {
67  if (isNull_) {
68  return other.isNull_;
69  }
70  return current_ == other.current_;
71 }
72 
73 void ValueIteratorBase::copy(const SelfType& other) {
74  current_ = other.current_;
75  isNull_ = other.isNull_;
76 }
77 
79  const Value::CZString czstring = (*current_).first;
80  if (czstring.data()) {
81  if (czstring.isStaticString())
82  return Value(StaticString(czstring.data()));
83  return Value(czstring.data(), czstring.data() + czstring.length());
84  }
85  return Value(czstring.index());
86 }
87 
89  const Value::CZString czstring = (*current_).first;
90  if (!czstring.data())
91  return czstring.index();
92  return Value::UInt(-1);
93 }
94 
96  char const* keey;
97  char const* end;
98  keey = memberName(&end);
99  if (!keey) return JSONCPP_STRING();
100  return JSONCPP_STRING(keey, end);
101 }
102 
103 char const* ValueIteratorBase::memberName() const {
104  const char* cname = (*current_).first.data();
105  return cname ? cname : "";
106 }
107 
108 char const* ValueIteratorBase::memberName(char const** end) const {
109  const char* cname = (*current_).first.data();
110  if (!cname) {
111  *end = NULL;
112  return NULL;
113  }
114  *end = cname + (*current_).first.length();
115  return cname;
116 }
117 
118 // //////////////////////////////////////////////////////////////////
119 // //////////////////////////////////////////////////////////////////
120 // //////////////////////////////////////////////////////////////////
121 // class ValueConstIterator
122 // //////////////////////////////////////////////////////////////////
123 // //////////////////////////////////////////////////////////////////
124 // //////////////////////////////////////////////////////////////////
125 
127 
129  const Value::ObjectValues::iterator& current)
130  : ValueIteratorBase(current) {}
131 
133  : ValueIteratorBase(other) {}
134 
137  copy(other);
138  return *this;
139 }
140 
141 // //////////////////////////////////////////////////////////////////
142 // //////////////////////////////////////////////////////////////////
143 // //////////////////////////////////////////////////////////////////
144 // class ValueIterator
145 // //////////////////////////////////////////////////////////////////
146 // //////////////////////////////////////////////////////////////////
147 // //////////////////////////////////////////////////////////////////
148 
150 
151 ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
152  : ValueIteratorBase(current) {}
153 
155  : ValueIteratorBase(other) {
156  throwRuntimeError("ConstIterator to Iterator should never be allowed.");
157 }
158 
160  : ValueIteratorBase(other) {}
161 
163  copy(other);
164  return *this;
165 }
166 
167 } // namespace Json
difference_type computeDistance(const SelfType &other) const
bool isEqual(const SelfType &other) const
base class for Value iterators.
Definition: value.h:709
#define JSONCPP_STRING
Definition: config.h:179
Lightweight wrapper to tag static string.
Definition: value.h:131
void copy(const SelfType &other)
const iterator for object and array value.
Definition: value.h:774
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.
std::string name() const
Return the member name of the referenced Value, or "" if it is not an objectValue.
SelfType & operator=(const SelfType &other)
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::UInt UInt
Definition: value.h:183
SelfType & operator=(const ValueIteratorBase &other)
Represents a JSON value.
Definition: value.h:177
unsigned int UInt
Definition: config.h:154
Iterator for object and array value.
Definition: value.h:824
char const * memberName() const
Return the member name of the referenced Value.