Beispiel #1
0
 /// <summary>
 /// Ensure that the given native object <paramref name="wrapper"/>
 /// has not been disposed.
 /// </summary>
 /// <param name="wrapper">The native object wrapper to validate.</param>
 public unsafe static void NotDisposed(NativeDisposable wrapper)
 {
     if (wrapper.IsDisposed)
     {
         throw new ObjectDisposedException(wrapper.GetType().Name);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Safely executes the given native function call for a given
        /// object, ensuring that the object is not disposed and that it
        /// will be kept alive until the end of the call.
        /// </summary>
        /// <param name="call">The function to invoke.</param>
        /// <param name="obj">The object to validate and keep alive.</param>
        public static T NativeCall <T>(Func <T> call, NativeDisposable obj)
        {
            Ensure.NotDisposed(obj);
            T ret = call();

            GC.KeepAlive(obj);
            return(ret);
        }
Beispiel #3
0
        /// <summary>
        /// Creates a lazy value that will be created by the given
        /// <paramref name="valueFactory"/> upon first use, ensuring
        /// that the given <paramref name="nativeObject"/> is not disposed.
        /// Subsequent values will be returned without invoking the factory.
        /// </summary>
        /// <param name="valueFactory">The function to invoke to populate the value.</param>
        /// <param name="nativeObject">A native object to ensure is not disposed.</param>
        public LazyNative(Func <T> valueFactory, NativeDisposable nativeObject)
        {
            Ensure.ArgumentNotNull(valueFactory, "valueFactory");
            Ensure.ArgumentNotNull(nativeObject, "nativeObject");

            this.valueFactory = valueFactory;
            this.nativeObject = nativeObject;
        }
Beispiel #4
0
 /// <summary>
 /// Invoke the given function and ensure that its return code
 /// indicates success; ie that it is non-negative.
 /// </summary>
 /// <param name="call">The function call to invoke.</param>
 /// <param name="obj">The object to validate and keep alive.</param>
 public static void NativeSuccess(Func <int> call, NativeDisposable obj)
 {
     NativeSuccess(NativeCall <int>(call, obj));
 }