changed the way how child_spaces are created in Space class
- removed child_spaces and name pointers - now a table with child spaces is created under "child_spaces" object field - a name of the child space is stored in "name" field of the child object added methods for manipulating with child spaces: TableType * find_child_space_table() bool child_spaces_empty() size_t child_spaces_size() Space * find_child_space(size_t table_index) Space & add_child_space(const wchar_t * space_name) Space & add_child_space(const std::wstring & space_name) std::wstring * find_child_space_name() std::wstring get_child_space_name() bool is_child_space_name(const wchar_t * name) added additional methods: size_t str_size() size_t wstr_size() size_t object_size() size_t table_size()
This commit is contained in:
parent
c11aa78335
commit
5ce36ea844
|
@ -36,7 +36,6 @@
|
|||
*/
|
||||
|
||||
#include <wchar.h>
|
||||
#include <utility>
|
||||
#include "space.h"
|
||||
#include "utf8/utf8.h"
|
||||
#include "convert/convert.h"
|
||||
|
@ -81,8 +80,6 @@ Space & Space::operator=(Space && space)
|
|||
Space::~Space()
|
||||
{
|
||||
remove_value();
|
||||
remove_child_spaces();
|
||||
remove_space_name();
|
||||
}
|
||||
|
||||
|
||||
|
@ -194,6 +191,54 @@ void Space::clear()
|
|||
|
||||
|
||||
|
||||
size_t Space::str_size() const
|
||||
{
|
||||
if( is_str() )
|
||||
{
|
||||
return value.value_string.size();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
size_t Space::wstr_size() const
|
||||
{
|
||||
if( is_wstr() )
|
||||
{
|
||||
return value.value_wstring.size();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
size_t Space::object_size() const
|
||||
{
|
||||
if( is_object() )
|
||||
{
|
||||
return value.value_object.size();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
size_t Space::table_size() const
|
||||
{
|
||||
if( is_table() )
|
||||
{
|
||||
return value.value_table.size();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Space::set_null()
|
||||
{
|
||||
initialize_value_null_if_needed();
|
||||
|
@ -678,21 +723,6 @@ Space & Space::add_empty_space(const std::wstring & field)
|
|||
|
||||
|
||||
|
||||
Space & Space::add_child_space(const wchar_t * space_name)
|
||||
{
|
||||
initialize_child_spaces_if_needed();
|
||||
|
||||
child_spaces->push_back(new Space());
|
||||
Space * last_space = child_spaces->back();
|
||||
|
||||
last_space->initialize_space_name_if_needed();
|
||||
last_space->name->append(space_name);
|
||||
|
||||
return *last_space;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Space::is_null() const
|
||||
{
|
||||
return type == type_null;
|
||||
|
@ -1884,31 +1914,143 @@ bool Space::has_value(const wchar_t * field, const std::wstring & val) const
|
|||
|
||||
|
||||
|
||||
const Space * Space::find_child_space_const(const wchar_t * name) const
|
||||
{
|
||||
const TableType * child_table = find_child_space_table();
|
||||
|
||||
if( child_table )
|
||||
{
|
||||
for(const Space & space : *child_table)
|
||||
{
|
||||
if( space.is_equal(child_spaces_name, name) )
|
||||
{
|
||||
return &space;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const Space * Space::find_child_space_const(size_t table_index) const
|
||||
{
|
||||
const TableType * child_table = find_child_space_table();
|
||||
|
||||
if( child_table && table_index < child_table->size() )
|
||||
{
|
||||
return (*child_table)[table_index];
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Space::TableType * Space::find_child_space_table()
|
||||
{
|
||||
return get_table(child_spaces_field_table_name);
|
||||
}
|
||||
|
||||
|
||||
const Space::TableType * Space::find_child_space_table() const
|
||||
{
|
||||
return get_table(child_spaces_field_table_name);
|
||||
}
|
||||
|
||||
|
||||
bool Space::child_spaces_empty() const
|
||||
{
|
||||
const TableType * child_table = find_child_space_table();
|
||||
|
||||
if( child_table )
|
||||
{
|
||||
return child_table->empty();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t Space::child_spaces_size() const
|
||||
{
|
||||
const TableType * child_table = find_child_space_table();
|
||||
|
||||
if( child_table )
|
||||
{
|
||||
return child_table->size();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Space * Space::find_child_space(const wchar_t * name)
|
||||
{
|
||||
return find_child_space_generic(name);
|
||||
return const_cast<Space*>(find_child_space_const(name));
|
||||
}
|
||||
|
||||
|
||||
Space * Space::find_child_space(const std::wstring & name)
|
||||
{
|
||||
return find_child_space_generic(name);
|
||||
return find_child_space(name.c_str());
|
||||
}
|
||||
|
||||
|
||||
const Space * Space::find_child_space(const wchar_t * name) const
|
||||
{
|
||||
return find_child_space_generic(name);
|
||||
return find_child_space_const(name);
|
||||
}
|
||||
|
||||
|
||||
const Space * Space::find_child_space(const std::wstring & name) const
|
||||
{
|
||||
return find_child_space_generic(name);
|
||||
return find_child_space(name.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
Space * Space::find_child_space(size_t table_index)
|
||||
{
|
||||
return const_cast<Space*>(find_child_space_const(table_index));
|
||||
}
|
||||
|
||||
const Space * Space::find_child_space(size_t table_index) const
|
||||
{
|
||||
return find_child_space_const(table_index);
|
||||
}
|
||||
|
||||
|
||||
Space & Space::add_child_space()
|
||||
{
|
||||
initialize_child_spaces_if_needed();
|
||||
|
||||
TableType * child_table = find_child_space_table();
|
||||
child_table->push_back(new Space());
|
||||
|
||||
return child_table->back();
|
||||
}
|
||||
|
||||
|
||||
Space & Space::add_child_space(const wchar_t * space_name)
|
||||
{
|
||||
initialize_child_spaces_if_needed();
|
||||
|
||||
TableType * child_table = find_child_space_table();
|
||||
child_table->push_back(new Space());
|
||||
Space * last_space = child_table->back();
|
||||
last_space->add(child_spaces_name, space_name);
|
||||
|
||||
return *last_space;
|
||||
}
|
||||
|
||||
|
||||
Space & Space::add_child_space(const std::wstring & space_name)
|
||||
{
|
||||
return add_child_space(space_name.c_str());
|
||||
}
|
||||
|
||||
|
||||
Space & Space::find_add_child_space(const wchar_t * name)
|
||||
{
|
||||
Space * space = find_child_space(name);
|
||||
|
@ -1928,19 +2070,62 @@ Space & Space::find_add_child_space(const std::wstring & name)
|
|||
}
|
||||
|
||||
|
||||
std::wstring * Space::find_child_space_name()
|
||||
{
|
||||
return get_wstr(child_spaces_name);
|
||||
}
|
||||
|
||||
|
||||
const std::wstring * Space::find_child_space_name() const
|
||||
{
|
||||
return get_wstr(child_spaces_name);
|
||||
}
|
||||
|
||||
|
||||
std::wstring Space::get_child_space_name() const
|
||||
{
|
||||
const std::wstring * name = find_child_space_name();
|
||||
|
||||
if( name )
|
||||
{
|
||||
return *name;
|
||||
}
|
||||
else
|
||||
{
|
||||
return std::wstring();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Space::is_child_space_name(const wchar_t * name) const
|
||||
{
|
||||
return is_equal(child_spaces_name, name);
|
||||
}
|
||||
|
||||
|
||||
bool Space::is_child_space_name(const std::wstring & name) const
|
||||
{
|
||||
return is_equal(child_spaces_name, name.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Space::remove_child_space(const wchar_t * name)
|
||||
{
|
||||
if( child_spaces )
|
||||
{
|
||||
for(size_t i=0 ; i<child_spaces->size() ; )
|
||||
{
|
||||
Space * child = (*child_spaces)[i];
|
||||
TableType * child_table = find_child_space_table();
|
||||
|
||||
if( child->name && (*child->name) == name )
|
||||
if( child_table )
|
||||
{
|
||||
for(size_t i=0 ; i<child_table->size() ; )
|
||||
{
|
||||
Space * child = (*child_table)[i];
|
||||
|
||||
if( child->is_equal(child_spaces_name, name) )
|
||||
{
|
||||
delete child;
|
||||
child = nullptr;
|
||||
child_spaces->erase(child_spaces->begin() + i);
|
||||
child_table->erase(child_table->begin() + i);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1958,13 +2143,13 @@ void Space::remove_child_space(const std::wstring & name)
|
|||
|
||||
void Space::remove_child_space(size_t index)
|
||||
{
|
||||
if( child_spaces && index < child_spaces->size() )
|
||||
{
|
||||
Space * child = (*child_spaces)[index];
|
||||
delete child;
|
||||
child = nullptr;
|
||||
TableType * child_table = find_child_space_table();
|
||||
|
||||
child_spaces->erase(child_spaces->begin() + index);
|
||||
if( child_table && index < child_table->size() )
|
||||
{
|
||||
Space * child = (*child_table)[index];
|
||||
delete child;
|
||||
child_table->erase(child_table->begin() + index);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2022,46 +2207,10 @@ void Space::copy_value_from(const Space & space)
|
|||
}
|
||||
|
||||
|
||||
void Space::copy_child_spaces_from(const Space & space)
|
||||
{
|
||||
if( space.child_spaces )
|
||||
{
|
||||
initialize_child_spaces_if_needed();
|
||||
child_spaces->clear();
|
||||
|
||||
for(size_t i = 0 ; i < space.child_spaces->size() ; ++i)
|
||||
{
|
||||
child_spaces->push_back(new Space((*space.child_spaces)[i]));
|
||||
}
|
||||
}
|
||||
else
|
||||
if( child_spaces )
|
||||
{
|
||||
remove_child_spaces();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Space::copy_space_name_from(const Space & space)
|
||||
{
|
||||
if( space.name )
|
||||
{
|
||||
initialize_space_name_if_needed();
|
||||
*name = *space.name;
|
||||
}
|
||||
else
|
||||
if( name )
|
||||
{
|
||||
remove_space_name();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Space::copy_from(const Space & space)
|
||||
{
|
||||
copy_value_from(space);
|
||||
copy_child_spaces_from(space);
|
||||
copy_space_name_from(space);
|
||||
}
|
||||
|
||||
|
||||
|
@ -2126,9 +2275,6 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -2137,8 +2283,6 @@ void Space::move_from(Space && space)
|
|||
void Space::initialize()
|
||||
{
|
||||
type = type_null;
|
||||
child_spaces = nullptr;
|
||||
name = nullptr;
|
||||
}
|
||||
|
||||
void Space::initialize_value_null_if_needed()
|
||||
|
@ -2300,18 +2444,17 @@ void Space::initialize_value_table_if_needed(TableType && tab)
|
|||
|
||||
void Space::initialize_child_spaces_if_needed()
|
||||
{
|
||||
if( child_spaces == nullptr )
|
||||
Space * child_spaces = get_object_field(child_spaces_field_table_name);
|
||||
|
||||
if( child_spaces )
|
||||
{
|
||||
child_spaces = new TableType();
|
||||
if( !child_spaces->is_table() )
|
||||
child_spaces->set_empty_table();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Space::initialize_space_name_if_needed()
|
||||
{
|
||||
if( name == nullptr )
|
||||
else
|
||||
{
|
||||
name = new std::wstring();
|
||||
Space & new_child_spaces = add_empty_space(child_spaces_field_table_name);
|
||||
new_child_spaces.set_empty_table();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2396,34 +2539,6 @@ void Space::remove_value_table()
|
|||
}
|
||||
|
||||
|
||||
void Space::remove_child_spaces()
|
||||
{
|
||||
if( child_spaces )
|
||||
{
|
||||
for(size_t i = 0 ; i < child_spaces->size() ; ++i)
|
||||
{
|
||||
delete (*child_spaces)[i];
|
||||
(*child_spaces)[i] = nullptr;
|
||||
}
|
||||
|
||||
delete child_spaces;
|
||||
child_spaces = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Space::remove_space_name()
|
||||
{
|
||||
if( name )
|
||||
{
|
||||
delete name;
|
||||
name = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -135,6 +135,9 @@ public:
|
|||
typedef std::map<std::wstring, Space*> ObjectType;
|
||||
typedef std::vector<Space*> TableType;
|
||||
|
||||
constexpr static const wchar_t * child_spaces_field_table_name = L"child_spaces";
|
||||
constexpr static const wchar_t * child_spaces_name = L"name";
|
||||
|
||||
enum Escape
|
||||
{
|
||||
no_escape,
|
||||
|
@ -179,10 +182,6 @@ public:
|
|||
Type type;
|
||||
Value value;
|
||||
|
||||
std::wstring * name;
|
||||
TableType * child_spaces;
|
||||
|
||||
|
||||
|
||||
Space();
|
||||
Space(const Space & space);
|
||||
|
@ -211,6 +210,12 @@ public:
|
|||
|
||||
void clear();
|
||||
|
||||
size_t str_size() const;
|
||||
size_t wstr_size() const;
|
||||
size_t object_size() const;
|
||||
size_t table_size() const;
|
||||
|
||||
|
||||
// set a new value
|
||||
void set_null();
|
||||
void set_empty_string();
|
||||
|
@ -576,16 +581,35 @@ public:
|
|||
|
||||
|
||||
// for child spaces (used only in Space format)
|
||||
Space * find_child_space(const wchar_t * name);
|
||||
Space * find_child_space(const std::wstring & name);
|
||||
TableType * find_child_space_table();
|
||||
const TableType * find_child_space_table() const;
|
||||
|
||||
bool child_spaces_empty() const;
|
||||
size_t child_spaces_size() const;
|
||||
|
||||
Space * find_child_space(const wchar_t * name);
|
||||
Space * find_child_space(const std::wstring & name);
|
||||
const Space * find_child_space(const wchar_t * name) const;
|
||||
const Space * find_child_space(const std::wstring & name) const;
|
||||
|
||||
Space * find_child_space(size_t table_index);
|
||||
const Space * find_child_space(size_t table_index) const;
|
||||
|
||||
Space & add_child_space();
|
||||
Space & add_child_space(const wchar_t * space_name);
|
||||
Space & add_child_space(const std::wstring & space_name);
|
||||
|
||||
Space & find_add_child_space(const wchar_t * name);
|
||||
Space & find_add_child_space(const std::wstring & name);
|
||||
|
||||
std::wstring * find_child_space_name();
|
||||
const std::wstring * find_child_space_name() const;
|
||||
|
||||
std::wstring get_child_space_name() const;
|
||||
|
||||
bool is_child_space_name(const wchar_t * name) const;
|
||||
bool is_child_space_name(const std::wstring & name) const;
|
||||
|
||||
void remove_child_space(const wchar_t * name);
|
||||
void remove_child_space(const std::wstring & name);
|
||||
void remove_child_space(size_t index);
|
||||
|
@ -739,44 +763,6 @@ protected:
|
|||
}
|
||||
|
||||
|
||||
template<typename ArgType>
|
||||
Space * find_child_space_generic(const ArgType & name)
|
||||
{
|
||||
if( child_spaces )
|
||||
{
|
||||
for(size_t i = 0 ; i < child_spaces->size() ; ++i)
|
||||
{
|
||||
Space * space = (*child_spaces)[i];
|
||||
|
||||
if( space->name && *space->name == name )
|
||||
{
|
||||
return space;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
template<typename ArgType>
|
||||
Space * find_child_space_generic(const ArgType & name) const
|
||||
{
|
||||
if( child_spaces )
|
||||
{
|
||||
for(size_t i = 0 ; i < child_spaces->size() ; ++i)
|
||||
{
|
||||
Space * space = (*child_spaces)[i];
|
||||
|
||||
if( space->name && *space->name == name )
|
||||
{
|
||||
return space;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
template<typename StreamType>
|
||||
|
@ -969,7 +955,7 @@ protected:
|
|||
if( !is_main_object )
|
||||
{
|
||||
str << '{';
|
||||
print_if(pretty_print && (!value.value_object.empty() || (child_spaces && !child_spaces->empty())), str, '\n');
|
||||
print_if(pretty_print && (!value.value_object.empty() || !child_spaces_empty()), str, '\n');
|
||||
}
|
||||
|
||||
bool is_first = true;
|
||||
|
@ -1009,21 +995,25 @@ protected:
|
|||
template<typename StreamType>
|
||||
void serialize_child_spaces(StreamType & str, bool pretty_print, int level) const
|
||||
{
|
||||
if( child_spaces && !child_spaces->empty() )
|
||||
const TableType * child_table = find_child_space_table();
|
||||
|
||||
if( child_table && !child_table->empty() )
|
||||
{
|
||||
print_if(pretty_print, str, '\n');
|
||||
|
||||
for(Space * child_space : *child_spaces)
|
||||
for(Space * child_space : *child_table)
|
||||
{
|
||||
print_if(!pretty_print, str, ' ');
|
||||
|
||||
if( child_space->name && !child_space->name->empty() )
|
||||
const std::wstring * name = child_space->get_wstr(child_spaces_name);
|
||||
|
||||
if( name && !name->empty() )
|
||||
{
|
||||
bool quote_field = should_field_be_quoted(*child_space->name);
|
||||
bool quote_field = should_field_be_quoted(*name);
|
||||
|
||||
print_level(pretty_print, level, str);
|
||||
print_if(quote_field, str, '"');
|
||||
serialize_string_buffer(child_space->name->c_str(), str, Escape::escape_space);
|
||||
serialize_string_buffer(name->c_str(), str, Escape::escape_space);
|
||||
print_if(quote_field, str, '"');
|
||||
|
||||
str << ' ';
|
||||
|
@ -1287,8 +1277,6 @@ protected:
|
|||
|
||||
|
||||
void copy_value_from(const Space & space);
|
||||
void copy_child_spaces_from(const Space & space);
|
||||
void copy_space_name_from(const Space & space);
|
||||
void copy_from(const Space & space);
|
||||
|
||||
void copy_value_object(const Value & value_from);
|
||||
|
@ -1312,15 +1300,15 @@ protected:
|
|||
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();
|
||||
|
||||
void remove_value();
|
||||
void remove_value_string();
|
||||
void remove_value_wstring();
|
||||
void remove_value_object();
|
||||
void remove_value_table();
|
||||
void remove_child_spaces();
|
||||
void remove_space_name();
|
||||
|
||||
const Space * find_child_space_const(const wchar_t * name) const;
|
||||
const Space * find_child_space_const(size_t table_index) const;
|
||||
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue