JsonCpp project page | Classes | Namespace | JsonCpp home page

json_valueiterator.inl
Go to the documentation of this file.
// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
// included by json_value.cpp
namespace Json {
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// class ValueIteratorBase
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
ValueIteratorBase::ValueIteratorBase() : current_() {}
ValueIteratorBase::ValueIteratorBase(
const Value::ObjectValues::iterator& current)
: current_(current), isNull_(false) {}
Value& ValueIteratorBase::deref() { return current_->second; }
const Value& ValueIteratorBase::deref() const { return current_->second; }
void ValueIteratorBase::increment() { ++current_; }
void ValueIteratorBase::decrement() { --current_; }
ValueIteratorBase::difference_type
ValueIteratorBase::computeDistance(const SelfType& other) const {
// Iterator for null value are initialized using the default
// constructor, which initialize current_ to the default
// std::map::iterator. As begin() and end() are two instance
// of the default std::map::iterator, they can not be compared.
// To allow this, we handle this comparison specifically.
if (isNull_ && other.isNull_) {
return 0;
}
// Usage of std::distance is not portable (does not compile with Sun Studio 12
// RogueWave STL,
// which is the one used by default).
// Using a portable hand-made version for non random iterator instead:
// return difference_type( std::distance( current_, other.current_ ) );
difference_type myDistance = 0;
for (Value::ObjectValues::iterator it = current_; it != other.current_;
++it) {
++myDistance;
}
return myDistance;
}
bool ValueIteratorBase::isEqual(const SelfType& other) const {
if (isNull_) {
return other.isNull_;
}
return current_ == other.current_;
}
void ValueIteratorBase::copy(const SelfType& other) {
current_ = other.current_;
isNull_ = other.isNull_;
}
Value ValueIteratorBase::key() const {
const Value::CZString czstring = (*current_).first;
if (czstring.data()) {
if (czstring.isStaticString())
return Value(StaticString(czstring.data()));
return Value(czstring.data(), czstring.data() + czstring.length());
}
return Value(czstring.index());
}
UInt ValueIteratorBase::index() const {
const Value::CZString czstring = (*current_).first;
if (!czstring.data())
return czstring.index();
return Value::UInt(-1);
}
String ValueIteratorBase::name() const {
char const* keey;
char const* end;
keey = memberName(&end);
if (!keey)
return String();
return String(keey, end);
}
char const* ValueIteratorBase::memberName() const {
const char* cname = (*current_).first.data();
return cname ? cname : "";
}
char const* ValueIteratorBase::memberName(char const** end) const {
const char* cname = (*current_).first.data();
if (!cname) {
*end = nullptr;
return nullptr;
}
*end = cname + (*current_).first.length();
return cname;
}
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// class ValueConstIterator
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
ValueConstIterator::ValueConstIterator() = default;
ValueConstIterator::ValueConstIterator(
const Value::ObjectValues::iterator& current)
: ValueIteratorBase(current) {}
ValueConstIterator::ValueConstIterator(ValueIterator const& other)
: ValueIteratorBase(other) {}
ValueConstIterator&
ValueConstIterator::operator=(const ValueIteratorBase& other) {
copy(other);
return *this;
}
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// class ValueIterator
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
ValueIterator::ValueIterator() = default;
ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
: ValueIteratorBase(current) {}
ValueIterator::ValueIterator(const ValueConstIterator& other)
: ValueIteratorBase(other) {
throwRuntimeError("ConstIterator to Iterator should never be allowed.");
}
ValueIterator::ValueIterator(const ValueIterator& other) = default;
ValueIterator& ValueIterator::operator=(const SelfType& other) {
copy(other);
return *this;
}
} // namespace Json
Json::StaticString
Lightweight wrapper to tag static string.
Definition value.h:161
Json::ValueConstIterator
const iterator for object and array value.
Definition value.h:943
Json::ValueConstIterator::ValueConstIterator
ValueConstIterator()
Json::ValueConstIterator::operator=
SelfType & operator=(const ValueIteratorBase &other)
Definition json_valueiterator.inl:126
Json::Value
Represents a JSON value.
Definition value.h:207
Json::Value::UInt
Json::UInt UInt
Definition value.h:215
Json::ValueIteratorBase
base class for Value iterators.
Definition value.h:869
Json::ValueIteratorBase::isEqual
bool isEqual(const SelfType &other) const
Definition json_valueiterator.inl:55
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::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::ValueIteratorBase::copy
void copy(const SelfType &other)
Definition json_valueiterator.inl:62
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::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::ValueIteratorBase::deref
const Value & deref() const
Definition json_valueiterator.inl:25
Json::ValueIteratorBase::SelfType
ValueIteratorBase SelfType
Definition value.h:874
Json::ValueIteratorBase::computeDistance
difference_type computeDistance(const SelfType &other) const
Definition json_valueiterator.inl:32
Json::ValueIteratorBase::ValueIteratorBase
ValueIteratorBase()
Definition json_valueiterator.inl:18
Json::ValueIteratorBase::increment
void increment()
Definition json_valueiterator.inl:27
Json::ValueIteratorBase::difference_type
int difference_type
Definition value.h:873
Json::ValueIteratorBase::decrement
void decrement()
Definition json_valueiterator.inl:29
Json::ValueIterator
Iterator for object and array value.
Definition value.h:994
Json::ValueIterator::ValueIterator
ValueIterator()
Json::ValueIterator::SelfType
ValueIterator SelfType
Definition value.h:1003
Json::ValueIterator::operator=
SelfType & operator=(const SelfType &other)
Definition json_valueiterator.inl:151
Json
JSON (JavaScript Object Notation).
Definition allocator.h:16
Json::UInt
unsigned int UInt
Definition config.h:110
Json::String
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition config.h:135