Logrus
Topic Author
Posts: 73
Joined: 22 Jul 2015, 12:16

Range-based for loop for ObservableCollection

03 Jun 2025, 10:29

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?
 
User avatar
jsantos
Site Admin
Posts: 4823
Joined: 20 Jan 2012, 17:18
Contact:

Re: Range-based for loop for ObservableCollection

03 Jun 2025, 10:46

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?
 
Logrus
Topic Author
Posts: 73
Joined: 22 Jul 2015, 12:16

Re: Range-based for loop for ObservableCollection

03 Jun 2025, 10:50

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) {}
 
User avatar
jsantos
Site Admin
Posts: 4823
Joined: 20 Jan 2012, 17:18
Contact:

Re: Range-based for loop for ObservableCollection

05 Jun 2025, 14:50

Please, create a ticket for this. Thank you!
 
Logrus
Topic Author
Posts: 73
Joined: 22 Jul 2015, 12:16

Re: Range-based for loop for ObservableCollection

06 Jun 2025, 13:13

 
User avatar
jsantos
Site Admin
Posts: 4823
Joined: 20 Jan 2012, 17:18
Contact:

Re: Range-based for loop for ObservableCollection

09 Jun 2025, 16:39

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.

Who is online

Users browsing this forum: Bing [Bot] and 3 guests