cwObject.h/cpp : Added next_child_ele(). Renamed _objAppendLeftMostNode() to _objAppendRightMostNode().

This commit is contained in:
kevin 2021-12-25 22:11:14 -05:00
parent 1c39480b6f
commit 02311b3305
3 changed files with 44 additions and 24 deletions

View File

@ -322,7 +322,7 @@ namespace cw
object_t* _objTypeDuplContainer( const struct object_str* src, struct object_str* parent ) object_t* _objTypeDuplContainer( const struct object_str* src, struct object_str* parent )
{ {
object_t* o = _objAppendLeftMostNode( parent, _objAllocate( src->type->id, parent )); object_t* o = _objAppendRightMostNode( parent, _objAllocate( src->type->id, parent ));
for(object_t* ch=src->u.children; ch!=nullptr; ch=ch->sibling) for(object_t* ch=src->u.children; ch!=nullptr; ch=ch->sibling)
ch->type->duplicate(ch,o); ch->type->duplicate(ch,o);
@ -444,7 +444,7 @@ namespace cw
return kOkRC; return kOkRC;
} }
object_t* _objAppendLeftMostNode( object_t* parent, object_t* newNode ) object_t* _objAppendRightMostNode( object_t* parent, object_t* newNode )
{ {
if( newNode == nullptr ) if( newNode == nullptr )
return nullptr; return nullptr;
@ -473,7 +473,7 @@ namespace cw
object_t* _objCreateConainerNode( lex::handle_t lexH, object_t* parent, objTypeId_t tid ) object_t* _objCreateConainerNode( lex::handle_t lexH, object_t* parent, objTypeId_t tid )
{ {
if( _objVerifyParentIsValueContainer(lexH,parent,_objTypeIdToLabel(tid)) == kOkRC ) if( _objVerifyParentIsValueContainer(lexH,parent,_objTypeIdToLabel(tid)) == kOkRC )
return _objAppendLeftMostNode( parent, _objAllocate( tid, parent )); return _objAppendRightMostNode( parent, _objAllocate( tid, parent ));
return nullptr; return nullptr;
} }
@ -541,7 +541,7 @@ cw::rc_t cw::object_t::append_child( struct object_str* child )
if( !is_container() ) if( !is_container() )
return cwLogError(kInvalidDataTypeRC,"The parent of a child object node must be a 'container'."); return cwLogError(kInvalidDataTypeRC,"The parent of a child object node must be a 'container'.");
_objAppendLeftMostNode( this, child ); _objAppendRightMostNode( this, child );
return kOkRC; return kOkRC;
} }
@ -628,7 +628,7 @@ const struct cw::object_str* cw::object_t::find( const char* label, unsigned fla
if( o->is_pair() && textCompare(o->pair_label(),label) == 0 ) if( o->is_pair() && textCompare(o->pair_label(),label) == 0 )
return o->pair_value(); return o->pair_value();
const object_t* ch; const object_t* ch;
if( cwIsFlag(flags,kRecurseFl) ) if( cwIsFlag(flags,kRecurseFl) )
if((ch = o->find(label)) != nullptr ) if((ch = o->find(label)) != nullptr )
return ch; return ch;
@ -659,6 +659,22 @@ struct cw::object_str* cw::object_t::child_ele( unsigned idx )
return const_cast<struct object_str*>(((const object_t*)this)->child_ele(idx)); return const_cast<struct object_str*>(((const object_t*)this)->child_ele(idx));
} }
const struct cw::object_str* cw::object_t::next_child_ele( const struct object_str* ele ) const
{
if( is_container() )
{
if( ele == nullptr )
return u.children;
return ele->sibling;
}
return nullptr;
}
struct cw::object_str* cw::object_t::next_child_ele( struct object_str* ele )
{
return const_cast<struct object_str*>(((const object_t*)this)->next_child_ele(ele));
}
unsigned cw::object_t::to_string( char* buf, unsigned bufByteN ) const unsigned cw::object_t::to_string( char* buf, unsigned bufByteN ) const
{ {
@ -723,7 +739,7 @@ cw::object_t* cw::newListObject( object_t* parent )
cw::object_t* cw::newPairObject( const char* label, object_t* value, object_t* parent) cw::object_t* cw::newPairObject( const char* label, object_t* value, object_t* parent)
{ {
object_t* pair = _objAppendLeftMostNode(parent, _objAllocate( kPairTId, parent) ); object_t* pair = _objAppendRightMostNode(parent, _objAllocate( kPairTId, parent) );
_objCreateValueNode<const char*>( pair, label ); _objCreateValueNode<const char*>( pair, label );
@ -780,7 +796,6 @@ cw::rc_t cw::objectFromString( const char* s, object_t*& objRef )
unsigned lexId = lex::kErrorLexTId; unsigned lexId = lex::kErrorLexTId;
object_t* cnp = _objAllocate(kRootTId,nullptr); object_t* cnp = _objAllocate(kRootTId,nullptr);
object_t* root = cnp; object_t* root = cnp;
objRef = nullptr; objRef = nullptr;
if((rc = lex::create(lexH,s,textLength(s), lexFlags )) != kOkRC ) if((rc = lex::create(lexH,s,textLength(s), lexFlags )) != kOkRC )
@ -860,7 +875,7 @@ cw::rc_t cw::objectFromString( const char* s, object_t*& objRef )
break; break;
case kNullLexTId: case kNullLexTId:
_objAppendLeftMostNode( cnp, _objAllocate( kNullTId, cnp )); _objAppendRightMostNode( cnp, _objAllocate( kNullTId, cnp ));
break; break;
case kSegmentedIdLexTId: case kSegmentedIdLexTId:
@ -870,7 +885,7 @@ cw::rc_t cw::objectFromString( const char* s, object_t*& objRef )
// if the parent is an object then this string must be a pair label // if the parent is an object then this string must be a pair label
if( cnp->is_dict() ) if( cnp->is_dict() )
cnp = _objAppendLeftMostNode( cnp, _objAllocate( kPairTId, cnp )); cnp = _objAppendRightMostNode( cnp, _objAllocate( kPairTId, cnp ));
unsigned n = lex::tokenCharCount(lexH); unsigned n = lex::tokenCharCount(lexH);
char s[ n + 1 ]; char s[ n + 1 ];

View File

@ -192,6 +192,11 @@ namespace cw
const struct object_str* child_ele( unsigned idx ) const; const struct object_str* child_ele( unsigned idx ) const;
struct object_str* child_ele( unsigned idx ); struct object_str* child_ele( unsigned idx );
// Set 'ele' to nullptr to return first child. Returns nullptr when 'ele' is last child.
const struct object_str* next_child_ele( const struct object_str* ele) const;
struct object_str* next_child_ele( struct object_str* ele);
// Set flag 'kRecurseFl' to recurse into the object in search of the value. // Set flag 'kRecurseFl' to recurse into the object in search of the value.
// Set flag 'kOptionalFl' if the label is optional and may not exist. // Set flag 'kOptionalFl' if the label is optional and may not exist.
template< typename T > template< typename T >

View File

@ -7,7 +7,7 @@ namespace cw
objType_t* _objIdToType( objTypeId_t tid ); objType_t* _objIdToType( objTypeId_t tid );
object_t* _objAllocate( objTypeId_t tid=kInvalidTId, object_t* parent=NULL ); object_t* _objAllocate( objTypeId_t tid=kInvalidTId, object_t* parent=NULL );
object_t* _objCreateConainerNode( lex::handle_t lexH, object_t* parent, objTypeId_t tid ); object_t* _objCreateConainerNode( lex::handle_t lexH, object_t* parent, objTypeId_t tid );
object_t* _objAppendLeftMostNode( object_t* parent, object_t* newNode ); object_t* _objAppendRightMostNode( object_t* parent, object_t* newNode );
template< typename T > template< typename T >
object_t* _objSetLeafValue( object_t* obj, T value ) object_t* _objSetLeafValue( object_t* obj, T value )
@ -127,49 +127,49 @@ namespace cw
} }
template<> object_t* _objCreateValueNode<uint8_t>( object_t* parent, uint8_t value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<uint8_t>( object_t* parent, uint8_t value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<int8_t>( object_t* parent, int8_t value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<int8_t>( object_t* parent, int8_t value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<uint16_t>( object_t* parent, uint16_t value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<uint16_t>( object_t* parent, uint16_t value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<int16_t>( object_t* parent, int16_t value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<int16_t>( object_t* parent, int16_t value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<uint32_t>( object_t* parent, uint32_t value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<uint32_t>( object_t* parent, uint32_t value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<int32_t>( object_t* parent, int32_t value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<int32_t>( object_t* parent, int32_t value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<uint64_t>( object_t* parent, uint64_t value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<uint64_t>( object_t* parent, uint64_t value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<int64_t>( object_t* parent, int64_t value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<int64_t>( object_t* parent, int64_t value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<float>( object_t* parent, float value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<float>( object_t* parent, float value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<double>( object_t* parent, double value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<double>( object_t* parent, double value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode< char*>( object_t* parent, char* value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode< char*>( object_t* parent, char* value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<const char*>( object_t* parent, const char* value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<const char*>( object_t* parent, const char* value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<bool>( object_t* parent, bool value, const char* msg, unsigned flags ) template<> object_t* _objCreateValueNode<bool>( object_t* parent, bool value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); } { return _objAppendRightMostNode( parent, _objCallSetLeafValue( _objAllocate(), value ) ); }
template< typename T > template< typename T >
object_t*_objCreatePairNode( object_t* parentObj, const char* label, const T& value, const char* msg=nullptr, unsigned flags=0 ) object_t*_objCreatePairNode( object_t* parentObj, const char* label, const T& value, const char* msg=nullptr, unsigned flags=0 )
{ {
object_t* pair = _objAppendLeftMostNode(parentObj, _objAllocate( kPairTId, parentObj) ); object_t* pair = _objAppendRightMostNode(parentObj, _objAllocate( kPairTId, parentObj) );
_objCreateValueNode<const char*>( pair, label, msg, flags ); _objCreateValueNode<const char*>( pair, label, msg, flags );
return _objCreateValueNode<T>( pair, value, msg, flags ); return _objCreateValueNode<T>( pair, value, msg, flags );