cwObject.h/cpp : Added newPairObject(), typeid: kCStringTId, changed list_ele() to child_ele()

This commit is contained in:
kevin.larke 2020-04-06 19:15:03 -04:00
parent 7d7a36fe03
commit bfe9d9ebfc
4 changed files with 177 additions and 19 deletions

View File

@ -173,7 +173,7 @@ namespace cw
// for each serial port cfg
for(unsigned i=0; i<p->serialN; ++i)
{
const object_t* e = cfgL->list_ele(i);
const object_t* e = cfgL->child_ele(i);
serialPort_t* r = p->serialA + i;
if( e == nullptr )
@ -281,7 +281,7 @@ namespace cw
for(unsigned i=0; i<p->audioCfgN; ++i)
{
audioCfg_t* r = p->audioCfgA + i;
if((node = deviceL->list_ele(i)) == nullptr )
if((node = deviceL->child_ele(i)) == nullptr )
{
if(( rc = node->getv(
"enableFl", r->enableFl,

View File

@ -86,7 +86,9 @@ namespace cw
rc_t _objTypeValueFromBool( const object_t* o, unsigned tid, void* dst ) { return getObjectValue(o->u.b,tid,dst,o->type->label); }
rc_t _objTypeValueFromNonValue( const object_t* o, unsigned tid, void* dst )
{ return cwLogError(kInvalidArgRC, "There is no conversion from '%s' to '%s'.", _objTypeIdToLabel(tid), o->type->label); }
{
return cwLogError(kInvalidArgRC, "There is no conversion from '%s' to '%s'.", _objTypeIdToLabel(tid), o->type->label);
}
rc_t _objTypeValueFromString( const object_t* o, unsigned tid, void* dst )
{
@ -124,8 +126,8 @@ namespace cw
void _objTypePrintUInt16( const object_t* o, print_ctx_t& c ) { printf("%i",o->u.u16); }
void _objTypePrintInt32( const object_t* o, print_ctx_t& c ) { printf("%i",o->u.i32); }
void _objTypePrintUInt32( const object_t* o, print_ctx_t& c ) { printf("%i",o->u.u32); }
void _objTypePrintInt64( const object_t* o, print_ctx_t& c ) { printf("%lli",o->u.i64); }
void _objTypePrintUInt64( const object_t* o, print_ctx_t& c ) { printf("%lli",o->u.u64); }
void _objTypePrintInt64( const object_t* o, print_ctx_t& c ) { printf("%li",o->u.i64); }
void _objTypePrintUInt64( const object_t* o, print_ctx_t& c ) { printf("%li",o->u.u64); }
void _objTypePrintBool( const object_t* o, print_ctx_t& c ) { printf("%s",o->u.b ? "true" : "false"); }
void _objTypePrintFloat( const object_t* o, print_ctx_t& c ) { printf("%f",o->u.f); }
void _objTypePrintDouble( const object_t* o, print_ctx_t& c ) { printf("%f",o->u.d); }
@ -277,6 +279,7 @@ namespace cw
{ kFloatTId, "float", 0, _objTypeFree, _objTypeValueFromFloat, _objTypePrintFloat, _objTypeToStringFloat },
{ kDoubleTId, "double", 0, _objTypeFree, _objTypeValueFromDouble, _objTypePrintDouble, _objTypeToStringDouble },
{ kStringTId, "string", 0, _objTypeFreeString, _objTypeValueFromString, _objTypePrintString, _objTypeToStringString },
{ kCStringTId, "cstring", 0, _objTypeFree, _objTypeValueFromString, _objTypePrintString, _objTypeToStringString },
{ kVectTId, "vect", 0, _objTypeFree, _objTypeValueFromVect, _objTypePrintVect, _objTypeToStringVect },
{ kPairTId, "pair", kContainerFl | kValueContainerFl, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintPair, _objTypeToStringPair },
{ kListTId, "list", kContainerFl | kValueContainerFl, _objTypeFree, _objTypeValueFromNonValue, _objTypePrintList, _objTypeToStringList },
@ -460,7 +463,7 @@ cw::rc_t cw::object_t::value( uint64_t& v ) const { return type->value(this,kUIn
cw::rc_t cw::object_t::value( float& v ) const { return type->value(this,kFloatTId,&v); }
cw::rc_t cw::object_t::value( double& v ) const { return type->value(this,kDoubleTId,&v); }
cw::rc_t cw::object_t::value( char*& v ) const { return type->value(this,kStringTId,&v); }
cw::rc_t cw::object_t::value( const char*& v ) const { return type->value(this,kStringTId,&v); }
cw::rc_t cw::object_t::value( const char*& v ) const { return type->value(this,kCStringTId,&v); }
const char* cw::object_t::pair_label() const
{
@ -510,9 +513,9 @@ struct cw::object_str* cw::object_t::find( const char* label, unsigned flags )
return const_cast<struct object_str*>(((const object_t*)this)->find(label,flags));
}
const struct cw::object_str* cw::object_t::list_ele( unsigned idx ) const
const struct cw::object_str* cw::object_t::child_ele( unsigned idx ) const
{
if( is_list() )
if( is_container() )
{
unsigned i = 0;
for(object_t* o=u.children; o!=nullptr; o=o->sibling,++i)
@ -522,9 +525,9 @@ 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 )
struct cw::object_str* cw::object_t::child_ele( unsigned idx )
{
return const_cast<struct object_str*>(((const object_t*)this)->list_ele(idx));
return const_cast<struct object_str*>(((const object_t*)this)->child_ele(idx));
}
@ -574,9 +577,53 @@ cw::object_t* cw::newObject( float v, object_t* parent)
cw::object_t* cw::newObject( double v, object_t* parent)
{ return _objCreateValueNode<double>( parent, v ); }
cw::object_t* cw::newObject( char* v, object_t* parent)
{ return _objCreateValueNode<const char*>( parent, v ); }
cw::object_t* cw::newObject( const char* v, object_t* parent)
{ return _objCreateValueNode<const char*>( parent, v ); }
cw::object_t* cw::newPairObject( const char* label, std::uint8_t v, object_t* parent)
{ return _objCreatePairNode<uint8_t>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, std::int8_t v, object_t* parent)
{ return _objCreatePairNode<int8_t>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, std::uint16_t v, object_t* parent)
{ return _objCreatePairNode<uint16_t>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, std::int16_t v, object_t* parent)
{ return _objCreatePairNode<int16_t>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, std::uint32_t v, object_t* parent)
{ return _objCreatePairNode<uint32_t>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, std::int32_t v, object_t* parent)
{ return _objCreatePairNode<int32_t>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, std::uint64_t v, object_t* parent)
{ return _objCreatePairNode<uint64_t>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, std::int64_t v, object_t* parent)
{ return _objCreatePairNode<uint64_t>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, bool v, object_t* parent)
{ return _objCreatePairNode<bool>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, float v, object_t* parent)
{ return _objCreatePairNode<float>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, double v, object_t* parent)
{ return _objCreatePairNode<double>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, char* v, object_t* parent)
{ return _objCreatePairNode<const char*>( parent, label, v ); }
cw::object_t* cw::newPairObject( const char* label, const char* v, object_t* parent)
{ return _objCreatePairNode<const char*>( parent, label, v ); }
cw::rc_t cw::objectFromString( const char* s, object_t*& objRef )
{
lex::handle_t lexH;

View File

@ -23,11 +23,12 @@ namespace cw
kDoubleTId = 0x00001000,
kBoolTId = 0x00002000,
kStringTId = 0x00004000,
kVectTId = 0x00008000,
kPairTId = 0x00010000,
kListTId = 0x00020000,
kDictTId = 0x00040000,
kRootTId = 0x00080000,
kCStringTId = 0x00008000, // static string (don't delete)
kVectTId = 0x00010000,
kPairTId = 0x00020000,
kListTId = 0x00040000,
kDictTId = 0x00080000,
kRootTId = 0x00100000,
kHexFl = 0x10000000,
kIdentFl = 0x20000000
@ -139,8 +140,8 @@ namespace cw
const struct object_str* find( const char* label, unsigned flags=0 ) const;
struct object_str* find( const char* label, unsigned flags=0 );
const struct object_str* list_ele( unsigned idx ) const;
struct object_str* list_ele( unsigned idx );
const struct object_str* child_ele( unsigned idx ) const;
struct object_str* child_ele( unsigned idx );
// Set flag 'kNoRecurseFl' to no recurse into the object in search of the value.
// Set flag 'kOptional' if the label is optional and may not exist.
@ -171,6 +172,10 @@ namespace cw
return rc;
}
template< typename T >
struct object_str* insertPair( const char* label, const T& v )
{ return newPairObject(label, v, this); }
unsigned to_string( char* buf, unsigned bufByteN ) const;
void print(const print_ctx_t* c=NULL) const;
@ -187,8 +192,24 @@ namespace cw
object_t* newObject( bool v, object_t* parent=nullptr);
object_t* newObject( float v, object_t* parent=nullptr);
object_t* newObject( double v, object_t* parent=nullptr);
object_t* newObject( char* v, object_t* parent=nullptr);
object_t* newObject( const char* v, object_t* parent=nullptr);
// Return a pointer to the value node.
object_t* newPairObject( const char* label, std::uint8_t v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, std::int8_t v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, std::int16_t v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, std::uint16_t v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, std::int32_t v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, std::uint32_t v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, std::int64_t v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, std::uint64_t v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, bool v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, float v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, double v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, char* v, object_t* parent=nullptr);
object_t* newPairObject( const char* label, const char* v, object_t* parent=nullptr);
rc_t objectFromString( const char* s, object_t*& objRef );
rc_t objectFromFile( const char* fn, object_t*& objRef );
void objectPrintTypes( object_t* o );

View File

@ -12,6 +12,7 @@ namespace cw
template< typename T >
object_t* _objSetLeafValue( object_t* obj, T value )
{
cwLogError(kObjAllocFailRC,"Unhandled object type at leaf node.");
return NULL;
}
@ -25,16 +26,79 @@ namespace cw
return obj;
}
template<> object_t* _objSetLeafValue<uint8_t>( object_t* obj, uint8_t value )
{
if( obj != NULL )
{
obj->u.u8 = value;
obj->type = _objIdToType(kUInt8TId);
}
return obj;
}
template<> object_t* _objSetLeafValue<int16_t>( object_t* obj, int16_t value )
{
if( obj != NULL )
{
obj->u.i16 = value;
obj->type = _objIdToType(kInt16TId);
}
return obj;
}
template<> object_t* _objSetLeafValue<uint16_t>( object_t* obj, uint16_t value )
{
if( obj != NULL )
{
obj->u.u16 = value;
obj->type = _objIdToType(kUInt16TId);
}
return obj;
}
template<> object_t* _objSetLeafValue<int32_t>( object_t* obj, int32_t value )
{
if( obj != NULL )
{
obj->u.i32 = value;
obj->type = _objIdToType(kInt32TId);
obj->type = _objIdToType(kInt32TId);
}
return obj;
}
template<> object_t* _objSetLeafValue<uint32_t>( object_t* obj, uint32_t value )
{
if( obj != NULL )
{
obj->u.u32 = value;
obj->type = _objIdToType(kUInt32TId);
}
return obj;
}
template<> object_t* _objSetLeafValue<int64_t>( object_t* obj, int64_t value )
{
if( obj != NULL )
{
obj->u.i64 = value;
obj->type = _objIdToType(kInt64TId);
}
return obj;
}
template<> object_t* _objSetLeafValue<uint64_t>( object_t* obj, uint64_t value )
{
if( obj != NULL )
{
obj->u.u64 = value;
obj->type = _objIdToType(kUInt64TId);
}
return obj;
}
template<> object_t* _objSetLeafValue<double>( object_t* obj, double value )
{
if( obj != NULL )
@ -65,9 +129,20 @@ namespace cw
return obj;
}
template<> object_t* _objSetLeafValue<const char*>( object_t* obj, const char* value )
{
if( obj != NULL )
{
obj->u.str = (char*)value;
obj->type = _objIdToType(kCStringTId);
}
return obj;
}
template< typename T >
object_t*_objCreateValueNode( object_t* obj, T value, const char* msg=nullptr, unsigned flags=0 )
{
cwLogError(kObjAllocFailRC,"Unhandled type at value node.");
return NULL;
}
@ -104,9 +179,24 @@ namespace cw
template<> object_t* _objCreateValueNode< char*>( object_t* parent, char* value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<const char*>( object_t* parent, const char* value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objSetLeafValue( _objAllocate(), value ) ); }
template<> object_t* _objCreateValueNode<bool>( object_t* parent, bool value, const char* msg, unsigned flags )
{ return _objAppendLeftMostNode( parent, _objSetLeafValue( _objAllocate(), value ) ); }
template< typename T >
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) );
_objCreateValueNode<const char*>( pair, label, msg, flags );
return _objCreateValueNode<T>( pair, value, msg, flags );
}
template< typename T >
rc_t getObjectValue( const T& src, unsigned tid, void* dst, const char* typeLabel )
{