mono / CppSharp

Tools and libraries to glue C/C++ APIs to high-level languages
MIT License
3.14k stars 516 forks source link

Handle returned arrays and arrays in parameters of overrides #953

Open ddobrev opened 7 years ago

ddobrev commented 7 years ago

We don't know the size of an array returned from native code. There's nothing we can do except let users deal with it if they know that size. @tritao suggests we use a special class called CArray which is constructed with a single pointer and has a convenient method which takes a size and returns an iterator based on it.

There are two ways we can use this CArray:

  1. Only use it for returned arrays and for arrays in parameters of virtual methods - use regular C# arrays in the remaining case of parameters used by non-virtual functions;
  2. Use CArray in all cases.

The first way has the advantage of providing a simpler API users are familiar with. The second way has the advantage of consistency. We have so far put the decision off until the time comes to implement this.

ddobrev commented 7 years ago

More notes: Arrays do not always have a fixed size, sometimes delimiters are used instead. This basically suggests it's the user's responsibility to properly work with the array. Which leads us to approach:

  1. Simply use an IEnumerable in such cases and let users deal with it themselves - just as in C++. It has the disadvantage of having the users think they can just enumerate it as usual which would lead to crashes.

The other way is to have an overload of CArray which returns an enumerator not based on a size. This way users can still look for the delimiter themselves.

ddobrev commented 7 years ago

In addition, we could make CArray itself IEnumerable with a default implementation which looks for null as the delimiter. GetEnumerator can have one more overload taking a lambda which determines where to end the iteration.

ddobrev commented 7 years ago

We could also feature a mechanism for users to manually determine how a returned array should be handled: by size or by a delimiter.