1e5c31af7Sopenharmony_ci// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors 2e5c31af7Sopenharmony_ci// Distributed under MIT license, or public domain if desired and 3e5c31af7Sopenharmony_ci// recognized in your jurisdiction. 4e5c31af7Sopenharmony_ci// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE 5e5c31af7Sopenharmony_ci 6e5c31af7Sopenharmony_ci// included by json_value.cpp 7e5c31af7Sopenharmony_ci 8e5c31af7Sopenharmony_cinamespace Json { 9e5c31af7Sopenharmony_ci 10e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 11e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 12e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 13e5c31af7Sopenharmony_ci// class ValueIteratorBase 14e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 15e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 16e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 17e5c31af7Sopenharmony_ci 18e5c31af7Sopenharmony_ciValueIteratorBase::ValueIteratorBase() : current_() {} 19e5c31af7Sopenharmony_ci 20e5c31af7Sopenharmony_ciValueIteratorBase::ValueIteratorBase( 21e5c31af7Sopenharmony_ci const Value::ObjectValues::iterator& current) 22e5c31af7Sopenharmony_ci : current_(current), isNull_(false) {} 23e5c31af7Sopenharmony_ci 24e5c31af7Sopenharmony_ciValue& ValueIteratorBase::deref() { return current_->second; } 25e5c31af7Sopenharmony_ciconst Value& ValueIteratorBase::deref() const { return current_->second; } 26e5c31af7Sopenharmony_ci 27e5c31af7Sopenharmony_civoid ValueIteratorBase::increment() { ++current_; } 28e5c31af7Sopenharmony_ci 29e5c31af7Sopenharmony_civoid ValueIteratorBase::decrement() { --current_; } 30e5c31af7Sopenharmony_ci 31e5c31af7Sopenharmony_ciValueIteratorBase::difference_type 32e5c31af7Sopenharmony_ciValueIteratorBase::computeDistance(const SelfType& other) const { 33e5c31af7Sopenharmony_ci // Iterator for null value are initialized using the default 34e5c31af7Sopenharmony_ci // constructor, which initialize current_ to the default 35e5c31af7Sopenharmony_ci // std::map::iterator. As begin() and end() are two instance 36e5c31af7Sopenharmony_ci // of the default std::map::iterator, they can not be compared. 37e5c31af7Sopenharmony_ci // To allow this, we handle this comparison specifically. 38e5c31af7Sopenharmony_ci if (isNull_ && other.isNull_) { 39e5c31af7Sopenharmony_ci return 0; 40e5c31af7Sopenharmony_ci } 41e5c31af7Sopenharmony_ci 42e5c31af7Sopenharmony_ci // Usage of std::distance is not portable (does not compile with Sun Studio 12 43e5c31af7Sopenharmony_ci // RogueWave STL, 44e5c31af7Sopenharmony_ci // which is the one used by default). 45e5c31af7Sopenharmony_ci // Using a portable hand-made version for non random iterator instead: 46e5c31af7Sopenharmony_ci // return difference_type( std::distance( current_, other.current_ ) ); 47e5c31af7Sopenharmony_ci difference_type myDistance = 0; 48e5c31af7Sopenharmony_ci for (Value::ObjectValues::iterator it = current_; it != other.current_; 49e5c31af7Sopenharmony_ci ++it) { 50e5c31af7Sopenharmony_ci ++myDistance; 51e5c31af7Sopenharmony_ci } 52e5c31af7Sopenharmony_ci return myDistance; 53e5c31af7Sopenharmony_ci} 54e5c31af7Sopenharmony_ci 55e5c31af7Sopenharmony_cibool ValueIteratorBase::isEqual(const SelfType& other) const { 56e5c31af7Sopenharmony_ci if (isNull_) { 57e5c31af7Sopenharmony_ci return other.isNull_; 58e5c31af7Sopenharmony_ci } 59e5c31af7Sopenharmony_ci return current_ == other.current_; 60e5c31af7Sopenharmony_ci} 61e5c31af7Sopenharmony_ci 62e5c31af7Sopenharmony_civoid ValueIteratorBase::copy(const SelfType& other) { 63e5c31af7Sopenharmony_ci current_ = other.current_; 64e5c31af7Sopenharmony_ci isNull_ = other.isNull_; 65e5c31af7Sopenharmony_ci} 66e5c31af7Sopenharmony_ci 67e5c31af7Sopenharmony_ciValue ValueIteratorBase::key() const { 68e5c31af7Sopenharmony_ci const Value::CZString czstring = (*current_).first; 69e5c31af7Sopenharmony_ci if (czstring.data()) { 70e5c31af7Sopenharmony_ci if (czstring.isStaticString()) 71e5c31af7Sopenharmony_ci return Value(StaticString(czstring.data())); 72e5c31af7Sopenharmony_ci return Value(czstring.data(), czstring.data() + czstring.length()); 73e5c31af7Sopenharmony_ci } 74e5c31af7Sopenharmony_ci return Value(czstring.index()); 75e5c31af7Sopenharmony_ci} 76e5c31af7Sopenharmony_ci 77e5c31af7Sopenharmony_ciUInt ValueIteratorBase::index() const { 78e5c31af7Sopenharmony_ci const Value::CZString czstring = (*current_).first; 79e5c31af7Sopenharmony_ci if (!czstring.data()) 80e5c31af7Sopenharmony_ci return czstring.index(); 81e5c31af7Sopenharmony_ci return Value::UInt(-1); 82e5c31af7Sopenharmony_ci} 83e5c31af7Sopenharmony_ci 84e5c31af7Sopenharmony_ciString ValueIteratorBase::name() const { 85e5c31af7Sopenharmony_ci char const* keey; 86e5c31af7Sopenharmony_ci char const* end; 87e5c31af7Sopenharmony_ci keey = memberName(&end); 88e5c31af7Sopenharmony_ci if (!keey) 89e5c31af7Sopenharmony_ci return String(); 90e5c31af7Sopenharmony_ci return String(keey, end); 91e5c31af7Sopenharmony_ci} 92e5c31af7Sopenharmony_ci 93e5c31af7Sopenharmony_cichar const* ValueIteratorBase::memberName() const { 94e5c31af7Sopenharmony_ci const char* cname = (*current_).first.data(); 95e5c31af7Sopenharmony_ci return cname ? cname : ""; 96e5c31af7Sopenharmony_ci} 97e5c31af7Sopenharmony_ci 98e5c31af7Sopenharmony_cichar const* ValueIteratorBase::memberName(char const** end) const { 99e5c31af7Sopenharmony_ci const char* cname = (*current_).first.data(); 100e5c31af7Sopenharmony_ci if (!cname) { 101e5c31af7Sopenharmony_ci *end = nullptr; 102e5c31af7Sopenharmony_ci return nullptr; 103e5c31af7Sopenharmony_ci } 104e5c31af7Sopenharmony_ci *end = cname + (*current_).first.length(); 105e5c31af7Sopenharmony_ci return cname; 106e5c31af7Sopenharmony_ci} 107e5c31af7Sopenharmony_ci 108e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 109e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 110e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 111e5c31af7Sopenharmony_ci// class ValueConstIterator 112e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 113e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 114e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 115e5c31af7Sopenharmony_ci 116e5c31af7Sopenharmony_ciValueConstIterator::ValueConstIterator() = default; 117e5c31af7Sopenharmony_ci 118e5c31af7Sopenharmony_ciValueConstIterator::ValueConstIterator( 119e5c31af7Sopenharmony_ci const Value::ObjectValues::iterator& current) 120e5c31af7Sopenharmony_ci : ValueIteratorBase(current) {} 121e5c31af7Sopenharmony_ci 122e5c31af7Sopenharmony_ciValueConstIterator::ValueConstIterator(ValueIterator const& other) 123e5c31af7Sopenharmony_ci : ValueIteratorBase(other) {} 124e5c31af7Sopenharmony_ci 125e5c31af7Sopenharmony_ciValueConstIterator& ValueConstIterator:: 126e5c31af7Sopenharmony_cioperator=(const ValueIteratorBase& other) { 127e5c31af7Sopenharmony_ci copy(other); 128e5c31af7Sopenharmony_ci return *this; 129e5c31af7Sopenharmony_ci} 130e5c31af7Sopenharmony_ci 131e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 132e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 133e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 134e5c31af7Sopenharmony_ci// class ValueIterator 135e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 136e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 137e5c31af7Sopenharmony_ci// ////////////////////////////////////////////////////////////////// 138e5c31af7Sopenharmony_ci 139e5c31af7Sopenharmony_ciValueIterator::ValueIterator() = default; 140e5c31af7Sopenharmony_ci 141e5c31af7Sopenharmony_ciValueIterator::ValueIterator(const Value::ObjectValues::iterator& current) 142e5c31af7Sopenharmony_ci : ValueIteratorBase(current) {} 143e5c31af7Sopenharmony_ci 144e5c31af7Sopenharmony_ciValueIterator::ValueIterator(const ValueConstIterator& other) 145e5c31af7Sopenharmony_ci : ValueIteratorBase(other) { 146e5c31af7Sopenharmony_ci throwRuntimeError("ConstIterator to Iterator should never be allowed."); 147e5c31af7Sopenharmony_ci} 148e5c31af7Sopenharmony_ci 149e5c31af7Sopenharmony_ciValueIterator::ValueIterator(const ValueIterator&) = default; 150e5c31af7Sopenharmony_ci 151e5c31af7Sopenharmony_ciValueIterator& ValueIterator::operator=(const SelfType& other) { 152e5c31af7Sopenharmony_ci copy(other); 153e5c31af7Sopenharmony_ci return *this; 154e5c31af7Sopenharmony_ci} 155e5c31af7Sopenharmony_ci 156e5c31af7Sopenharmony_ci} // namespace Json 157