added move semantics to Space class

added methods:
Space(Space && space);
Space & operator=(Space && space);
void set(const Space & space);
void set(Space && space);
Space & add(const Space & space);
Space & add(Space && space);
Space & add(const wchar_t * field, const Space & space);
Space & add(const wchar_t * field, Space && space);
Space & add(const std::wstring & field, const Space & space);
Space & add(const std::wstring & field, Space && space);
This commit is contained in:
Tomasz Sowa 2021-05-19 22:24:53 +02:00
parent 430822bad8
commit 604b47db32
2 changed files with 182 additions and 5 deletions

View File

@ -35,11 +35,9 @@
* THE POSSIBILITY OF SUCH DAMAGE.
*/
//#include <cstdlib>
#include <wchar.h>
#include <limits>
#include <utility>
#include "space.h"
//#include "textstream/textstream.h"
#include "utf8/utf8.h"
#include "convert/convert.h"
@ -59,6 +57,13 @@ Space::Space(const Space & space)
copy_from(space);
}
Space::Space(Space && space)
{
initialize();
move_from(std::move(space));
}
Space & Space::operator=(const Space & space)
{
copy_from(space);
@ -66,6 +71,13 @@ Space & Space::operator=(const Space & space)
}
Space & Space::operator=(Space && space)
{
move_from(std::move(space));
return *this;
}
Space::~Space()
{
remove_value();
@ -337,6 +349,10 @@ void Space::set(const std::wstring & str)
value.value_wstring = str;
}
void Space::set(const Space & space)
{
copy_from(space);
}
void Space::set(const Space * space)
{
@ -350,6 +366,12 @@ void Space::set(const Space * space)
}
}
void Space::set(Space && space)
{
move_from(std::move(space));
}
Space & Space::add(bool val)
{
@ -426,11 +448,25 @@ Space & Space::add(const std::wstring & val)
return add_generic(val);
}
Space & Space::add(const Space & space)
{
return add_generic(space);
}
Space & Space::add(const Space * space)
{
return add_generic(space);
}
Space & Space::add(Space && space)
{
initialize_value_table_if_needed();
Space * new_space = new Space(std::move(space));
value.value_table.push_back(new_space);
return *value.value_table.back();
}
Space & Space::add_empty_space()
{
return add_generic(static_cast<Space*>(nullptr));
@ -514,11 +550,26 @@ Space & Space::add(const wchar_t * field, const std::wstring & val)
return add_generic(field, val);
}
Space & Space::add(const wchar_t * field, const Space & space)
{
return add_generic(field, space);
}
Space & Space::add(const wchar_t * field, const Space * space)
{
return add_generic(field, space);
}
Space & Space::add(const wchar_t * field, Space && space)
{
initialize_value_object_if_needed();
auto insert_res = value.value_object.insert(std::make_pair(field, nullptr));
insert_res.first->second = new Space(std::move(space));
return *(insert_res.first->second);
}
Space & Space::add_empty_space(const wchar_t * field)
{
return add_generic(field, static_cast<Space*>(nullptr));
@ -602,11 +653,21 @@ Space & Space::add(const std::wstring & field, const std::wstring & val)
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, const Space & space)
{
return add_generic(field, space);
}
Space & Space::add(const std::wstring & field, const Space * space)
{
return add_generic(field, space);
}
Space & Space::add(const std::wstring & field, Space && space)
{
return add(field.c_str(), std::move(space));
}
Space & Space::add_empty_space(const std::wstring & field)
{
return add_generic(field, static_cast<Space*>(nullptr));
@ -2034,6 +2095,45 @@ void Space::copy_value_table(const Value & value_from)
}
void Space::move_value_from(Space && space)
{
switch(space.type)
{
case type_string:
initialize_value_string_if_needed(std::move(space.value.value_string));
break;
case type_wstring:
initialize_value_wstring_if_needed(std::move(space.value.value_wstring));
break;
case type_object:
initialize_value_object_if_needed(std::move(space.value.value_object));
break;
case type_table:
initialize_value_table_if_needed(std::move(space.value.value_table));
break;
default:
copy_value_from(space);
break;
}
}
void Space::move_from(Space && space)
{
move_value_from(std::move(space));
space.type = Type::type_null;
std::swap(child_spaces, space.child_spaces);
std::swap(name, space.name);
}
void Space::initialize()
{
type = type_null;
@ -2106,6 +2206,22 @@ void Space::initialize_value_string_if_needed()
}
void Space::initialize_value_string_if_needed(std::string && str)
{
if( type != type_string )
{
remove_value();
new (&value) std::string(std::move(str));
type = type_string;
}
else
{
value.value_string = std::move(str);
}
}
void Space::initialize_value_wstring_if_needed()
{
if( type != type_wstring )
@ -2117,6 +2233,20 @@ void Space::initialize_value_wstring_if_needed()
}
void Space::initialize_value_wstring_if_needed(std::wstring && str)
{
if( type != type_wstring )
{
remove_value();
new (&value) std::wstring(std::move(str));
type = type_wstring;
}
else
{
value.value_wstring = std::move(str);
}
}
void Space::initialize_value_object_if_needed()
{
if( type != type_object )
@ -2127,6 +2257,20 @@ void Space::initialize_value_object_if_needed()
}
}
void Space::initialize_value_object_if_needed(ObjectType && obj)
{
if( type != type_object )
{
remove_value();
new (&value) ObjectType(std::move(obj));
type = type_object;
}
else
{
value.value_object = std::move(obj);
}
}
void Space::initialize_value_table_if_needed()
{
@ -2139,6 +2283,21 @@ void Space::initialize_value_table_if_needed()
}
void Space::initialize_value_table_if_needed(TableType && tab)
{
if( type != type_table )
{
remove_value();
new (&value) TableType(std::move(tab));
type = type_table;
}
else
{
value.value_table = std::move(tab);
}
}
void Space::initialize_child_spaces_if_needed()
{
if( child_spaces == nullptr )
@ -2264,7 +2423,6 @@ void Space::remove_space_name()
} // namespace

