aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/IdDictionary.cs
blob: 56ffcd0c3d04a82d2045d206abb2fb258680b406 (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
74
75
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

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

        public ICollection<object> Values => _objs.Values;

        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 dataObject) && dataObject is T data)
            {
                return data;
            }

            return default;
        }

        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;
        }
    }
}