Boost logo

Boost :

From: Preston A. Elder (prez_at_[hidden])
Date: 2005-01-28 23:26:22


On Fri, 28 Jan 2005 22:10:21 -0500, Preston A. Elder wrote:

> It also makes it easier for people writing custom parsers, since they can
> call the standard validation functions too. Notice, btw, how I made the
> instantiation of the validator static - this should save some cycles
> (not too many, but some) - by only ever creating one instance of the
> validator for each type in each function calling it - as opposed to
> creating an instance of the validator each time it is needed :)

Actually, a better idea would be to replace that 4th value (the 'long')
and have instead the parser passed to the validate function, defaulting,
of course, to validate_internal<T> (which uses lexical_cast).

This way, if they want to define a new TYPE validator, specialize
validate_internal<T>. If they want to define a different way to validate
a specific type (ie. a parser), then create a new 'validator' - which
basically is any function that has:
   template<typename charT>
   T operator()(const std::basic_string<charT> &s);
defined (where T is the type to be returned - left as an exercise for the
use to ensure that it is the same as T the validator is called with).

so we end up with the validate function looking like:
template<class T, class charT>
validate(boost::any &v,
         const std::vector<std::basic_string<charT> >& xs), T *,
         const boost::function1<T, const std::basic_string<charT> &> &parser =
                validate_internal<T>());

You might be guessing where I'm going now. This makes the 'validate'
functions basically only a way to differentiate between how data is stored
(ie. as a value directly, as a vector of values, etc). The actual details
of how to retrieve the data will then be completely left up to the parser,
which could either be a specialization of validate_internal (or the
default version using lexical_cast), a stand-alone function that returns
the correct type, or any class that has an operator() accepting a
(w)string and returns the correct type.

As an added bonus then, the parser doesn't need to know whether its being
stored in a vector, (as it currently does need to in my hacked up version).

More food for thought. Meanwhile, I'm going to alter my hacked up version
to do the above, and I'll post a link to the code when done :)

-- 
PreZ :)
Founder. The Neuromancy Society (http://www.neuromancy.net)

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