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
18 ValueIteratorBase::ValueIteratorBase() : current_() {}
19
20 ValueIteratorBase::ValueIteratorBase(
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
31 ValueIteratorBase::difference_type
32 ValueIteratorBase::computeDistance(const SelfType& other) const {
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
67 Value ValueIteratorBase::key() const {
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
77 UInt ValueIteratorBase::index() const {
78 const Value::CZString czstring = (*current_).first;
79 if (!czstring.data())
80 return czstring.index();
81 return Value::UInt(-1);
82 }
83
84 String ValueIteratorBase::name() const {
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
116 ValueConstIterator::ValueConstIterator() = default;
117
118 ValueConstIterator::ValueConstIterator(
119 const Value::ObjectValues::iterator& current)
120 : ValueIteratorBase(current) {}
121
122 ValueConstIterator::ValueConstIterator(ValueIterator const& other)
123 : ValueIteratorBase(other) {}
124
125 ValueConstIterator& ValueConstIterator::
126 operator=(const ValueIteratorBase& other) {
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
144 ValueIterator::ValueIterator(const ValueConstIterator& other)
145 : ValueIteratorBase(other) {
146 throwRuntimeError("ConstIterator to Iterator should never be allowed.");
147 }
148
149 ValueIterator::ValueIterator(const ValueIterator& other) = default;
150
151 ValueIterator& ValueIterator::operator=(const SelfType& other) {
152 copy(other);
153 return *this;
154 }
155
156 } // namespace Json
Json::ValueConstIterator::operator=
SelfType & operator=(const ValueIteratorBase &other)
Definition: json_valueiterator.inl:126
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:93
Json::Value::UInt
Json::UInt UInt
Definition: value.h:200
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:84
Json::ValueConstIterator
const iterator for object and array value.
Definition: value.h:820
Json::ValueIterator
Iterator for object and array value.
Definition: value.h:871
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::ValueIteratorBase::copy
void copy(const SelfType &other)
Definition: json_valueiterator.inl:62
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:67
Json::ValueConstIterator::ValueConstIterator
ValueConstIterator()
Json::UInt
unsigned int UInt
Definition: config.h:109
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:77
Json::Value
Represents a JSON value.
Definition: value.h:193
Json::ValueIterator::operator=
SelfType & operator=(const SelfType &other)
Definition: json_valueiterator.inl:151
Json::ValueIterator::ValueIterator
ValueIterator()
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::ValueIteratorBase
base class for Value iterators.
Definition: value.h:746
Json::ValueIteratorBase::difference_type
int difference_type
Definition: value.h:750
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:132
Json::ValueIteratorBase::isEqual
bool isEqual(const SelfType &other) const
Definition: json_valueiterator.inl:55
Json::ValueIteratorBase::increment
void increment()
Definition: json_valueiterator.inl:27
Json::StaticString
Lightweight wrapper to tag static string.
Definition: value.h:147
Json::ValueIteratorBase::decrement
void decrement()
Definition: json_valueiterator.inl:29