cwObject.h/cpp : Added unlink() and non const find(),par_value(),list_ele().

This commit is contained in:
kevin.larke 2020-03-25 11:34:28 -04:00
parent 1a11282915
commit e4328c950b
2 changed files with 65 additions and 4 deletions

View File

@ -376,8 +376,40 @@ namespace cw
}
void cw::object_t::unlink()
{
// if this node has no parent then there it is not part of a tree
// and therefore cannot be unlinked
if( parent == nullptr )
return;
object_t* c0 = nullptr;
object_t* c = parent->u.children;
for(; c!=nullptr; c=c->sibling)
{
if( c == this )
{
if( c0 == nullptr )
parent->u.children = c->sibling;
else
c0->sibling = c->sibling;
c->parent = nullptr;
c->sibling = nullptr;
return;
}
c0 = c;
}
// if a child has a parent then it must be in that parent's child list
cwAssert(0);
}
void cw::object_t::free()
{
unlink();
if( is_container() )
{
object_t* o1 = nullptr;
@ -435,7 +467,16 @@ const struct cw::object_str* cw::object_t::pair_value() const
return nullptr;
}
const struct cw::object_str* cw::object_t::find( const char* label ) const
struct cw::object_str* cw::object_t::pair_value()
{
cwAssert( is_pair() );
if( is_pair() )
return u.children->sibling;
return nullptr;
}
const struct cw::object_str* cw::object_t::find( const char* label, bool recurseFl ) const
{
if( is_container() )
{
@ -445,13 +486,19 @@ const struct cw::object_str* cw::object_t::find( const char* label ) const
return o->pair_value();
const object_t* ch;
if((ch = o->find(label)) != nullptr )
return ch;
if( recurseFl )
if((ch = o->find(label)) != nullptr )
return ch;
}
}
return nullptr;
}
struct cw::object_str* cw::object_t::find( const char* label, bool recurseFl )
{
return const_cast<struct object_str*>(((const object_t*)this)->find(label,recurseFl));
}
const struct cw::object_str* cw::object_t::list_ele( unsigned idx ) const
{
if( is_list() )
@ -464,6 +511,12 @@ const struct cw::object_str* cw::object_t::list_ele( unsigned idx ) const
return nullptr;
}
struct cw::object_str* cw::object_t::list_ele( unsigned idx )
{
return const_cast<struct object_str*>(((const object_t*)this)->list_ele(idx));
}
unsigned cw::object_t::to_string( char* buf, unsigned bufByteN ) const
{
return type->to_string(this,buf,bufByteN );

View File

@ -94,6 +94,7 @@ namespace cw
} u;
void unlink();
void free();
unsigned child_count() const;
@ -121,9 +122,16 @@ namespace cw
rc_t value( char*& v ) const;
const char* pair_label() const;
const struct object_str* pair_value() const;
const struct object_str* find( const char* label ) const;
struct object_str* pair_value();
// Search for the pair label 'label'.
const struct object_str* find( const char* label, bool recurseFl=true ) const;
struct object_str* find( const char* label, bool recurseFl=true );
const struct object_str* list_ele( unsigned idx ) const;
struct object_str* list_ele( unsigned idx );
template< typename T >
rc_t get( const char* label, T& v ) const