GNOME Bugzilla – Bug 329281
Parsers could be given a stream with the usual operator >>
Last modified: 2014-10-21 14:32:33 UTC
It is a common way to deal wioth streams to use the << and >> operators. As Parsers can deal with istreams, they could provide a >> operator to take such a stream, as a convenience.
Created attachment 58435 [details] [review] Patch to include the new header file The patch doesn't contain the header itself, as I have no write access to the CVS repository, and CVS needs write access to produce a diff on an added file <sick>.
Created attachment 58436 [details] New header file This file contains the actual header implementing operator>> for parsers objects.
1. The use of a template does not see wise. Wouldn't that match any class, causing mismatches or clashes with other (even non-libxml++ classes) that are used with the stream operator? 2. Wouldn't the use of the stream operator imply that the data can be provided gradually, with repeated calls to the stream operator? That is not the case - parse_stream() pulls the whole docucment from the stream in one go.
Do you have any thoughts about my reply above?
Sorry for the delay, I didn't notive your comment in November... (In reply to comment #3) > Wouldn't that match any class, > causing mismatches or clashes with other (even non-libxml++ classes) that are > used with the stream operator? I don't think so. The first argument of the template must provide a parse_stream method with a single argument of type std::istream&. That's pretty restrictive. > 2. Wouldn't the use of the stream operator imply that the data can be provided > gradually, with repeated calls to the stream operator? That is not the case - > parse_stream() pulls the whole docucment from the stream in one go. Semantically, it seems to me that the stream operator is meant to take from the stream whatever is needed to fill the appropriate right operand. If it's a number, it will take as many digit characters as necessary. For the parser, it will take everything.
namespace xmlpp { template<typename Parser_T> Parser_T& operator>>(std::istream& in, Parser_T& parser) { parser.parse_stream(in); return parser; } } The return type is wrong. It shall be std::istream&. A template is unnecessary and unwise. Overload resolution does not consider function bodies, only function prototypes. Overload resolution may find that the template is a viable function even if Parser_T is a class without a parse_stream() method. Usually this will be no great problem, because 1. It's in namespace xmlpp. Argument-dependent lookup includes it in the set of candidate functions only if Parser_T is also in namespace xmlpp. 2. A non-template function with an exact match of argument types is a better choice than a template function. Still, there is no reason to use a template. A non-template operator will do, and it's less risky. Like so: namespace xmlpp { inline std::istream& operator>>(std::istream& in, Parser& parser) { parser.parse_stream(in); return in; } } Parser::parse_stream() is virtual.
Created attachment 287519 [details] [review] patch: Parser: Add input operator>>() This is a patch with a non-template operator.
I have pushed this patch: https://git.gnome.org/browse/libxml++/commit/?id=2bd4773ca3e89fe83b0cbc8c3b476b682735028f