Line data Source code
1 : // Copyright 2007-2010 Baptiste Lepilleur
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 0 : ValueIteratorBase::ValueIteratorBase()
19 0 : : current_(), isNull_(true) {
20 0 : }
21 :
22 0 : ValueIteratorBase::ValueIteratorBase(
23 0 : const Value::ObjectValues::iterator& current)
24 0 : : current_(current), isNull_(false) {}
25 :
26 0 : Value& ValueIteratorBase::deref() const {
27 0 : return current_->second;
28 : }
29 :
30 0 : void ValueIteratorBase::increment() {
31 0 : ++current_;
32 0 : }
33 :
34 0 : void ValueIteratorBase::decrement() {
35 0 : --current_;
36 0 : }
37 :
38 : ValueIteratorBase::difference_type
39 0 : ValueIteratorBase::computeDistance(const SelfType& other) const {
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 0 : if (isNull_ && other.isNull_) {
49 0 : 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 0 : difference_type myDistance = 0;
58 0 : for (Value::ObjectValues::iterator it = current_; it != other.current_;
59 : ++it) {
60 0 : ++myDistance;
61 : }
62 0 : return myDistance;
63 : #endif
64 : }
65 :
66 0 : bool ValueIteratorBase::isEqual(const SelfType& other) const {
67 0 : if (isNull_) {
68 0 : return other.isNull_;
69 : }
70 0 : return current_ == other.current_;
71 : }
72 :
73 0 : void ValueIteratorBase::copy(const SelfType& other) {
74 0 : current_ = other.current_;
75 0 : isNull_ = other.isNull_;
76 0 : }
77 :
78 0 : Value ValueIteratorBase::key() const {
79 0 : const Value::CZString czstring = (*current_).first;
80 0 : if (czstring.data()) {
81 0 : if (czstring.isStaticString())
82 0 : return Value(StaticString(czstring.data()));
83 0 : return Value(czstring.data(), czstring.data() + czstring.length());
84 : }
85 0 : return Value(czstring.index());
86 : }
87 :
88 0 : UInt ValueIteratorBase::index() const {
89 0 : const Value::CZString czstring = (*current_).first;
90 0 : if (!czstring.data())
91 0 : return czstring.index();
92 0 : return Value::UInt(-1);
93 : }
94 :
95 0 : JSONCPP_STRING ValueIteratorBase::name() const {
96 : char const* keey;
97 : char const* end;
98 0 : keey = memberName(&end);
99 0 : if (!keey) return JSONCPP_STRING();
100 0 : return JSONCPP_STRING(keey, end);
101 : }
102 :
103 0 : char const* ValueIteratorBase::memberName() const {
104 0 : const char* cname = (*current_).first.data();
105 0 : return cname ? cname : "";
106 : }
107 :
108 0 : char const* ValueIteratorBase::memberName(char const** end) const {
109 0 : const char* cname = (*current_).first.data();
110 0 : if (!cname) {
111 0 : *end = NULL;
112 0 : return NULL;
113 : }
114 0 : *end = cname + (*current_).first.length();
115 0 : return cname;
116 : }
117 :
118 : // //////////////////////////////////////////////////////////////////
119 : // //////////////////////////////////////////////////////////////////
120 : // //////////////////////////////////////////////////////////////////
121 : // class ValueConstIterator
122 : // //////////////////////////////////////////////////////////////////
123 : // //////////////////////////////////////////////////////////////////
124 : // //////////////////////////////////////////////////////////////////
125 :
126 0 : ValueConstIterator::ValueConstIterator() {}
127 :
128 0 : ValueConstIterator::ValueConstIterator(
129 0 : const Value::ObjectValues::iterator& current)
130 0 : : ValueIteratorBase(current) {}
131 :
132 0 : ValueConstIterator::ValueConstIterator(ValueIterator const& other)
133 0 : : ValueIteratorBase(other) {}
134 :
135 0 : ValueConstIterator& ValueConstIterator::
136 : operator=(const ValueIteratorBase& other) {
137 0 : copy(other);
138 0 : return *this;
139 : }
140 :
141 : // //////////////////////////////////////////////////////////////////
142 : // //////////////////////////////////////////////////////////////////
143 : // //////////////////////////////////////////////////////////////////
144 : // class ValueIterator
145 : // //////////////////////////////////////////////////////////////////
146 : // //////////////////////////////////////////////////////////////////
147 : // //////////////////////////////////////////////////////////////////
148 :
149 0 : ValueIterator::ValueIterator() {}
150 :
151 0 : ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
152 0 : : ValueIteratorBase(current) {}
153 :
154 0 : ValueIterator::ValueIterator(const ValueConstIterator& other)
155 0 : : ValueIteratorBase(other) {
156 0 : throwRuntimeError("ConstIterator to Iterator should never be allowed.");
157 : }
158 :
159 0 : ValueIterator::ValueIterator(const ValueIterator& other)
160 0 : : ValueIteratorBase(other) {}
161 :
162 0 : ValueIterator& ValueIterator::operator=(const SelfType& other) {
163 0 : copy(other);
164 0 : return *this;
165 : }
166 :
167 : } // namespace Json
|