← Back to team overview

dolfin team mailing list archive

Re: Ugly const_cast?

 

On Thu, Mar 5, 2009 at 9:46 AM, Anders Logg <logg@xxxxxxxxx> wrote:
> 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.

Sure, this could be done, but we'll swamp the object with pointers.
For example, the XMLMap already has 6 non-const pointers. Your
suggestion will double this :)

Ola

>
>> 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
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
>
> iEYEARECAAYFAkmvkV4ACgkQTuwUCDsYZdGVfgCcCB1Ff7tCaaERynVyXFrdSqbE
> Z+gAn2jTgbt+Pt+s8Xjz2rCQW/duJyz2
> =2OeS
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> DOLFIN-dev mailing list
> DOLFIN-dev@xxxxxxxxxx
> http://www.fenics.org/mailman/listinfo/dolfin-dev
>
>



-- 
Ola Skavhaug


Follow ups

References