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