Пример #1
0
        public CefTypeKind GetTypeKind(ITypeSymbol symbol)
        {
            if (!symbol.Name.StartsWith("cef_"))
            {
                return(CefTypeKind.Unknown);
            }

            INamedTypeSymbol value;

            if (RefCounted.TryGetValue(symbol.Name, out value) && Equals(value, symbol))
            {
                return(CefTypeKind.RefCounted);
            }
            if (Scoped.TryGetValue(symbol.Name, out value) && Equals(value, symbol))
            {
                return(CefTypeKind.Scoped);
            }
            if (Enums.TryGetValue(symbol.Name, out value) && Equals(value, symbol))
            {
                return(CefTypeKind.Enum);
            }
            if (Sized.TryGetValue(symbol.Name, out value) && Equals(value, symbol))
            {
                return(CefTypeKind.Sized);
            }
            if (Simple.TryGetValue(symbol.Name, out value) && Equals(value, symbol))
            {
                return(CefTypeKind.Simple);
            }
            throw new NotImplementedException();
        }
Пример #2
0
        /// <summary>
        /// Returns an instance of a type that represents a <see cref="CefBaseRefCounted"/>
        /// object by an unspecified pointer.
        /// </summary>
        /// <param name="ptr">A pointer to a ref-counted struct.</param>
        /// <returns>
        /// A <see cref="CefBaseRefCounted"/>-based object that corresponds to the pointer
        /// or null if wrapper not found.
        /// </returns>
        public static CefBaseRefCounted GetInstance(IntPtr ptr)
        {
            RefCountedReference reference;

            Internal.CefBaseRefCountedImpl.GlobalSyncRoot.EnterReadLock();
            try
            {
                RefCounted.TryGetValue(ptr, out reference);
                if (reference != null && reference.Instance.TryGetTarget(out CefBaseRefCounted instance))
                {
                    return(instance);
                }
            }
            finally
            {
                Internal.CefBaseRefCountedImpl.GlobalSyncRoot.ExitReadLock();
            }
            return(null);
        }
Пример #3
0
        /// <summary>
        /// Returns a wrapper for the specified pointer.
        /// </summary>
        /// <typeparam name="TClass">The type of wrapper.</typeparam>
        /// <param name="create">Represents a method that create a new wrapper.</param>
        /// <param name="instance">The pointer to ref-counted CEF struct.</param>
        /// <returns>Returns an existing or new wrapper for the specified pointer.</returns>
        public unsafe static TClass Wrap <TClass>(Func <IntPtr, TClass> create, T *instance)
            where TClass : CefBaseRefCounted <T>
        {
            if (instance == null)
            {
                return(null);
            }

            RefCountedWrapperStruct *ws = null;
            CefBaseRefCounted        wrapper;
            IntPtr key = new IntPtr(instance);

            Internal.CefBaseRefCountedImpl.GlobalSyncRoot.EnterUpgradeableReadLock();
            try
            {
                if (CefApi.UseUnsafeImplementation)
                {
                    ws = GetWrapperStructPtr(instance);
                    if (ws != null && UnsafeRefCounted.TryGetValue(ws->cppObject, out WeakReference <CefBaseRefCounted> weakRef) &&
                        weakRef.TryGetTarget(out wrapper))
                    {
                        ((cef_base_ref_counted_t *)instance)->Release();
                        return((TClass)wrapper);
                    }
                }

                if (RefCounted.TryGetValue(key, out RefCountedReference reference) &&
                    reference.Instance.TryGetTarget(out wrapper))
                {
                    ((cef_base_ref_counted_t *)instance)->Release();
                    return((TClass)wrapper);
                }
#if DEBUG
                else if (CefStructure.IsAllocated(key))
                {
                    throw new InvalidCefObjectException(string.Format("Unexpected access to {0}.", typeof(TClass).Name));
                }
#endif
                else
                {
                    Internal.CefBaseRefCountedImpl.GlobalSyncRoot.EnterWriteLock();
                    try
                    {
                        TClass typedWrapper = create(key);
                        var    weakRef      = new WeakReference <CefBaseRefCounted>(typedWrapper);
                        RefCounted[key] = new RefCountedReference(weakRef);
                        if (ws != null)
                        {
                            UnsafeRefCounted[ws->cppObject] = weakRef;
                        }
                        return(typedWrapper);
                    }
                    finally
                    {
                        Internal.CefBaseRefCountedImpl.GlobalSyncRoot.ExitWriteLock();
                    }
                }
            }
            finally
            {
                Internal.CefBaseRefCountedImpl.GlobalSyncRoot.ExitUpgradeableReadLock();
            }
        }