Add an easier std::string Variant constructor.

This commit is contained in:
Alex Szpakowski
2019-07-20 10:59:12 -03:00
parent 32f381576e
commit b43074efa5
2 changed files with 12 additions and 4 deletions
+8 -3
View File
@@ -58,21 +58,26 @@ Variant::Variant(double number)
data.number = number; data.number = number;
} }
Variant::Variant(const char *string, size_t len) Variant::Variant(const char *str, size_t len)
{ {
if (len <= MAX_SMALL_STRING_LENGTH) if (len <= MAX_SMALL_STRING_LENGTH)
{ {
type = SMALLSTRING; type = SMALLSTRING;
memcpy(data.smallstring.str, string, len); memcpy(data.smallstring.str, str, len);
data.smallstring.len = (uint8) len; data.smallstring.len = (uint8) len;
} }
else else
{ {
type = STRING; type = STRING;
data.string = new SharedString(string, len); data.string = new SharedString(str, len);
} }
} }
Variant::Variant(const std::string &str)
: Variant(str.c_str(), str.length())
{
}
Variant::Variant(void *lightuserdata) Variant::Variant(void *lightuserdata)
: type(LUSERDATA) : type(LUSERDATA)
{ {
+4 -1
View File
@@ -26,6 +26,7 @@
#include "common/int.h" #include "common/int.h"
#include <cstring> #include <cstring>
#include <string>
#include <vector> #include <vector>
#include <set> #include <set>
@@ -59,6 +60,7 @@ public:
: len(len) : len(len)
{ {
str = new char[len+1]; str = new char[len+1];
str[len] = '\0';
memcpy(str, string, len); memcpy(str, string, len);
} }
virtual ~SharedString() { delete[] str; } virtual ~SharedString() { delete[] str; }
@@ -99,7 +101,8 @@ public:
Variant(); Variant();
Variant(bool boolean); Variant(bool boolean);
Variant(double number); Variant(double number);
Variant(const char *string, size_t len); Variant(const char *str, size_t len);
Variant(const std::string &str);
Variant(void *lightuserdata); Variant(void *lightuserdata);
Variant(love::Type *type, love::Object *object); Variant(love::Type *type, love::Object *object);
Variant(std::vector<std::pair<Variant, Variant>> *table); Variant(std::vector<std::pair<Variant, Variant>> *table);