You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

117 regels
1.9 KiB

  1. //
  2. // Deus Hax (working title)
  3. // Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
  4. //
  5. #if defined HAVE_CONFIG_H
  6. # include "config.h"
  7. #endif
  8. #include <cstring>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include "core.h"
  12. #if defined WIN32
  13. # define strcasecmp _stricmp
  14. #endif
  15. /*
  16. * Dict implementation class
  17. */
  18. class DictData
  19. {
  20. friend class Dict;
  21. public:
  22. DictData() :
  23. entities(0),
  24. nentities(0)
  25. {
  26. /* Nothing to do */
  27. }
  28. ~DictData()
  29. {
  30. if (nentities)
  31. fprintf(stderr, "ERROR: still %i entities in dict\n", nentities);
  32. free(entities);
  33. }
  34. private:
  35. Entity **entities;
  36. int nentities;
  37. };
  38. /*
  39. * Public Dict class
  40. */
  41. Dict::Dict()
  42. {
  43. data = new DictData();
  44. }
  45. Dict::~Dict()
  46. {
  47. delete data;
  48. }
  49. int Dict::MakeSlot(char const *name)
  50. {
  51. int id, empty = -1;
  52. /* If the entry is already registered, remember its ID. Look for an
  53. * empty slot at the same time. */
  54. for (id = 0; id < data->nentities; id++)
  55. {
  56. Entity *e = data->entities[id];
  57. if (!e)
  58. empty = id;
  59. else if (!strcasecmp(name, e->GetName()))
  60. break;
  61. }
  62. /* If this is a new entry, create a new slot for it. */
  63. if (id == data->nentities)
  64. {
  65. if (empty == -1)
  66. {
  67. empty = data->nentities++;
  68. data->entities = (Entity **)realloc(data->entities,
  69. data->nentities * sizeof(Entity *));
  70. }
  71. data->entities[empty] = NULL;
  72. id = empty;
  73. }
  74. else
  75. {
  76. Ticker::Ref(data->entities[id]);
  77. }
  78. return id;
  79. }
  80. void Dict::RemoveSlot(int id)
  81. {
  82. if (Ticker::Unref(data->entities[id]) == 0)
  83. data->entities[id] = NULL;
  84. data->nentities--;
  85. }
  86. void Dict::SetEntity(int id, Entity *entity)
  87. {
  88. Ticker::Ref(entity);
  89. data->entities[id] = entity;
  90. }
  91. Entity *Dict::GetEntity(int id)
  92. {
  93. return data->entities[id];
  94. }