Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ComponentReference.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.Globalization;
7 using System.Text;
8 
9 namespace SiliconStudio.Core.Diagnostics
10 {
11  /// <summary>
12  /// Contains information about a tracked component.
13  /// </summary>
14  public class ComponentReference
15  {
16 
17  /// <summary>
18  /// Initializes a new instance of the <see cref="ComponentReference"/> class.
19  /// </summary>
20  /// <param name="creationTime">The creation time.</param>
21  /// <param name="component">The component to track.</param>
22  public ComponentReference(DateTime creationTime, IComponent component)
23  {
24  CreationTime = creationTime;
25  // Creates a short week reference to the Component
26  Object = new WeakReference(component);
27  Events = new List<ComponentEventInfo>();
28 
29  try
30  {
31  throw new Exception();
32  }
33  catch (Exception ex)
34  {
35  StackTrace = ex.StackTrace;
36  }
37  }
38 
39  /// <summary>
40  /// Gets the stack trace when the track object was created.
41  /// </summary>
42  /// <value>The stack trace.</value>
43  public string StackTrace { get; private set; }
44 
45  public List<ComponentEventInfo> Events { get; private set; }
46 
47  /// <summary>
48  /// Gets a value indicating whether the tracked object has been destroyed when tracking events.
49  /// </summary>
50  /// <value>
51  /// <c>true</c> if this instance is destroyed; otherwise, <c>false</c>.
52  /// </value>
53  public bool IsDestroyed { get; internal set; }
54 
55  /// <summary>
56  /// Gets the time the object was created.
57  /// </summary>
58  /// <value>The creation time.</value>
59  public DateTime CreationTime { get; private set; }
60 
61  /// <summary>
62  /// Gets a weak reference to the tracked object.
63  /// </summary>
64  /// <value>The weak reference to the tracked object.</value>
65  public WeakReference Object { get; private set; }
66 
67  /// <summary>
68  /// Gets a value indicating whether the tracked object is alive.
69  /// </summary>
70  /// <value><c>true</c> if tracked object is alive; otherwise, <c>false</c>.</value>
71  public bool IsAlive
72  {
73  get { return this.Object.IsAlive; }
74  }
75 
76  /// <summary>
77  /// Returns a <see cref="System.String"/> that represents this instance.
78  /// </summary>
79  /// <returns>
80  /// A <see cref="System.String"/> that represents this instance.
81  /// </returns>
82  public override string ToString()
83  {
84  var component = this.Object.Target as IComponent;
85  if (component == null)
86  return string.Empty;
87 
88  var builder = new StringBuilder();
89  builder.AppendFormat(CultureInfo.InvariantCulture, "Active component Id: [{0}] Name: [{1}] Class: [{2}] Time [{3}] Stack: {4}", component.Id, component.Name, component.GetType().FullName, CreationTime, StackTrace).AppendLine();
90  return builder.ToString();
91  }
92  }
93 }
Contains information about a tracked component.
ComponentReference(DateTime creationTime, IComponent component)
Initializes a new instance of the ComponentReference class.
override string ToString()
Returns a System.String that represents this instance.
Base interface for all components.
Definition: IComponent.cs:8