Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ComponentBaseExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 
5 namespace SiliconStudio.Core
6 {
7  /// <summary>
8  /// Extensions for <see cref="IComponent"/>.
9  /// </summary>
10  public static class ComponentBaseExtensions
11  {
12  /// <summary>
13  /// Keeps a component alive by adding it to a container.
14  /// </summary>
15  /// <typeparam name="T">A component</typeparam>
16  /// <param name="thisArg">The component to keep alive.</param>
17  /// <param name="container">The container that will keep a reference to the component.</param>
18  /// <returns>The same component instance</returns>
19  public static void RemoveKeepAliveBy<T>(this T thisArg, ICollectorHolder container) where T : IReferencable
20  {
21  if (ReferenceEquals(thisArg, null))
22  return;
23  container.Collector.Remove(thisArg);
24  }
25 
26  /// <summary>
27  /// Keeps a component alive by adding it to a container.
28  /// </summary>
29  /// <typeparam name="T">A component</typeparam>
30  /// <param name="thisArg">The component to keep alive.</param>
31  /// <param name="container">The container that will keep a reference to the component.</param>
32  /// <returns>The same component instance</returns>
33  public static T KeepAliveBy<T>(this T thisArg, ICollectorHolder container) where T : IReferencable
34  {
35  if (ReferenceEquals(thisArg, null))
36  return thisArg;
37  return container.Collector.Add(thisArg);
38  }
39 
40  /// <summary>
41  /// Keeps a component alive by adding it to a container.
42  /// </summary>
43  /// <typeparam name="T">A component</typeparam>
44  /// <param name="thisArg">The component to keep alive.</param>
45  /// <param name="collector">The collector.</param>
46  /// <returns>The same component instance</returns>
47  public static T KeepAliveBy<T>(this T thisArg, ObjectCollector collector) where T : IReferencable
48  {
49  if (ReferenceEquals(thisArg, null))
50  return thisArg;
51  return collector.Add(thisArg);
52  }
53 
54  /// <summary>
55  /// Pins this component as a new reference.
56  /// </summary>
57  /// <typeparam name="T">A component</typeparam>
58  /// <param name="thisArg">The component to add a reference to.</param>
59  /// <returns>This component.</returns>
60  /// <remarks>This method is equivalent to call <see cref="IReferencable.AddReference"/> and return this instance.</remarks>
61  public static T KeepReference<T>(this T thisArg) where T : IReferencable
62  {
63  if (ReferenceEquals(thisArg, null))
64  return thisArg;
65  thisArg.AddReference();
66  return thisArg;
67  }
68 
69  /// <summary>
70  /// Keeps a component alive by adding it to a container.
71  /// </summary>
72  /// <typeparam name="T">A component</typeparam>
73  /// <param name="thisArg">The component to keep alive.</param>
74  /// <param name="container">The container that will keep a reference to the component.</param>
75  /// <returns>The same component instance</returns>
76  public static T DisposeBy<T>(this T thisArg, ICollectorHolder container) where T : IDisposable
77  {
78  if (ReferenceEquals(thisArg, null))
79  return thisArg;
80  return container.Collector.Add(thisArg);
81  }
82  }
83 }
Base interface for all referencable objects.
Definition: IReferencable.cs:8
Interface ICollectorHolder for an instance that can collect other instance.
Definition: ICollector.cs:8
A struct to dispose IDisposable, IReferencable instances and allocated unmanaged memory.