[CTFont] Subclass NativeObject + numerous other code updates (#13102)

* [CTFont] Subclass NativeObject + numerous other code updates

* Subclass NativeObject to reuse object lifetime code.
* Enable nullability and fix code accordingly.
* Use 'is' and 'is not' instead of '==' and '!=' for object identity.
* Use CFString.CreateNative/ReleaseNative instead of other means to create
  native strings (the fastest and least memory hungry option).
* Use the null-safe NativeObjectExtensions.GetHandle extension method to get
  the handle instead of checking for null (avoids some code duplication).
* Use 'nameof (parameter)' instead of string constants.
* Use the 'Runtime.GetNSObject<T> (IntPtr, bool)' overload to specify handle
  ownership, to avoid having to call NSObject.DangerousRelease manually later.
* Use Array.Empty<T> instead of creating an empty array manually.
* Add an NSArray.ArrayFromHandle overload that releases the handle if requested (and use it).

* Remove more code.
This commit is contained in:
Rolf Bjarne Kvinge 2021-10-27 10:17:05 +02:00 коммит произвёл GitHub
Родитель bbb5838113
Коммит becfa7d992
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 348 добавлений и 305 удалений

Просмотреть файл

@ -90,7 +90,7 @@ namespace CoreAnimation {
var handle = _Font;
nint type = CFType.GetTypeID (handle);
if (type == CTFont.GetTypeID ())
return new CTFont (handle);
return new CTFont (handle, false);
else if (type == CGFont.GetTypeID ())
return new CGFont (handle, false);
else if (type == CFString.GetTypeID ())

Просмотреть файл

@ -24,57 +24,10 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using ObjCRuntime;
using CoreFoundation;
using CoreGraphics;
using Foundation;
namespace CoreText {
public partial class CTFont : INativeObject, IDisposable {
internal IntPtr handle;
internal CTFont (IntPtr handle)
: this (handle, false)
{
}
internal CTFont (IntPtr handle, bool owns)
{
if (handle == IntPtr.Zero) {
GC.SuppressFinalize (this);
throw new ArgumentNullException ("handle");
}
this.handle = handle;
if (!owns)
CFObject.CFRetain (handle);
}
~CTFont ()
{
Dispose (false);
}
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
public IntPtr Handle {
get { return handle; }
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero){
CFObject.CFRelease (handle);
handle = IntPtr.Zero;
}
}
public partial class CTFont : NativeObject {
}
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -355,6 +355,14 @@ namespace Foundation {
return ret;
}
static public T [] ArrayFromHandle<T> (IntPtr handle, Converter<IntPtr, T> creator, bool releaseHandle)
{
var rv = ArrayFromHandle<T> (handle, creator);
if (releaseHandle && handle == IntPtr.Zero)
NSObject.DangerousRelease (handle);
return rv;
}
// FIXME: before proving a real `this` indexer we need to clean the issues between
// NSObject and INativeObject coexistance across all the API (it can not return T)