Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ComponentTracker.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 using System.Collections.Generic;
5 using System.Diagnostics;
6 using System.Linq;
7 using System.Text;
8 
9 namespace SiliconStudio.Core.Diagnostics
10 {
11  /// <summary>
12  /// Track all allocated objects.
13  /// </summary>
14  public class ComponentTracker
15  {
16  /// <summary>
17  /// Enable ComponentTracker.
18  /// </summary>
19  public static readonly bool Enable = false;
20 
21  /// <summary>
22  /// Enable ComponentTracker event tracking system.
23  /// </summary>
24  public static readonly bool EnableEvents = false;
25 
26  private static readonly Dictionary<long, ComponentReference> ObjectReferences = new Dictionary<long, ComponentReference>();
27 
28  /// <summary>
29  /// Tracks the specified component object.
30  /// </summary>
31  /// <param name="component">The component object.</param>
32  public static void Track(IComponent component)
33  {
34  if (component == null)
35  return;
36  lock (ObjectReferences)
37  {
38  ComponentReference componentReference;
39 
40  // Object is already tracked
41  if (!ObjectReferences.TryGetValue(component.Id, out componentReference))
42  {
43  ObjectReferences.Add(component.Id, new ComponentReference(DateTime.Now, component));
44  }
45  }
46  if (Enable && EnableEvents)
47  NotifyEvent(component, ComponentEventType.Instantiate);
48  }
49 
50  /// <summary>
51  /// Finds a component reference from a specified id.
52  /// </summary>
53  /// <param name="id">The id of the component</param>
54  /// <returns>A component reference</returns>
55  public static ComponentReference Find(long id)
56  {
57  lock (ObjectReferences)
58  {
59  ComponentReference componentReference;
60 
61  // Object is already tracked
62  if (ObjectReferences.TryGetValue(id, out componentReference))
63  return componentReference;
64  }
65  return null;
66  }
67 
68  /// <summary>
69  /// Finds a component reference for a specific component.
70  /// </summary>
71  /// <param name="component">The component instance.</param>
72  /// <returns>A component reference</returns>
73  public static ComponentReference Find(IComponent component)
74  {
75  return Find(component.Id);
76  }
77 
78  /// <summary>
79  /// Untracks the specified component.
80  /// </summary>
81  /// <param name="component">The COM object.</param>
82  public static void UnTrack(IComponent component)
83  {
84  if (component == null)
85  return;
86 
87  if (Enable && EnableEvents)
88  {
89  NotifyEvent(component, ComponentEventType.Destroy);
90  }
91  else
92  {
93  // Only remove it if we don't track events.
94  lock (ObjectReferences)
95  {
96  ObjectReferences.Remove(component.Id);
97  }
98  }
99  }
100 
101  /// <summary>
102  /// Should be called everytime an event happens for a IComponent.
103  /// </summary>
104  /// <param name="component">The component.</param>
105  /// <param name="eventType">Type of the event.</param>
106  public static void NotifyEvent(IComponent component, ComponentEventType eventType)
107  {
108  ComponentReference componentReference;
109 
110  // Object is already tracked
111  if (ObjectReferences.TryGetValue(component.Id, out componentReference))
112  {
113  componentReference.Events.Add(new ComponentEventInfo(eventType));
114  if (eventType == ComponentEventType.Destroy)
115  {
116  componentReference.IsDestroyed = true;
117  }
118  }
119  }
120 
121  /// <summary>
122  /// Reports all COM and IReferencable object that are active and not yet disposed.
123  /// </summary>
124  /// <returns>The list of active objects.</returns>
125  public static List<ComponentReference> FindActiveObjects()
126  {
127  var activeObjects = new List<ComponentReference>();
128  lock (ObjectReferences)
129  {
130  activeObjects.AddRange(ObjectReferences.Values.Where(x => !x.IsDestroyed));
131  }
132 
133  return activeObjects;
134  }
135 
136  /// <summary>
137  /// Reports all COM object that are active and not yet disposed.
138  /// </summary>
139  /// <returns>The report about active objects.</returns>
140  public static string ReportActiveObjects()
141  {
142  var text = new StringBuilder();
143  foreach (var findActiveObject in FindActiveObjects())
144  {
145  var findActiveObjectStr = findActiveObject.ToString();
146  if (!string.IsNullOrEmpty(findActiveObjectStr))
147  text.AppendLine(findActiveObjectStr);
148  }
149  return text.ToString();
150  }
151  }
152 }
ComponentEventType
TODO: Update summary.
static void NotifyEvent(IComponent component, ComponentEventType eventType)
Should be called everytime an event happens for a IComponent.
long Id
Gets the id of this component.
Definition: IComponent.cs:13
static ComponentReference Find(long id)
Finds a component reference from a specified id.
Contains information about a tracked component.
static string ReportActiveObjects()
Reports all COM object that are active and not yet disposed.
static ComponentReference Find(IComponent component)
Finds a component reference for a specific component.
static List< ComponentReference > FindActiveObjects()
Reports all COM and IReferencable object that are active and not yet disposed.
static void Track(IComponent component)
Tracks the specified component object.
Base interface for all components.
Definition: IComponent.cs:8
static void UnTrack(IComponent component)
Untracks the specified component.
Contains information about a AddReference/Release event.