View File

@ -186,8 +186,9 @@ public:
Space();
Space(const Space & space);
Space(Space && space);
Space & operator=(const Space & space);
// add move cctor
Space & operator=(Space && space);
~Space();
Space(bool val);
@ -232,7 +233,9 @@ public:
void set(const wchar_t * str);
void set(const std::string & str);
void set(const std::wstring & str);
void set(const Space & space);
void set(const Space * space);
void set(Space && space);
// add a value to the table, change to table if needed, return the reference to the new inserted item
@ -251,7 +254,9 @@ public:
Space & add(const wchar_t * val);
Space & add(const std::string & val);
Space & add(const std::wstring & val);
Space & add(const Space & space);
Space & add(const Space * space);
Space & add(Space && space);
Space & add_empty_space(); // IMPROVEME rename me to something better
@ -272,7 +277,9 @@ public:
Space & add(const wchar_t * field, const wchar_t * val);
Space & add(const wchar_t * field, const std::string & val);
Space & add(const wchar_t * field, const std::wstring & val);
Space & add(const wchar_t * field, const Space & space);
Space & add(const wchar_t * field, const Space * space);
Space & add(const wchar_t * field, Space && space);
Space & add_empty_space(const wchar_t * field); // IMPROVEME rename me to something better
Space & add(const std::wstring & field, bool val);
@ -290,7 +297,9 @@ public:
Space & add(const std::wstring & field, const wchar_t * val);
Space & add(const std::wstring & field, const std::string & val);
Space & add(const std::wstring & field, const std::wstring & val);
Space & add(const std::wstring & field, const Space & space);
Space & add(const std::wstring & field, const Space * space);
Space & add(const std::wstring & field, Space && space);
Space & add_empty_space(const std::wstring & field); // IMPROVEME rename me to something better
@ -1285,6 +1294,9 @@ protected:
void copy_value_object(const Value & value_from);
void copy_value_table(const Value & value_from);
void move_value_from(Space && space);
void move_from(Space && space);
void initialize();
void initialize_value_null_if_needed();
void initialize_value_bool_if_needed();
@ -1292,9 +1304,13 @@ protected:
void initialize_value_float_if_needed();
void initialize_value_double_if_needed();
void initialize_value_string_if_needed();
void initialize_value_string_if_needed(std::string && str);
void initialize_value_wstring_if_needed();
void initialize_value_wstring_if_needed(std::wstring && str);
void initialize_value_object_if_needed();
void initialize_value_object_if_needed(ObjectType && obj);
void initialize_value_table_if_needed();
void initialize_value_table_if_needed(TableType && tab);
void initialize_child_spaces_if_needed();
void initialize_space_name_if_needed();
@ -1309,6 +1325,9 @@ protected:
};
} // namespace