Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ComponentBase.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.Threading;
5 using SiliconStudio.Core.Diagnostics;
6 
7 namespace SiliconStudio.Core
8 {
9  /// <summary>
10  /// Base class for a framework component.
11  /// </summary>
12  public abstract class ComponentBase : IComponent, IDisposable, ICollectorHolder, IReferencable
13  {
14  private static long globalCounterId;
15  private int counter = 1;
16  private ObjectCollector collector;
17  private string name;
18 
19  /// <summary>
20  /// Initializes a new instance of the <see cref="ComponentBase"/> class.
21  /// </summary>
22  protected ComponentBase()
23  : this(null)
24  {
25  }
26 
27  /// <summary>
28  /// Initializes a new instance of the <see cref="ComponentBase"/> class.
29  /// </summary>
30  /// <param name="name">The name attached to this component</param>
31  protected ComponentBase(string name)
32  {
33  Name = name ?? GetType().Name;
34  Id = Interlocked.Increment(ref globalCounterId);
35  collector = new ObjectCollector();
36 
37  // Track this component
38  if (ComponentTracker.Enable) ComponentTracker.Track(this);
39  Tags = new PropertyContainer(this);
40  }
41 
42  public long Id { get; private set; }
43 
44  /// <inheritdoc/>
45  int IReferencable.ReferenceCount { get { return counter; } }
46 
47  /// <summary>
48  /// Gets or sets the name of this component.
49  /// </summary>
50  /// <value>
51  /// The name.
52  /// </value>
53  public string Name
54  {
55  get
56  {
57  return name;
58  }
59  set
60  {
61  if (value == name) return;
62 
63  name = value;
64  OnNameChanged();
65  }
66  }
67 
68  /// <summary>
69  /// Called when <see cref="Name"/> property was changed.
70  /// </summary>
71  protected virtual void OnNameChanged()
72  {
73  }
74 
75  /// <inheritdoc/>
76  int IReferencable.AddReference()
77  {
79  ComponentTracker.NotifyEvent(this, ComponentEventType.AddReference);
80 
81  int newCounter = Interlocked.Increment(ref counter);
82  if (newCounter <= 1) throw new InvalidOperationException(FrameworkResources.AddReferenceError);
83  return newCounter;
84  }
85 
86  /// <inheritdoc/>
87  int IReferencable.Release()
88  {
90  ComponentTracker.NotifyEvent(this, ComponentEventType.Release);
91 
92  int newCounter = Interlocked.Decrement(ref counter);
93  if (newCounter == 0)
94  {
95  Destroy();
96  }
97  else if (newCounter < 0)
98  {
99  throw new InvalidOperationException(FrameworkResources.ReleaseReferenceError);
100  }
101  return newCounter;
102  }
103 
104  public void Dispose()
105  {
106  int newcounter = Interlocked.Decrement(ref counter);
107  if (newcounter != 0)
108  throw new InvalidOperationException(FrameworkResources.ReleaseReferenceError);
109  Destroy();
110 
111  IsDisposed = true;
112  }
113 
114  /// <summary>
115  /// Has the component been disposed or not yet.
116  /// </summary>
117  public bool IsDisposed { get; private set; }
118 
119  /// <summary>
120  /// Disposes of object resources.
121  /// </summary>
122  protected virtual void Destroy()
123  {
124  // Untrack this object
125  if (ComponentTracker.Enable) ComponentTracker.UnTrack(this);
126 
127  collector.Dispose();
128  }
129 
130  ObjectCollector ICollectorHolder.Collector
131  {
132  get
133  {
134  collector.EnsureValid();
135  return collector;
136  }
137  }
138 
139  public override string ToString()
140  {
141  return string.Format("{0}: {1}", this.GetType().Name, Name);
142  }
143 
144  /// <summary>
145  /// Gets the attached properties to this component.
146  /// </summary>
148  }
149 }
virtual void OnNameChanged()
Called when Name property was changed.
Base interface for all referencable objects.
Definition: IReferencable.cs:8
ComponentBase()
Initializes a new instance of the ComponentBase class.
ComponentBase.Destroy() event.
Interface ICollectorHolder for an instance that can collect other instance.
Definition: ICollector.cs:8
Represents a container that can hold properties, lightweight to embed (lazy initialized).
PropertyContainer Tags
Gets the attached properties to this component.
ComponentBase(string name)
Initializes a new instance of the ComponentBase class.
Base class for a framework component.
static readonly bool EnableEvents
Enable ComponentTracker event tracking system.
A struct to dispose IDisposable, IReferencable instances and allocated unmanaged memory.
virtual void Destroy()
Disposes of object resources.
Base interface for all components.
Definition: IComponent.cs:8
static readonly bool Enable
Enable ComponentTracker.