← Back to team overview

dolfin team mailing list archive

Re: Ugly const_cast?

 

On Thu, Mar 05, 2009 at 09:36:05AM +0100, Ola Skavhaug wrote:
> As some might have seen from the buildbot's failures last night, we're
> currently implementing a nested stack based xml parser that enables
> the reuse of the different parsers (we call them XMLHandlers) when
> reading and writing data to xml files. We want to use the handlers for
> both input and output, and we run into problems at construction time.
> For instance, when writing a std::vector<uint, int> x to file, we get
> a const reference, and need to cast away the constness in order to
> construct the XMLArray object:
> 
> 
> void NewXMLFile::operator<<(const std::vector<int>& x)
> {
>   XMLArray xml_array(const_cast<std::vector<int>&>(x), *this);
>   xml_array.write(filename);
> }
> 
> Does anyone object to this const_cast?

Is this because the handlers are used both for reading and writing?

One option would be to split it into two files, one for reading and
one for writing but that is inconvenient. How about instead having two
objects as arguments to the constructor:

  XMLFoo(Foo* input_foo, const Foo* output_foo, NewXMLFile& parser);

Then we need to dolfin_assert() the pointers and use the correct one
for input and output.

> Btw, we should probably not send the filename here, but the ofstream
> being written to, in order to reuse the output methods as well as the
> parsing methods.

Sounds good.

-- 
Anders

Attachment: signature.asc
Description: Digital signature


Follow ups

References