NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
json_valueiterator.inl
浏览该文件的文档.
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
8namespace nim_cpp_wrapper_util {
9namespace Json {
10
11// //////////////////////////////////////////////////////////////////
12// //////////////////////////////////////////////////////////////////
13// //////////////////////////////////////////////////////////////////
14// class ValueIteratorBase
15// //////////////////////////////////////////////////////////////////
16// //////////////////////////////////////////////////////////////////
17// //////////////////////////////////////////////////////////////////
18
21
22ValueIteratorBase::ValueIteratorBase(const Value::ObjectValues::iterator& current)
23 : current_(current)
24 , isNull_(false) {}
25
27 return current_->second;
28}
30 return current_->second;
31}
32
36
40
42 // Iterator for null value are initialized using the default
43 // constructor, which initialize current_ to the default
44 // std::map::iterator. As begin() and end() are two instance
45 // of the default std::map::iterator, they can not be compared.
46 // To allow this, we handle this comparison specifically.
47 if (isNull_ && other.isNull_) {
48 return 0;
49 }
50
51 // Usage of std::distance is not portable (does not compile with Sun Studio 12
52 // RogueWave STL,
53 // which is the one used by default).
54 // Using a portable hand-made version for non random iterator instead:
55 // return difference_type( std::distance( current_, other.current_ ) );
56 difference_type myDistance = 0;
57 for (Value::ObjectValues::iterator it = current_; it != other.current_; ++it) {
58 ++myDistance;
59 }
60 return myDistance;
61}
62
63bool ValueIteratorBase::isEqual(const SelfType& other) const {
64 if (isNull_) {
65 return other.isNull_;
66 }
67 return current_ == other.current_;
68}
69
71 current_ = other.current_;
72 isNull_ = other.isNull_;
73}
74
76 const Value::CZString czstring = (*current_).first;
77 if (czstring.data()) {
78 if (czstring.isStaticString())
79 return Value(StaticString(czstring.data()));
80 return Value(czstring.data(), czstring.data() + czstring.length());
81 }
82 return Value(czstring.index());
83}
84
86 const Value::CZString czstring = (*current_).first;
87 if (!czstring.data())
88 return czstring.index();
89 return Value::UInt(-1);
90}
91
93 char const* keey;
94 char const* end;
95 keey = memberName(&end);
96 if (!keey)
97 return String();
98 return String(keey, end);
99}
100
101char const* ValueIteratorBase::memberName() const {
102 const char* cname = (*current_).first.data();
103 return cname ? cname : "";
104}
105
106char const* ValueIteratorBase::memberName(char const** end) const {
107 const char* cname = (*current_).first.data();
108 if (!cname) {
109 *end = nullptr;
110 return nullptr;
111 }
112 *end = cname + (*current_).first.length();
113 return cname;
114}
115
116// //////////////////////////////////////////////////////////////////
117// //////////////////////////////////////////////////////////////////
118// //////////////////////////////////////////////////////////////////
119// class ValueConstIterator
120// //////////////////////////////////////////////////////////////////
121// //////////////////////////////////////////////////////////////////
122// //////////////////////////////////////////////////////////////////
123
125
126ValueConstIterator::ValueConstIterator(const Value::ObjectValues::iterator& current)
127 : ValueIteratorBase(current) {}
128
131
133 copy(other);
134 return *this;
135}
136
137// //////////////////////////////////////////////////////////////////
138// //////////////////////////////////////////////////////////////////
139// //////////////////////////////////////////////////////////////////
140// class ValueIterator
141// //////////////////////////////////////////////////////////////////
142// //////////////////////////////////////////////////////////////////
143// //////////////////////////////////////////////////////////////////
144
146
147ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
148 : ValueIteratorBase(current) {}
149
151 : ValueIteratorBase(other) {
152 throwRuntimeError("ConstIterator to Iterator should never be allowed.");
153}
154
155ValueIterator::ValueIterator(const ValueIterator& other) = default;
156
158 copy(other);
159 return *this;
160}
161
162} // namespace Json
163} // namespace nim_cpp_wrapper_util
Lightweight wrapper to tag static string.
Definition value.h:149
ArrayIndex index() const
Definition json_value.cpp:330
bool isStaticString() const
Definition json_value.cpp:341
char const * data() const
Definition json_value.cpp:335
unsigned length() const
Definition json_value.cpp:338
const iterator for object and array value.
Definition value.h:869
SelfType & operator=(const ValueIteratorBase &other)
Definition json_valueiterator.inl:132
Represents a JSON value.
Definition value.h:196
nim_cpp_wrapper_util::Json::UInt UInt
Definition value.h:203
base class for Value iterators.
Definition value.h:798
ValueIteratorBase()
Definition json_valueiterator.inl:19
void decrement()
Definition json_valueiterator.inl:37
Value::ObjectValues::iterator current_
Definition value.h:855
void increment()
Definition json_valueiterator.inl:33
bool isEqual(const SelfType &other) const
Definition json_valueiterator.inl:63
Value key() const
Definition json_valueiterator.inl:75
char const * memberName() const
Definition json_valueiterator.inl:101
String name() const
Definition json_valueiterator.inl:92
const Value & deref() const
Definition json_valueiterator.inl:29
UInt index() const
Definition json_valueiterator.inl:85
void copy(const SelfType &other)
Definition json_valueiterator.inl:70
difference_type computeDistance(const SelfType &other) const
Definition json_valueiterator.inl:41
Iterator for object and array value.
Definition value.h:920
SelfType & operator=(const SelfType &other)
Definition json_valueiterator.inl:157
JSONCPP_NORETURN void throwRuntimeError(String const &msg)
used internally
Definition json_value.cpp:223
unsigned int UInt
Definition config.h:89
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition config.h:110
JSON (JavaScript Object Notation).
Definition allocator.h:14
#define false
Definition stdbool.h:33