Skip to content

Commit

Permalink
Add iterator to var
Browse files Browse the repository at this point in the history
  • Loading branch information
FigBug committed Nov 18, 2024
1 parent 536c57f commit 01ba3ca
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
67 changes: 67 additions & 0 deletions modules/juce_core/containers/juce_Variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,73 @@ var::NativeFunctionArgs::NativeFunctionArgs (const var& t, const var* args, int
}

//==============================================================================
VarIterator::VarIterator (const var& v_, bool isEnd)
: v (v_)
{
if (isEnd)
{
if (v.isArray())
index = v.getArray()->size();
else if (auto o = v.getDynamicObject())
itr = o->getProperties().end();
}
else
{
if (v.isArray())
index = 0;
else if (auto o = v.getDynamicObject())
itr = o->getProperties().begin();
}
}

VarIterator& VarIterator::operator++()
{
if (v.isArray())
{
index++;
}
else if (v.getDynamicObject())
{
auto i = (const NamedValueSet::NamedValue*)itr;
i++;
itr = i;
}
return *this;
}

bool VarIterator::operator== (const VarIterator& other) const
{
return index == other.index && itr == other.itr;
}

bool VarIterator::operator!= (const VarIterator& other) const
{
return ! (*this == other);
}

VarIterator::NamedValue VarIterator::operator*() const
{
if (v.isArray())
{
return { index, (*v.getArray())[index] };
}
else if (v.getDynamicObject())
{
auto i = (const NamedValueSet::NamedValue*)itr;
return { i->name.toString(), i->value };
}
return {};
}

VarIterator begin (const var& v)
{
return VarIterator (v, false);
}

VarIterator end (const var& v)
{
return VarIterator (v, true);
}

//==============================================================================
#if JUCE_ALLOW_STATIC_NULL_VARIABLES
Expand Down
38 changes: 38 additions & 0 deletions modules/juce_core/containers/juce_Variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,42 @@ JUCE_API bool operator== (const var&, const String&);
JUCE_API bool operator!= (const var&, const String&);
JUCE_API bool operator== (const var&, const char*);
JUCE_API bool operator!= (const var&, const char*);


//==============================================================================
/** Iterator for a var.
You shouldn't ever need to use this class directly - it's used internally by begin()
and end() to allow range-based-for loops on a var.
*/
struct VarIterator
{
struct NamedValue
{
var name;
var value;
};

VarIterator (const var&, bool isEnd);
VarIterator& operator++();

bool operator== (const VarIterator&) const;
bool operator!= (const VarIterator&) const;
NamedValue operator*() const;

using difference_type = std::ptrdiff_t;
using value_type = NamedValue;
using reference = NamedValue&;
using pointer = NamedValue*;
using iterator_category = std::forward_iterator_tag;

private:
const var& v;
int index = 0;
const void* itr = nullptr;
};

VarIterator begin (const var&);
VarIterator end (const var&);


} // namespace juce

0 comments on commit 01ba3ca

Please sign in to comment.