types.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Copyright (c) 2006-2022 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. // STL
  21. #include <unordered_map>
  22. #include "types.h"
  23. namespace love
  24. {
  25. static std::unordered_map<std::string, Type*> types;
  26. Type::Type(const char *name, Type *parent)
  27. : name(name)
  28. , parent(parent)
  29. , id(0)
  30. , inited(false)
  31. {
  32. }
  33. void Type::init()
  34. {
  35. static uint32 nextId = 1;
  36. // Make sure we don't init twice, that would be bad
  37. if (inited)
  38. return;
  39. // Note: we add it here, not in the constructor, because some Types can get initialized before the map!
  40. types[name] = this;
  41. id = nextId++;
  42. bits[id] = true;
  43. inited = true;
  44. if (!parent)
  45. return;
  46. if (!parent->inited)
  47. parent->init();
  48. bits |= parent->bits;
  49. }
  50. uint32 Type::getId()
  51. {
  52. if (!inited)
  53. init();
  54. return id;
  55. }
  56. const char *Type::getName() const
  57. {
  58. return name;
  59. }
  60. Type *Type::byName(const char *name)
  61. {
  62. auto pos = types.find(name);
  63. if (pos == types.end())
  64. return nullptr;
  65. return pos->second;
  66. }
  67. } // love