Merge branch 'master' of gitea.larke.org:klarke/libcw

This commit is contained in:
kevin.larke 2020-04-21 15:00:03 -04:00
commit 24a43c4c47
3 changed files with 78 additions and 34 deletions

View File

@ -280,30 +280,63 @@ namespace cw
} }
object_t* _objTypeDuplContainer( const struct object_str* src, struct object_str* parent )
{
object_t* o = _objAppendLeftMostNode( parent, _objAllocate( src->type->id, parent ));
for(object_t* ch=src->u.children; ch!=nullptr; ch=ch->sibling)
ch->type->duplicate(ch,o);
return o;
}
object_t* _objTypeDuplNull( const struct object_str* src, struct object_str* parent ) { return _objAllocate( src->type->id, parent); }
object_t* _objTypeDuplError( const struct object_str* src, struct object_str* parent ) { return _objAllocate( src->type->id, parent); }
object_t* _objTypeDuplChar( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<char>(parent,src->u.c); }
object_t* _objTypeDuplInt8( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<int8_t>(parent,src->u.i8); }
object_t* _objTypeDuplUInt8( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<uint8_t>(parent,src->u.u8); }
object_t* _objTypeDuplInt16( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<int16_t>(parent,src->u.i16); }
object_t* _objTypeDuplUInt16( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<int16_t>(parent,src->u.u16); }
object_t* _objTypeDuplInt32( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<int32_t>(parent,src->u.i32); }
object_t* _objTypeDuplUInt32( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<int32_t>(parent,src->u.u32); }
object_t* _objTypeDuplInt64( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<int64_t>(parent,src->u.i64); }
object_t* _objTypeDuplUInt64( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<uint64_t>(parent,src->u.u64); }
object_t* _objTypeDuplBool( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<bool >(parent,src->u.b ); }
object_t* _objTypeDuplFloat( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<float >(parent,src->u.f ); }
object_t* _objTypeDuplDouble( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<double >(parent,src->u.d ); }
object_t* _objTypeDuplString( const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<char* >(parent,mem::duplStr(src->u.str)); }
object_t* _objTypeDuplCString(const struct object_str* src, struct object_str* parent ) { return _objCreateValueNode<const char*>(parent,mem::duplStr(src->u.str));}
object_t* _objTypeDuplVect( const struct object_str* src, struct object_str* parent ) { assert(0); }
object_t* _objTypeDuplPair( const struct object_str* src, struct object_str* parent ) { return _objTypeDuplContainer(src,parent); }
object_t* _objTypeDuplList( const struct object_str* src, struct object_str* parent ) { return _objTypeDuplContainer(src,parent); }
object_t* _objTypeDuplDict( const struct object_str* src, struct object_str* parent ) { return _objTypeDuplContainer(src,parent); }
object_t* _objTypeDuplRoot( const struct object_str* src, struct object_str* parent ) { return _objTypeDuplContainer(src,parent); }
objType_t _objTypeArray[] = objType_t _objTypeArray[] =
{ {
{ kNullTId, "null", 0, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintNull, _objTypeToStringNull }, { kNullTId, "null", 0, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintNull, _objTypeToStringNull, _objTypeDuplNull },
{ kErrorTId, "error", 0, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintError, _objTypeToStringError }, { kErrorTId, "error", 0, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintError, _objTypeToStringError, _objTypeDuplError },
{ kCharTId, "char", 0, _objTypeFree, _objTypeValueFromChar, _objTypePrintChar, _objTypeToStringChar }, { kCharTId, "char", 0, _objTypeFree, _objTypeValueFromChar, _objTypePrintChar, _objTypeToStringChar, _objTypeDuplChar },
{ kInt8TId, "int8", 0, _objTypeFree, _objTypeValueFromInt8, _objTypePrintInt8, _objTypeToStringInt8 }, { kInt8TId, "int8", 0, _objTypeFree, _objTypeValueFromInt8, _objTypePrintInt8, _objTypeToStringInt8, _objTypeDuplInt8 },
{ kUInt8TId, "uint8", 0, _objTypeFree, _objTypeValueFromUInt8, _objTypePrintUInt8, _objTypeToStringUInt8 }, { kUInt8TId, "uint8", 0, _objTypeFree, _objTypeValueFromUInt8, _objTypePrintUInt8, _objTypeToStringUInt8, _objTypeDuplUInt8 },
{ kInt16TId, "int16", 0, _objTypeFree, _objTypeValueFromInt16, _objTypePrintInt16, _objTypeToStringInt16 }, { kInt16TId, "int16", 0, _objTypeFree, _objTypeValueFromInt16, _objTypePrintInt16, _objTypeToStringInt16, _objTypeDuplInt16 },
{ kUInt16TId, "uint16", 0, _objTypeFree, _objTypeValueFromUInt16, _objTypePrintUInt16, _objTypeToStringUInt16 }, { kUInt16TId, "uint16", 0, _objTypeFree, _objTypeValueFromUInt16, _objTypePrintUInt16, _objTypeToStringUInt16, _objTypeDuplUInt16 },
{ kInt32TId, "int32", 0, _objTypeFree, _objTypeValueFromInt32, _objTypePrintInt32, _objTypeToStringInt32 }, { kInt32TId, "int32", 0, _objTypeFree, _objTypeValueFromInt32, _objTypePrintInt32, _objTypeToStringInt32, _objTypeDuplInt32 },
{ kUInt32TId, "uint32", 0, _objTypeFree, _objTypeValueFromUInt32, _objTypePrintUInt32, _objTypeToStringUInt32 }, { kUInt32TId, "uint32", 0, _objTypeFree, _objTypeValueFromUInt32, _objTypePrintUInt32, _objTypeToStringUInt32, _objTypeDuplUInt32 },
{ kInt64TId, "int64", 0, _objTypeFree, _objTypeValueFromInt64, _objTypePrintInt64, _objTypeToStringInt64 }, { kInt64TId, "int64", 0, _objTypeFree, _objTypeValueFromInt64, _objTypePrintInt64, _objTypeToStringInt64, _objTypeDuplInt64 },
{ kUInt64TId, "uint64", 0, _objTypeFree, _objTypeValueFromUInt64, _objTypePrintUInt64, _objTypeToStringUInt64 }, { kUInt64TId, "uint64", 0, _objTypeFree, _objTypeValueFromUInt64, _objTypePrintUInt64, _objTypeToStringUInt64, _objTypeDuplUInt64 },
{ kBoolTId, "bool", 0, _objTypeFree, _objTypeValueFromBool, _objTypePrintBool, _objTypeToStringBool }, { kBoolTId, "bool", 0, _objTypeFree, _objTypeValueFromBool, _objTypePrintBool, _objTypeToStringBool, _objTypeDuplBool },
{ kFloatTId, "float", 0, _objTypeFree, _objTypeValueFromFloat, _objTypePrintFloat, _objTypeToStringFloat }, { kFloatTId, "float", 0, _objTypeFree, _objTypeValueFromFloat, _objTypePrintFloat, _objTypeToStringFloat, _objTypeDuplFloat },
{ kDoubleTId, "double", 0, _objTypeFree, _objTypeValueFromDouble, _objTypePrintDouble, _objTypeToStringDouble }, { kDoubleTId, "double", 0, _objTypeFree, _objTypeValueFromDouble, _objTypePrintDouble, _objTypeToStringDouble, _objTypeDuplDouble },
{ kStringTId, "string", 0, _objTypeFreeString, _objTypeValueFromString, _objTypePrintString, _objTypeToStringString }, { kStringTId, "string", 0, _objTypeFreeString, _objTypeValueFromString, _objTypePrintString, _objTypeToStringString, _objTypeDuplString },
{ kCStringTId, "cstring", 0, _objTypeFree, _objTypeValueFromCString, _objTypePrintString, _objTypeToStringString }, { kCStringTId, "cstring", 0, _objTypeFree, _objTypeValueFromCString, _objTypePrintString, _objTypeToStringString, _objTypeDuplCString },
{ kVectTId, "vect", 0, _objTypeFree, _objTypeValueFromVect, _objTypePrintVect, _objTypeToStringVect }, { kVectTId, "vect", 0, _objTypeFree, _objTypeValueFromVect, _objTypePrintVect, _objTypeToStringVect, _objTypeDuplVect },
{ kPairTId, "pair", kContainerFl | kValueContainerFl, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintPair, _objTypeToStringPair }, { kPairTId, "pair", kContainerFl | kValueContainerFl, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintPair, _objTypeToStringPair, _objTypeDuplPair },
{ kListTId, "list", kContainerFl | kValueContainerFl, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintList, _objTypeToStringList }, { kListTId, "list", kContainerFl | kValueContainerFl, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintList, _objTypeToStringList, _objTypeDuplList },
{ kDictTId, "dict", kContainerFl, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintDict, _objTypeToStringDict }, { kDictTId, "dict", kContainerFl, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintDict, _objTypeToStringDict, _objTypeDuplDict },
{ kRootTId, "root", kContainerFl | kValueContainerFl, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintRoot, _objTypeToStringRoot }, { kRootTId, "root", kContainerFl | kValueContainerFl, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintRoot, _objTypeToStringRoot, _objTypeDuplRoot },
{ kInvalidTId, "<invalid>", 0, nullptr, nullptr, nullptr, nullptr } { kInvalidTId, "<invalid>", 0, nullptr, nullptr, nullptr, nullptr, nullptr }
}; };
@ -563,6 +596,9 @@ void cw::object_t::print(const print_ctx_t* c) const
type->print(this,ctx); type->print(this,ctx);
} }
cw::object_t* cw::object_t::duplicate() const
{ return type->duplicate(this,nullptr); }
cw::object_t* cw::newObject( std::uint8_t v, object_t* parent) cw::object_t* cw::newObject( std::uint8_t v, object_t* parent)
{ return _objCreateValueNode<uint8_t>( parent, v ); } { return _objCreateValueNode<uint8_t>( parent, v ); }

