QStringParser::formatArg() for pointer

Discuss anything related to product development
Post Reply
seasoned_geek
Posts: 253
Joined: Thu Jun 11 2020 12:18 pm

QStringParser::formatArg() for pointer

Post by seasoned_geek »

Ripping out some really bad code in this port to CopperSpice I've stumbled onto an issue. I can find nothing in the documentation for QStringParser::formatArg() that will let one format an address.

https://www.copperspice.com/docs/cs_api/class_qstringparser.html#a07fa9007c038539e56ce22ca6347d416

The bad code was using the sprintf() family and %p to get a hex address. So far I haven't been able to properly build the message.

Code: Select all

QTreeWidgetItem *item

 msg = QStringParser::formatArg( msg, reinterpret_cast<void *>(item), 0, 16);
no known conversion for argument 1 from ‘void*’ to 

 msg = QStringParser::formatArg( msg, item, 0, 16);
no known conversion for argument 1 from ‘QTreeWidgetItem*’
It's almost like there needs to be another method like this sans the precision.

Code: Select all

T QStringParser::formatArg 	( 	const T &  	str,
		V  	value,
		int  	fieldwidth = 0,
		char  	format = 'g',
		int  	precision = 6,
		QChar32  	fillChar = QChar32(' ') 
	) 		
The format code could be 'x' or 'H' to indicate hex. Perhaps even A for address?
ansel
Posts: 152
Joined: Fri Apr 10 2015 8:23 am

Re: QStringParser::formatArg() for pointer

Post by ansel »

It looks like you want a way to convert pointer to its string representation as a hex address. The simplest way to do this in CopperSpice is to convert the pointer into an unsigned integer of the appropriate size:

Code: Select all

QStringParser::formatArg( msg, reinterpret_cast<quintptr>(item), 0, 16);
Ansel Sermersheim
CopperSpice Cofounder
seasoned_geek
Posts: 253
Joined: Thu Jun 11 2020 12:18 pm

Re: QStringParser::formatArg() for pointer

Post by seasoned_geek »

Yeah.

The reinterpret_cast<>() I did was a final act of desperation. It would never survive code review because engineering coding standards bar of the use of reinterpret_cast.

If QStringParser is what we are supposed to use then it should have %p-like functionality built in.

That was the real gist of it.
barbara
Posts: 446
Joined: Sat Apr 04 2015 2:32 am
Contact:

Re: QStringParser::formatArg() for pointer

Post by barbara »

We are very well aware that a C style cast or a reinterpret_cast is not ideal code. To be clear, it is the philosophy of the CS Team to avoid this type of code in CopperSpice whenever possible.

If we were to add a method to QStringParser it would still need to use a C style cast in the implementation. So the code we suggested you use would be almost identical to the internal code, users would just not see the reinterpret_cast.

There is another reason we did not add the overload. The parameter would accept a void * and then it may be called in some very unexpected cases.

Barbara
seasoned_geek
Posts: 253
Joined: Thu Jun 11 2020 12:18 pm

Re: QStringParser::formatArg() for pointer

Post by seasoned_geek »

While what you say may well be true, the main difference is that FDA and other regulated environments where code must be externally reviewed for compliance with BARR or Joint Strike Coding standards means code with reinterpret_cast<> fails and one cannot proceed with certification/validation/clinical trials.

The void pointer issue will not lead to unexpected results if the the method has a different name.

formatPointer( void *)
formatAddressHex( void *)

etc.
barbara
Posts: 446
Joined: Sat Apr 04 2015 2:32 am
Contact:

Re: QStringParser::formatArg() for pointer

Post by barbara »

We have reviewed this and adding a new method would have side effects and contain an reinterpret_cast. At this time we are not making a change however it has been placed on our list of things to reconsider at a later date.
Post Reply