Skip to content

Commit

Permalink
COMP: Improve const correctness for ValueIterators (open-source-parse…
Browse files Browse the repository at this point in the history
…rs#1056)

The protected deref method had inconsistent interface
of being a const function that returned a non-const
reference.  Resolves open-source-parsers#914.
  • Loading branch information
hjmjohnson authored and baylesj committed Oct 17, 2019
1 parent a955529 commit b082693
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 15 additions & 4 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,14 @@ class JSON_API ValueIteratorBase {
char const* memberName(char const** end) const;

protected:
Value& deref() const;
/*! Internal utility functions to assist with implementing
* other iterator functions. The const and non-const versions
* of the "deref" protected methods expose the protected
* current_ member variable in a way that can often be
* optimized away by the compiler.
*/
const Value& deref() const;
Value& deref();

void increment();

Expand Down Expand Up @@ -895,9 +902,13 @@ class JSON_API ValueIterator : public ValueIteratorBase {
return *this;
}

reference operator*() const { return deref(); }

pointer operator->() const { return &deref(); }
/*! The return value of non-const iterators can be
* changed, so the these functions are not const
* because the returned references/pointers can be used
* to change state of the base class.
*/
reference operator*() { return deref(); }
pointer operator->() { return &deref(); }
};

inline void swap(Value& a, Value& b) { a.swap(b); }
Expand Down
3 changes: 2 additions & 1 deletion src/lib_json/json_valueiterator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ ValueIteratorBase::ValueIteratorBase(
const Value::ObjectValues::iterator& current)
: current_(current), isNull_(false) {}

Value& ValueIteratorBase::deref() const { return current_->second; }
Value& ValueIteratorBase::deref() { return current_->second; }
const Value& ValueIteratorBase::deref() const { return current_->second; }

void ValueIteratorBase::increment() { ++current_; }

Expand Down

0 comments on commit b082693

Please sign in to comment.