/// <summary> /// Gets a shared data for this device context with a delegate to create the shared data if it is not present. /// </summary> /// <typeparam name="T">Type of the shared data to get/create.</typeparam> /// <param name="type">Type of the data to share.</param> /// <param name="key">The key of the shared data.</param> /// <param name="sharedDataCreator">The shared data creator.</param> /// <returns> /// An instance of the shared data. The shared data will be disposed by this <see cref="GraphicsDevice" /> instance. /// </returns> public T GetOrCreateSharedData <T>(object key, CreateSharedData <T> sharedDataCreator) where T : class, IDisposable { lock (sharedDataPerDevice) { IDisposable localValue; if (!sharedDataPerDevice.TryGetValue(key, out localValue)) { localValue = sharedDataCreator(this); if (localValue == null) { return(null); } sharedDataToDispose.Add(localValue); sharedDataPerDevice.Add(key, localValue); } return((T)localValue); } }
/// <summary> /// Gets a shared data for this device context with a delegate to create the shared data if it is not present. /// </summary> /// <typeparam name="T">Type of the shared data to get/create.</typeparam> /// <param name="type">Type of the data to share.</param> /// <param name="key">The key of the shared data.</param> /// <param name="sharedDataCreator">The shared data creator.</param> /// <returns> /// An instance of the shared data. The shared data will be disposed by this <see cref="GraphicsDevice" /> instance. /// </returns> public T GetOrCreateSharedData <T>(GraphicsDeviceSharedDataType type, object key, CreateSharedData <T> sharedDataCreator) where T : class, IDisposable { Dictionary <object, IDisposable> dictionary = (type == GraphicsDeviceSharedDataType.PerDevice) ? sharedDataPerDevice : sharedDataPerDeviceContext; lock (dictionary) { IDisposable localValue; if (!dictionary.TryGetValue(key, out localValue)) { localValue = sharedDataCreator(this); if (localValue == null) { return(null); } sharedDataToDispose.Add(localValue); dictionary.Add(key, localValue); } return((T)localValue); } }