Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2008

Tip: Looking for answers? Try searching our database.

C++/CLI class implementing Enumerable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kenneth Porter - 07 May 2008 00:05 GMT
Can someone point me to documentation or examples on how to implement an
Enumerable interface in C++/CLI? I've found examples for C# but nothing for
C++.

I'm wrapping a native library that supplies iterators and I'd like to
provide the .NET equivalent in the wrapper.
David Anton - 07 May 2008 00:56 GMT
There aren't enough good samples in C++/CLI.
Any C# to C++/CLI converter should work for this (you can get a demo edition
of Instant C++ at our site - it converts C# to C++/CLI).
Signature

http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
Java to VB & C#
Instant C#: convert VB to C#
Instant VB: convert C# to VB
Instant C++: VB, C#, or Java to C++/CLI

> Can someone point me to documentation or examples on how to implement an
> Enumerable interface in C++/CLI? I've found examples for C# but nothing for
> C++.
>
> I'm wrapping a native library that supplies iterators and I'd like to
> provide the .NET equivalent in the wrapper.
Kenneth Porter - 07 May 2008 01:50 GMT
> Can someone point me to documentation or examples on how to implement
> an Enumerable interface in C++/CLI? I've found examples for C# but
> nothing for C++.

I found enough text in this online book to get me started, and I think I
can deduce the rest from the MSDN docs.

http://books.google.com/books?id=_DZpE7tzjKcC

http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx
Ben Voigt [C++ MVP] - 07 May 2008 14:18 GMT
> Can someone point me to documentation or examples on how to implement
> an Enumerable interface in C++/CLI? I've found examples for C# but
> nothing for C++.
>
> I'm wrapping a native library that supplies iterators and I'd like to
> provide the .NET equivalent in the wrapper.

something like this:

template <typename result, typename iterator>
ref class IteratorWrapper : public IEnumerable<result>
{
   iterator m_begin, m_end, m_current;

public:
   IteratorWrapper(iterator begin, iterator end)
       : m_begin(begin), m_end(end), m_current(begin)
   {}

   bool MoveNext( void ) override { return m_end != ++m_current; }
   property result Value {
       result get() { return *m_current; }
   }
};

Write a template helper function or two to get argument deduction:

template<typename T>
IEnumerable<T>^ WrapIterator(T* begin, T* end)
{
   return gcnew IteratorWrapper<T, T*>(begin, end);
}

template<typename T>
value struct WrapCollection
{
   template<typename T2>
   IEnumerable<T>^ WrapIterator(T2* begin, T2* end)
   {
       return gcnew IteratorWrapper<T, T2*>(begin, end);
   }
};

Now you can enumerate an array:
T[] a = new T(N);
IEnumerable<T>^ e = WrapIterator(a, a+N);

or a vector:
std::vector<T> v;
IEnumerable<T>^ e = WrapCollection<T>::WrapIterator(v.begin(), v.end());

and so on.
Ben Voigt [C++ MVP] - 07 May 2008 14:21 GMT
>> Can someone point me to documentation or examples on how to implement
>> an Enumerable interface in C++/CLI? I've found examples for C# but
[quoted text clipped - 7 lines]
> template <typename result, typename iterator>
> ref class IteratorWrapper : public IEnumerable<result>

oops, that should have been IEnumerator<result>, ditto for every other
mention of IEnumerable in my example

> {
>    iterator m_begin, m_end, m_current;
[quoted text clipped - 37 lines]
> v.end());
> and so on.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.