Variant.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Copyright (c) 2006-2021 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. #ifndef LOVE_VARIANT_H
  21. #define LOVE_VARIANT_H
  22. #include "common/config.h"
  23. #include "common/Object.h"
  24. #include "common/int.h"
  25. #include <cstring>
  26. #include <string>
  27. #include <vector>
  28. namespace love
  29. {
  30. class LOVE_EXPORT Variant
  31. {
  32. public:
  33. static const int MAX_SMALL_STRING_LENGTH = 15;
  34. enum Type
  35. {
  36. UNKNOWN = 0,
  37. BOOLEAN,
  38. NUMBER,
  39. STRING,
  40. SMALLSTRING,
  41. LUSERDATA,
  42. LOVEOBJECT,
  43. NIL,
  44. TABLE
  45. };
  46. class SharedString : public love::Object
  47. {
  48. public:
  49. SharedString(const char *string, size_t len)
  50. : len(len)
  51. {
  52. str = new char[len+1];
  53. str[len] = '\0';
  54. memcpy(str, string, len);
  55. }
  56. virtual ~SharedString() { delete[] str; }
  57. char *str;
  58. size_t len;
  59. };
  60. class SharedTable : public love::Object
  61. {
  62. public:
  63. SharedTable() {}
  64. virtual ~SharedTable() {}
  65. std::vector<std::pair<Variant, Variant>> pairs;
  66. };
  67. union Data
  68. {
  69. bool boolean;
  70. double number;
  71. SharedString *string;
  72. void *userdata;
  73. Proxy objectproxy;
  74. SharedTable *table;
  75. struct
  76. {
  77. char str[MAX_SMALL_STRING_LENGTH];
  78. uint8 len;
  79. } smallstring;
  80. };
  81. Variant();
  82. Variant(bool boolean);
  83. Variant(double number);
  84. Variant(const char *str, size_t len);
  85. Variant(const std::string &str);
  86. Variant(void *lightuserdata);
  87. Variant(love::Type *type, love::Object *object);
  88. Variant(SharedTable *table);
  89. Variant(const Variant &v);
  90. Variant(Variant &&v);
  91. ~Variant();
  92. Variant &operator = (const Variant &v);
  93. Type getType() const { return type; }
  94. const Data &getData() const { return data; }
  95. static Variant unknown() { return Variant(UNKNOWN); }
  96. private:
  97. Variant(Type vtype);
  98. Type type;
  99. Data data;
  100. }; // Variant
  101. } // love
  102. #endif // LOVE_VARIANT_H