Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GuidContainer.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.Linq;
6 
7 namespace SiliconStudio.Quantum
8 {
9  /// <summary>
10  /// Handles <see cref="Guid"/> generation and storage for model nodes.
11  /// </summary>
12  /// <remarks>This class will hold references on objects until they are unregistered or until the container is cleared.</remarks>
13  /// <remarks>This class is thread safe.</remarks>
15  {
16  private readonly Dictionary<object, Guid> objectGuids = new Dictionary<object, Guid>();
17 
18  /// <inheritdoc/>
19  public Guid GetOrCreateGuid(object obj)
20  {
21  if (obj == null) return Guid.NewGuid();
22 
23  lock (objectGuids)
24  {
25  Guid guid;
26  if (!objectGuids.TryGetValue(obj, out guid))
27  {
28  objectGuids.Add(obj, guid = Guid.NewGuid());
29  }
30  return guid;
31  }
32  }
33 
34  /// <inheritdoc/>
35  public Guid GetGuid(object obj)
36  {
37  lock (objectGuids)
38  {
39  Guid guid;
40  return obj != null && objectGuids.TryGetValue(obj, out guid) ? guid : Guid.Empty;
41  }
42  }
43 
44  /// <inheritdoc/>
45  public void RegisterGuid(Guid guid, object obj)
46  {
47  if (obj == null) throw new ArgumentNullException("obj");
48 
49  lock (objectGuids)
50  {
51  objectGuids[obj] = guid;
52  }
53  }
54 
55  /// <inheritdoc/>
56  public bool UnregisterGuid(Guid guid)
57  {
58  lock (objectGuids)
59  {
60  object key = objectGuids.SingleOrDefault(x => x.Value == guid).Key;
61  return key != null && objectGuids.Remove(key);
62  }
63  }
64 
65  /// <inheritdoc/>
66  public void Clear()
67  {
68  lock (objectGuids)
69  {
70  objectGuids.Clear();
71  }
72  }
73  }
74 }
bool UnregisterGuid(Guid guid)
Removes a Guid that was previously registered. The Guid to remove.true if a Guid has been actually re...
void Clear()
Clear the IGuidContainer, removing everything it references.
Base interface for Guid containers, object that can store a unique identifier for a collection of obj...
Handles Guid generation and storage for model nodes.
Guid GetGuid(object obj)
Gets the Guid for a given object, if available. The object.The Guid associated to the given object...
Guid GetOrCreateGuid(object obj)
Gets or or create a Guid for a given object. If the object is null, a new Guid will be returned...
void RegisterGuid(Guid guid, object obj)
Register the given Guid to the given object. If a Guid is already associated to this object...