1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21// STL
22#include <unordered_map>
23
24#include "types.h"
25
26namespace love
27{
28
29static std::unordered_map<std::string, Type*> types;
30
31Type::Type(const char *name, Type *parent)
32 : name(name)
33 , parent(parent)
34 , id(0)
35 , inited(false)
36{
37}
38
39void Type::init()
40{
41 static uint32 nextId = 1;
42
43 // Make sure we don't init twice, that would be bad
44 if (inited)
45 return;
46
47 // Note: we add it here, not in the constructor, because some Types can get initialized before the map!
48 types[name] = this;
49 id = nextId++;
50 bits[id] = true;
51 inited = true;
52
53 if (!parent)
54 return;
55 if (!parent->inited)
56 parent->init();
57 bits |= parent->bits;
58}
59
60uint32 Type::getId()
61{
62 if (!inited)
63 init();
64 return id;
65}
66
67const char *Type::getName() const
68{
69 return name;
70}
71
72Type *Type::byName(const char *name)
73{
74 auto pos = types.find(name);
75 if (pos == types.end())
76 return nullptr;
77 return pos->second;
78}
79
80} // love
81