Skip to content

Commit

Permalink
Modified variant so foreach() works when the variant type is a list. …
Browse files Browse the repository at this point in the history
…e.g foreach(const variant& v, variantlist.range()) {}
  • Loading branch information
sweetkristas committed Jul 2, 2012
1 parent bdd700a commit 56fce07
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,15 @@ std::ostream& operator<<(std::ostream& os, const variant& v)
return os;
}

std::pair<variant*,variant*> variant::range() const
{
if(type_ == TYPE_LIST) {
return std::pair<variant*,variant*>(&(*list_->begin), &(*list_->end));
}
variant v;
return std::pair<variant*,variant*>(&v,&v);
}

UNIT_TEST(variant_decimal)
{
variant d(9876000, variant::DECIMAL_VARIANT);
Expand All @@ -1774,3 +1783,21 @@ BENCHMARK(variant_assign)
}
}
}

UNIT_TEST(variant_foreach)
{
std::vector<variant> l1;
l1.push_back(variant(1));
l1.push_back(variant(2));
l1.push_back(variant(3));
variant vl1(&l1);
int n = 1;
foreach(const variant& v, vl1.range()) {
CHECK_EQ(v.as_int(), n);
n++;
}

foreach(const variant& v, variant().range()) {
CHECK(false, "foreach null variant operator failed");
}
}
2 changes: 2 additions & 0 deletions src/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ class variant {
void set_debug_info(const debug_info& info);
const debug_info* get_debug_info() const;

std::pair<variant*,variant*> range() const;

private:
void must_be(TYPE t) const {
#if !TARGET_OS_IPHONE
Expand Down

0 comments on commit 56fce07

Please sign in to comment.