Boost logo

Boost :

From: Pavel Vozenilek (pavel_vozenilek_at_[hidden])
Date: 2004-11-13 04:27:07


"Jonathan Turkanis" wrote:

> Here the code for DuckStuffedWithPork needs to be written just once. I
wouldn't
> want to have to write separate specializations
>
> class style1_oarchive : public text_archive { };
> class style2_oarchive : public text_archive { };
> class style3_oarchive : public text_archive { };
>
> template<>
> void serialize(style1_oarchive &ar, Duck &d){
> // ..
> }
>
> template<>
> void serialize(style2_oarchive &ar, Duck &d){
> // ..
> }
>
> template<>
> void serialize(style3_oarchive &ar, Duck &d){
> // ..
> }
>
> Here I have to repeat the commented-out code three times. If you want to
vary
> styles for two or more types independently this leads to a combinatorial
> explosion.
>

This could be solved by:

class common_style_oarchive : public text_archive {...};
class style1_oarchive : public common_style_oarchive { };
class style2_oarchive : public common_style_oarchive { };
class style3_oarchive : public common_style_oarchive { };

template<>
void serialize(common_style_oarchive &ar, Duck &d){
    // ..
}
This should cover typical situations.

When more combinations are needed there could be something as:

class common_style_oarchive : public text_archive {...};
class style1_oarchive : public common_style_oarchive { };
class style2_oarchive : public common_style_oarchive { };
class style3_oarchive : public common_style_oarchive { };

  template<Archive>
  void serialize(common_style_oarchive&, Duck& d) {
      //
     ....
  }

  template<>
  void serialize(style1_oarchive &ar, Duck &d){
        // switch temporarily style for Duck or whatever
        style_changer_raii_like<Duck> changer(ar, "......");
         serialize<common_style_oarchive>(ar, d);
   }

    template<>
    void serialize(style2_oarchive &ar, Duck &d){

        style_changer_raii_like<Duck> changer(ar, "......");
         serialize<common_style_oarchive>(ar, d);
    }

    template<>
    void serialize(style3_oarchive &ar, Duck &d){

        style_changer_raii_like<Duck> changer(ar, "......");
         serialize<common_style_oarchive>(ar, d);
    }

Would this be enough to keep code bloat down?

/Pavel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk