[Interest] Design question: providing loop status

Sean Murphy Sean.M.Murphy at us.kbr.com
Thu Apr 21 15:55:08 CEST 2022


I've got an existing class that will be used in both Qt and non-Qt/non-UI applications. One of that functions within that class parses large text files with what is essentially a while(!file.eof()) loop. I'd like to add functionality to the class that would provide some sort of parsing status back to anyone using the class - for example "currently on loop X of N". Essentially I'd like to add the equivalent of a Qt signal.

Ideas I've got so far:
1. Use a bunch of #ifdefs to conditionally turn this class into something that inherits from QObject and provide the signal. The downsides to this are currently this is just a POD class, and we make copies of this instances of this class all over the place currently. Since you can't copy QObjects, that would add some complexity. And then it also wouldn't be able to provide status back in non-Qt uses since that would be #ifdef'd out.
2. Modify the class to take an optional progress function pointer with a signature like "void progress(int currentValue, int totalLoops)", and then within the parsing loop, if that function pointer isn't null, call it with the current value and total. If the function pointer is null, the class would behave as it does now - silently parsing without providing any status back. Then whatever code is instantiating one of these classes can supply their own progress function.

I'm curious how other people have handled something like this? Right now I'm leaning more toward the function pointer solution since that would work for both Qt and non-Qt cases. I'm sure there's other ideas I haven't considered.

Thanks in advance!
Sean





This e-mail, including any attached files, may contain confidential information, privileged information and/or trade secrets for the sole use of the intended recipient. Any review, use, distribution, or disclosure by others is strictly prohibited. If you are not the intended recipient (or authorized to receive information for the intended recipient), please contact the sender by reply e-mail and delete all copies of this message.


More information about the Interest mailing list