Skip to content

Iterator conversions #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ class JSON_API ValueConstIterator : public ValueIteratorBase {
typedef ValueConstIterator SelfType;

ValueConstIterator();
ValueConstIterator(ValueIterator const& other);

private:
/*! \internal Use by Value to create an iterator.
Expand Down Expand Up @@ -787,7 +788,7 @@ class JSON_API ValueIterator : public ValueIteratorBase {
typedef ValueIterator SelfType;

ValueIterator();
ValueIterator(const ValueConstIterator& other);
explicit ValueIterator(const ValueConstIterator& other);
ValueIterator(const ValueIterator& other);

private:
Expand Down
7 changes: 6 additions & 1 deletion src/lib_json/json_valueiterator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ ValueConstIterator::ValueConstIterator(
const Value::ObjectValues::iterator& current)
: ValueIteratorBase(current) {}

ValueConstIterator::ValueConstIterator(ValueIterator const& other)
: ValueIteratorBase(other) {}

ValueConstIterator& ValueConstIterator::
operator=(const ValueIteratorBase& other) {
copy(other);
Expand All @@ -149,7 +152,9 @@ ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
: ValueIteratorBase(current) {}

ValueIterator::ValueIterator(const ValueConstIterator& other)
: ValueIteratorBase(other) {}
: ValueIteratorBase(other) {
throwRuntimeError("ConstIterator to Iterator should never be allowed.");
}

ValueIterator::ValueIterator(const ValueIterator& other)
: ValueIteratorBase(other) {}
Expand Down
31 changes: 31 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <json/json.h>
#include <cstring>
#include <limits>
#include <sstream>
#include <string>
#include <iomanip>

// Make numeric limits more convenient to talk about.
// Assumes int type in 32 bits.
Expand Down Expand Up @@ -2430,6 +2433,33 @@ JSONTEST_FIXTURE(IteratorTest, indexes) {
JSONTEST_ASSERT(it == json.end());
}

JSONTEST_FIXTURE(IteratorTest, const) {
Json::Value const v;
JSONTEST_ASSERT_THROWS(
Json::Value::iterator it(v.begin()) // Compile, but throw.
);

Json::Value value;

for(int i = 9; i < 12; ++i)
{
std::ostringstream out;
out << std::setw(2) << i;
std::string str = out.str();
value[str] = str;
}

std::ostringstream out;
//in old code, this will get a compile error
Json::Value::const_iterator iter = value.begin();
for(; iter != value.end(); ++iter)
{
out << *iter << ',';
}
std::string expected = "\" 9\",\"10\",\"11\",";
JSONTEST_ASSERT_STRING_EQUAL(expected, out.str());
}

int main(int argc, const char* argv[]) {
JsonTest::Runner runner;
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, checkNormalizeFloatingPointStr);
Expand Down Expand Up @@ -2500,6 +2530,7 @@ int main(int argc, const char* argv[]) {
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, distance);
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, names);
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, indexes);
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, const);

return runner.runCommandLine(argc, argv);
}