#9 Use ArgTraits instead of ValueExtractor specialization

closed
None
2
2007-06-14
2007-05-02
No

The use of the ValueExtractor class to specialize the handling of string arguments is ugly an inflexible (I know I wrote it :)). It was required as a quick fix at the time during heavy refactoring of the library and has worked well in practice. However, I ran in to a problem today when I wanted to implement a string-like type that should be used as a ValueArg type. This did not work because operator>> splits strings on spaces, i.e. the same reason we needed the specialization in the first place. A better solution would be to use the traits design pattern and create a traits class for each possible value type and then pass a traits object to the value extraction function for specialization purposes. In this way the type of the parameter will not determine the specialization, but a trait of the type will.

For this case only a single trait is required, AssignType, maybe we should consider if there are other useful meta-info we are interested in putting in the traits object as well. The categories I can come up with for AssignType is, StringLike (i.e. use assignment), ValueLike (i.e. use operator>>) and possibly Settable (i.e. use _value.Set()), but this is not really required because overloaded operator= can be used for this.

Discussion

  • Mike Smoot

    Mike Smoot - 2007-05-04

    Logged In: YES
    user_id=27603
    Originator: NO

    I'm not sure I understand traits well enough to comment coherently, but I'd definitely be interested in seeing what you're able to produce!

     
  • Daniel Aarno

    Daniel Aarno - 2007-05-04

    Logged In: YES
    user_id=330479
    Originator: YES

    I'll implement something to show the idea, when I get some spare time, and post a diff on the mailing-list and it will probably become more clear. Traits are used e.g. in the STL to determine the characteristics of itarators in order to specialize algorithms and make them more efficient, the boost graph library also makes use of that pattern if I recall correctly.

     
  • Daniel Aarno

    Daniel Aarno - 2007-05-11

    Logged In: YES
    user_id=330479
    Originator: YES

    File Added: ValueArg.diff

     
  • Daniel Aarno

    Daniel Aarno - 2007-05-11

    Suggested implementation as unidiff

     
  • Daniel Aarno

    Daniel Aarno - 2007-05-11

    Example showing the use of traits for new types

     
  • Daniel Aarno

    Daniel Aarno - 2007-05-11

    Logged In: YES
    user_id=330479
    Originator: YES

    File Added: test11.cpp

     
  • Daniel Aarno

    Daniel Aarno - 2007-05-11

    Logged In: YES
    user_id=330479
    Originator: YES

    I've attached a unidiff showing a suggested implementation for ValueArg and an example of how it can be used for new types. Please have a look at this and provide some comments. If it looks OK I'll go ahead and implement it for MultiArg as well and provide the relevant tests etc.

     
  • Daniel Aarno

    Daniel Aarno - 2007-05-11

    Logged In: YES
    user_id=330479
    Originator: YES

    Ohh, and I should say, no rush...

     
  • Mike Smoot

    Mike Smoot - 2007-05-14

    Logged In: YES
    user_id=27603
    Originator: NO

    Very cool. Definitely a lot cleaner than the previous approach! I vote for moving forward and implementing this for MultiArg, etc..

     
  • Daniel Aarno

    Daniel Aarno - 2007-06-14
    • status: open --> closed
     
  • Daniel Aarno

    Daniel Aarno - 2007-06-14

    Logged In: YES
    user_id=330479
    Originator: YES

    Changes committed to cvs

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks