aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/IdDictionary.cs
blob: 03d15498f65834ebd9306de07fc2e6b645b99f36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace Ryujinx.HLE.HOS
{
    class IdDictionary
    {
        private ConcurrentDictionary<int, object> Objs;

        public IdDictionary()
        {
            Objs = new ConcurrentDictionary<int, object>();
        }

        public bool Add(int Id, object Data)
        {
            return Objs.TryAdd(Id, Data);
        }

        public int Add(object Data)
        {
            for (int Id = 1; Id < int.MaxValue; Id++)
            {
                if (Objs.TryAdd(Id, Data))
                {
                    return Id;
                }
            }

            throw new InvalidOperationException();
        }

        public object GetData(int Id)
        {
            if (Objs.TryGetValue(Id, out object Data))
            {
                return Data;
            }

            return null;
        }

        public T GetData<T>(int Id)
        {
            if (Objs.TryGetValue(Id, out object Data) && Data is T)
            {
                return (T)Data;
            }

            return default(T);
        }

        public object Delete(int Id)
        {
            if (Objs.TryRemove(Id, out object Obj))
            {
                return Obj;
            }

            return null;
        }

        public ICollection<object> Clear()
        {
            ICollection<object> Values = Objs.Values;

            Objs.Clear();

            return Values;
        }
    }
}