Explicitly handle initing from empty arrays, and assign NULL or ""

This commit is contained in:
baldurk
2014-08-12 16:46:35 +01:00
parent 0c52714def
commit fceabf7948
3 changed files with 71 additions and 22 deletions
+40 -12
View File
@@ -31,9 +31,16 @@ wstr &wstr::operator =(const std::wstring &in)
{
Delete();
count = (int32_t)in.size();
elems = (wchar_t*)allocate(sizeof(wchar_t)*(count+1));
memcpy(elems, &in[0], sizeof(wchar_t)*in.size());
elems[count] = 0;
if(count == 0)
{
elems = L"";
}
else
{
elems = (wchar_t*)allocate(sizeof(wchar_t)*(count+1));
memcpy(elems, &in[0], sizeof(wchar_t)*in.size());
elems[count] = 0;
}
return *this;
}
@@ -41,9 +48,16 @@ str &str::operator =(const std::string &in)
{
Delete();
count = (int32_t)in.size();
elems = (char*)allocate(sizeof(char)*(count+1));
memcpy(elems, &in[0], sizeof(char)*in.size());
elems[count] = 0;
if(count == 0)
{
elems = "";
}
else
{
elems = (char*)allocate(sizeof(char)*(count+1));
memcpy(elems, &in[0], sizeof(char)*in.size());
elems[count] = 0;
}
return *this;
}
@@ -51,9 +65,16 @@ wstr &wstr::operator =(const wchar_t *const in)
{
Delete();
count = (int32_t)wcslen(in);
elems = (wchar_t*)allocate(sizeof(wchar_t)*(count+1));
memcpy(elems, &in[0], sizeof(wchar_t)*count);
elems[count] = 0;
if(count == 0)
{
elems = L"";
}
else
{
elems = (wchar_t*)allocate(sizeof(wchar_t)*(count+1));
memcpy(elems, &in[0], sizeof(wchar_t)*count);
elems[count] = 0;
}
return *this;
}
@@ -61,9 +82,16 @@ str &str::operator =(const char *const in)
{
Delete();
count = (int32_t)strlen(in);
elems = (char*)allocate(sizeof(char)*(count+1));
memcpy(elems, &in[0], sizeof(char)*count);
elems[count] = 0;
if(count == 0)
{
elems = "";
}
else
{
elems = (char*)allocate(sizeof(char)*(count+1));
memcpy(elems, &in[0], sizeof(char)*count);
elems[count] = 0;
}
return *this;
}
+12 -5
View File
@@ -71,9 +71,16 @@ struct array
{
Delete();
count = (int32_t)in.size();
elems = (T*)allocate(sizeof(T)*count);
for(int32_t i=0; i < count; i++)
new (elems+i) T(in[i]);
if(count == 0)
{
elems = 0;
}
else
{
elems = (T*)allocate(sizeof(T)*count);
for(int32_t i=0; i < count; i++)
new (elems+i) T(in[i]);
}
return *this;
}
@@ -117,7 +124,7 @@ struct str : public rdctype::array<char>
count = o.count;
if(count == 0)
{
elems = NULL;
elems = "";
}
else
{
@@ -146,7 +153,7 @@ struct wstr : public rdctype::array<wchar_t>
count = o.count;
if(count == 0)
{
elems = 0;
elems = L"";
}
else
{
+19 -5
View File
@@ -36,10 +36,17 @@ namespace rdctype
template<typename T>
void create_array(array<T> &ret, size_t count)
{
ret.elems = (T*)ret.allocate(sizeof(T)*count);
ret.count = (int32_t)count;
for(int32_t i=0; i < ret.count; i++)
new (ret.elems+i) T();
if(ret.count == 0)
{
ret.elems = 0;
}
else
{
ret.elems = (T*)ret.allocate(sizeof(T)*count);
for(int32_t i=0; i < ret.count; i++)
new (ret.elems+i) T();
}
}
#pragma warning(pop)
@@ -47,9 +54,16 @@ void create_array(array<T> &ret, size_t count)
template<typename T>
void create_array_uninit(array<T> &ret, size_t count)
{
ret.elems = (T*)ret.allocate(sizeof(T)*count);
ret.count = (int32_t)count;
memset(ret.elems, 0, sizeof(T)*count);
if(ret.count == 0)
{
ret.elems = 0;
}
else
{
ret.elems = (T*)ret.allocate(sizeof(T)*count);
memset(ret.elems, 0, sizeof(T)*count);
}
}
}; // namespace rdctype