View File

@ -68,6 +68,8 @@ namespace cw
rc_t (*value)( const struct object_str* o, unsigned tid, void* dst ); rc_t (*value)( const struct object_str* o, unsigned tid, void* dst );
void (*print)( const struct object_str* o, print_ctx_t& c ); void (*print)( const struct object_str* o, print_ctx_t& c );
unsigned( *to_string)( const struct object_str* o, char* buf, unsigned bufByteN ); unsigned( *to_string)( const struct object_str* o, char* buf, unsigned bufByteN );
struct object_str* (*duplicate)( const struct object_str* src, struct object_str* parent );
} objType_t; } objType_t;
@ -182,6 +184,7 @@ namespace cw
unsigned to_string( char* buf, unsigned bufByteN ) const; unsigned to_string( char* buf, unsigned bufByteN ) const;
void print(const print_ctx_t* c=NULL) const; void print(const print_ctx_t* c=NULL) const;
struct object_str* duplicate() const;
} object_t; } object_t;

View File

@ -329,15 +329,16 @@ namespace cw
return ele; return ele;
} }
rc_t _createElementsFromChildList( ui_t* p, object_t* po, unsigned wsSessId, ele_t* parentEle ); rc_t _createElementsFromChildList( ui_t* p, const object_t* po, unsigned wsSessId, ele_t* parentEle );
// //
rc_t _createEleFromRsrsc( ui_t* p, ele_t* parentEle, const char* eleType, object_t* o, unsigned wsSessId ) rc_t _createEleFromRsrsc( ui_t* p, ele_t* parentEle, const char* eleType, const object_t* srcObj, unsigned wsSessId )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
object_t* co = nullptr; object_t* co = nullptr;
ele_t* ele = nullptr; ele_t* ele = nullptr;
char* eleName = nullptr; char* eleName = nullptr;
object_t* o = srcObj->duplicate(); // duplicate the rsrc object so that we can modify it.
if( !o->is_dict() ) if( !o->is_dict() )
return cwLogError(kSyntaxErrorRC,"All ui element resource records must be dictionaries."); return cwLogError(kSyntaxErrorRC,"All ui element resource records must be dictionaries.");
@ -420,12 +421,15 @@ namespace cw
if( co != nullptr ) if( co != nullptr )
co->free(); co->free();
if( o != nullptr )
o->free();
return rc; return rc;
} }
// 'od' is an object dictionary where each pair in the dictionary has // 'od' is an object dictionary where each pair in the dictionary has
// the form: 'eleType':{ <object> } // the form: 'eleType':{ <object> }
rc_t _createElementsFromChildList( ui_t* p, object_t* po, unsigned wsSessId, ele_t* parentEle ) rc_t _createElementsFromChildList( ui_t* p, const object_t* po, unsigned wsSessId, ele_t* parentEle )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
@ -436,23 +440,25 @@ namespace cw
for(unsigned i=0; i<childN; ++i) for(unsigned i=0; i<childN; ++i)
{ {
object_t* o = po->child_ele(i); const object_t* o = po->child_ele(i);
if( !o->is_pair() ) if( !o->is_pair() )
return cwLogError(kSyntaxErrorRC,"All object dictionary children must be pairs."); return cwLogError(kSyntaxErrorRC,"All object dictionary children must be pairs.");
if((rc = _createEleFromRsrsc(p, parentEle, o->pair_label(), o->pair_value(), wsSessId )) != kOkRC ) // skip pairs whose value is not a dict
return rc; if( o->pair_value()->is_dict() )
if((rc = _createEleFromRsrsc(p, parentEle, o->pair_label(), o->pair_value(), wsSessId )) != kOkRC )
return rc;
} }
return rc; return rc;
} }
rc_t _createFromObj( ui_t* p, object_t* o, unsigned wsSessId, unsigned parentUuId ) rc_t _createFromObj( ui_t* p, const object_t* o, unsigned wsSessId, unsigned parentUuId )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
object_t* po = nullptr; const object_t* po = nullptr;
ele_t* parentEle = nullptr; ele_t* parentEle = nullptr;
char* eleName = nullptr; char* eleName = nullptr;
@ -460,7 +466,6 @@ namespace cw
if((po = o->find("parent",kNoRecurseFl | kOptionalFl)) == nullptr ) if((po = o->find("parent",kNoRecurseFl | kOptionalFl)) == nullptr )
return cwLogError(kSyntaxErrorRC,"UI resources must have a root 'parent' value."); return cwLogError(kSyntaxErrorRC,"UI resources must have a root 'parent' value.");
// get the parent element name // get the parent element name
if((rc = po->value(eleName)) != kOkRC ) if((rc = po->value(eleName)) != kOkRC )
return cwLogError(kOpFailRC,"The root 'parent' value could not be accessed."); return cwLogError(kOpFailRC,"The root 'parent' value could not be accessed.");
@ -470,13 +475,13 @@ namespace cw
return cwLogError(kSyntaxErrorRC,"A parent UI element named '%s' could not be found.",cwStringNullGuard(eleName)); return cwLogError(kSyntaxErrorRC,"A parent UI element named '%s' could not be found.",cwStringNullGuard(eleName));
// unlink the 'parent' pair // unlink the 'parent' pair
po = po->parent; //po = po->parent;
po->unlink(); //po->unlink();
rc = _createElementsFromChildList( p, o, wsSessId, parentEle ); rc = _createElementsFromChildList( p, o, wsSessId, parentEle );
po->free(); //po->free();
return rc; return rc;
} }