Variant.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <memory>
  21. #include "Variant.h"
  22. #include "common/StringMap.h"
  23. namespace love
  24. {
  25. Variant::Variant(Type vtype)
  26. : type(vtype)
  27. {}
  28. Variant::Variant()
  29. : type(NIL)
  30. {}
  31. Variant::Variant(bool boolean)
  32. : type(BOOLEAN)
  33. {
  34. data.boolean = boolean;
  35. }
  36. Variant::Variant(double number)
  37. : type(NUMBER)
  38. {
  39. data.number = number;
  40. }
  41. Variant::Variant(const char *str, size_t len)
  42. {
  43. if (len <= MAX_SMALL_STRING_LENGTH)
  44. {
  45. type = SMALLSTRING;
  46. memcpy(data.smallstring.str, str, len);
  47. data.smallstring.len = (uint8) len;
  48. }
  49. else
  50. {
  51. type = STRING;
  52. data.string = new SharedString(str, len);
  53. }
  54. }
  55. Variant::Variant(const std::string &str)
  56. : Variant(str.c_str(), str.length())
  57. {
  58. }
  59. Variant::Variant(void *lightuserdata)
  60. : type(LUSERDATA)
  61. {
  62. data.userdata = lightuserdata;
  63. }
  64. Variant::Variant(love::Type *lovetype, love::Object *object)
  65. : type(LOVEOBJECT)
  66. {
  67. data.objectproxy.type = lovetype;
  68. data.objectproxy.object = object;
  69. if (data.objectproxy.object != nullptr)
  70. data.objectproxy.object->retain();
  71. }
  72. // Variant gets ownership of the vector.
  73. Variant::Variant(SharedTable *table)
  74. : type(TABLE)
  75. {
  76. data.table = table;
  77. }
  78. Variant::Variant(const Variant &v)
  79. : type(v.type)
  80. , data(v.data)
  81. {
  82. if (type == STRING)
  83. data.string->retain();
  84. else if (type == LOVEOBJECT && data.objectproxy.object != nullptr)
  85. data.objectproxy.object->retain();
  86. else if (type == TABLE)
  87. data.table->retain();
  88. }
  89. Variant::Variant(Variant &&v)
  90. : type(std::move(v.type))
  91. , data(std::move(v.data))
  92. {
  93. v.type = NIL;
  94. }
  95. Variant::~Variant()
  96. {
  97. if (type == STRING)
  98. data.string->release();
  99. else if (type == LOVEOBJECT && data.objectproxy.object != nullptr)
  100. data.objectproxy.object->release();
  101. else if (type == TABLE)
  102. data.table->release();
  103. }
  104. Variant &Variant::operator = (const Variant &v)
  105. {
  106. if (v.type == STRING)
  107. v.data.string->retain();
  108. else if (v.type == LOVEOBJECT && v.data.objectproxy.object != nullptr)
  109. v.data.objectproxy.object->retain();
  110. else if (v.type == TABLE)
  111. v.data.table->retain();
  112. if (type == STRING)
  113. data.string->release();
  114. else if (type == LOVEOBJECT && data.objectproxy.object != nullptr)
  115. data.objectproxy.object->release();
  116. else if (type == TABLE)
  117. data.table->release();
  118. type = v.type;
  119. data = v.data;
  120. return *this;
  121. }
  122. } // love