CSharp bindings: user-defined string encoding and improved safety#14366
CSharp bindings: user-defined string encoding and improved safety#14366Mbucari wants to merge 11 commits into
Conversation
|
It is probably going to be next week before I can look at this |
|
@Mbucari can you rebase against master and run the tests again |
2e35cdc to
7ace005
Compare
| %typemap(imtype) (char **ignorechange) "byte[]" | ||
| %typemap(cstype) (char **ignorechange) "ref string" |
There was a problem hiding this comment.
This new typedef for char **ignorechange is nice because it handles the extra level of indirection in the in definition below, so cstype doesn't even need to be a ref string. This raises the question, how loathed is the GDAL community to change/remove a function after it's already been made public? If we changed this typedef to just accept a string, then only 2 methods would change: SpatialReference.ImportFromWkt(ref string); and Ogr.CreateGeometryFromWkt(ref string, SpatialReference). This would be more convenient, but it would break things downstream (albeit things which are easily fixed).
This question will be relevant to future additions I have planned, such as adding multi-argument typemaps for arrays (like already exist for Java) which will eliminate the need to pass both the array and the array length.
Expose $module.IStringMarshaller interfaces which have 2 methods: - `string FromNullTerminated(IntPtr pStr)` - Read native null-terminated data from a Gdal pointer and return a managed string. - `byte[] ToNullTerminated(string str)` - Create a null-terminated array of bytes from a managed string to send to Gdal. Public static properties `$module.StringMarshaller` can be used to get/set a custom marshaller interfaces. The default marshaller is internal `DefaultStringMarshaller`. String helper callback has been changed. When `SWIG_csharp_string_callback` is called, it decodes the unmanaged Gdal string to a managed .NET string (using StringMarshaller.ToNullTerminated()) and then returns a pointer to a pinned GCHandle of that string. StringFromPinnedGCHandle() is called from the C# methods to retrieve the string and free the GCHandle.
Create a dummy SWIG object, CsharpDummyObject, that can be extended using typemap(csimports) to add custom C# types to the module namespace.
What does this PR do?
Creates an interface for users to create custom string Encoder/Decoders for Gdal to use for all string operations. This serves a purpose similar to #3825 by allowing users to manage how strings are encoded/decoded to/from Gdal.
Expose
I$moduleStringEncoderinterfaces which have 2 methods:string FromNullTerminated(IntPtr pStr);- Read native null-terminated data from a Gdal pointer and return a managed string.byte[] ToNullTerminated(string str);- Create a null-terminated array of bytes from a managed string to send to Gdal.It also simplifies string marshalling changes made in recent PR by no longer requires C# to allocate/free any unmanaged memory. All string/byte[] objects are managed.
Here's an example of a custom StringEncoder.
@runette
What are related issues/pull requests?
#3825
#14283
Tasklist
Environment
Provide environment details, if relevant:
Mono and dotnet