I have a binary packet format for which I have to implement C ++ Reader. The library uses QT4, and the packet source can be any QIODevice, for example, QTcpSocket, QFile or QBuffer. The format includes packet format, and each packet may have too many sub-structures.
Reader API has many possible approaches:
read packets (status & status); - Returns from the value, and returns the error status through the context logic. Packet * OK variable (if not null) depends on it, then type correct or incorrect. Read Packet * (); - If there is no return on error or if there is not enough data, then any other method bool isError (); This can be found in the previous one by defining a fine parameter as the default of NULL . read status (packet & amp;; packet); - If the returned status is OK , the value of the reading is kept in the packet variable, otherwise it indicates an error or eof. read packets (); - A special "empty packet" value comes back in the case of EOF or error by value. Definitely there are other possible combinations, call wasError () was. The best option is not to approach Approach 1, 2 and 4 to caller the result to store a result as a separate variable. Approaches 2 and 3 involve messing with a heap, which I do not want to for obvious reasons. Approach 1 does not make clear what is returned in case of error, approach 5 fixes the fix, but introduces a special "blank" flag in packet structure, although it is probably not there. I can take the 5th approach, but information about a packet and position, but it introduces another "synthetic" type and still opens the question "Is there any error in packet What will happen in the field? "
Or I can take the third approach instead, QSharedPointer , so the caller does not have to manually mess with the heap but packet structure might already be a type of smart pointer (a shared square) that is for pimple Ho. Perhaps I can use that internal indicator instead and a isNull () method, such as Qstring. Is there a better, or traditional way of doing this?
OK, I have moved ahead and implemented it.
The signature method of reading is:
packet read packet (bool * error = NULL); The packet class looks like this:
class packet {inline packet (): p (NULL) {} // has returned to this error or EOF Packet (Constant Header and Header, Constant Data and Data); Inline Bull isNull () {Return P == Faucet;} Private: QSharedDataPointer & lt; PacketPrivate & gt; P; }; If the packet reader did not have the error argument, then the bool wasError () method was used to get the error code The readPacket () function has been provided. This implementation allows me to:
-
to make brilliant edges (while (! (Packet / Reader-> readPacket ()). IsNull )) - To avoid unrelated fields such as "error" or "null" in the packet class.
- Deleting manual allocation or pointers, or towards the collar To avoid obviously messing with smart pointers.
- Avoid Unregistered Fields When Using Default Constructor .
It is still far from right because either error was used to provide arguments or to call was error < / Code> later. But I believe that the only way to force the caller to check the errors will be to use exceptions, but I do not want to do it for portability reasons. < / Html>
Comments
Post a Comment