@ -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,30 +1914,142 @@ 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 )
{
@ -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 )
TableType * child_table = find_child_space_table ( ) ;
if ( child_table )
{
for ( size_t i = 0 ; i < child_spaces - > size ( ) ; )
for ( size_t i = 0 ; i < child_ table - > size ( ) ; )
{
Space * child = ( * child_spaces ) [ i ] ;
Space * child = ( * child_ table ) [ i ] ;
if ( child - > name & & ( * child - > name ) = = name )
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 ( ) )
TableType * child_table = find_child_space_table ( ) ;
if ( child_table & & index < child_table - > size ( ) )
{
Space * child = ( * child_spaces ) [ index ] ;
Space * child = ( * child_ table ) [ index ] ;
delete child ;
child = nullptr ;
child_spaces - > erase ( child_spaces - > begin ( ) + index ) ;
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