diff --git a/cwObject.cpp b/cwObject.cpp index 3d41b4a..6c8de5f 100644 --- a/cwObject.cpp +++ b/cwObject.cpp @@ -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(((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(((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 ); diff --git a/cwObject.h b/cwObject.h index c3d5085..02fef46 100644 --- a/cwObject.h +++ b/cwObject.h @@ -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