Page 1 of 1
Range-based for loop for ObservableCollection
Posted: 03 Jun 2025, 10:29
by Logrus
In the C++ SDK, the range-based for loop for ObservableCollection is not implemented.
Is there a specific reason for this, or is it simply not implemented?
Re: Range-based for loop for ObservableCollection
Posted: 03 Jun 2025, 10:46
by jsantos
Yeah, I would love to have this too, but the problem is that
ObservableCollection is a pointer, so the syntax would be a bit awkward and I am not sure that's good. Something like:
for (T* item: *collection) {}
What do you think?
Re: Range-based for loop for ObservableCollection
Posted: 03 Jun 2025, 10:50
by Logrus
namespace Noesis
{
template<typename T>
class ObservableCollectionIterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T*;
using difference_type = std::ptrdiff_t;
using pointer = T**;
using reference = T*&;
ObservableCollectionIterator(ObservableCollection<T>* collection, uint32_t index)
: mCollection(collection), mIndex(index)
{
}
T* operator*() const { return mCollection->Get(mIndex); }
ObservableCollectionIterator& operator++() { ++mIndex; return *this; }
bool operator==(const ObservableCollectionIterator& other) const
{
return mIndex == other.mIndex;
}
private:
ObservableCollection<T>* mCollection;
uint32_t mIndex;
};
template<typename T>
ObservableCollectionIterator<T> begin(ObservableCollection<T>* collection)
{
return { collection, 0 };
}
template<typename T>
ObservableCollectionIterator<T> begin(const ObservableCollection<T>* collection)
{
return { collection, 0 };
}
template<typename T>
ObservableCollectionIterator<T> begin(Ptr<ObservableCollection<T>>& collection)
{
return { collection->GetPtr(), 0 };
}
template<typename T>
ObservableCollectionIterator<T> begin(const Ptr<ObservableCollection<T>>& collection)
{
return { collection.GetPtr(), 0 };
}
template<typename T>
ObservableCollectionIterator<T> end(ObservableCollection<T>* collection)
{
return { collection, (uint32_t)collection->Count() };
}
template<typename T>
ObservableCollectionIterator<T> end(const ObservableCollection<T>* collection)
{
return { collection, (uint32_t)collection->Count() };
}
template<typename T>
ObservableCollectionIterator<T> end(Ptr<ObservableCollection<T>>& collection)
{
return { collection.GetPtr(), (uint32_t)collection->Count()};
}
template<typename T>
ObservableCollectionIterator<T> end(const Ptr<ObservableCollection<T>>& collection)
{
return { collection.GetPtr(), (uint32_t)collection->Count() };
}
}
It is possible to create overloads for begin and end. This should work for both raw pointers and Noesis::Ptr
for(auto item : collection) {}
Re: Range-based for loop for ObservableCollection
Posted: 05 Jun 2025, 14:50
by jsantos
Please, create a ticket for this. Thank you!
Re: Range-based for loop for ObservableCollection
Posted: 06 Jun 2025, 13:13
by Logrus
Re: Range-based for loop for ObservableCollection
Posted: 09 Jun 2025, 16:39
by jsantos
Thanks for the ticket. We didn’t have time to address this in 3.2.8, but it’s planned for the next version.
One of the reasons we haven’t modified this API yet is because we have long-term plans to improve the Reflection architecture to support using custom containers (such as std::vector) in MVVM.