Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-08-18 12:46:40


"Peter Dimov" <pdimov_at_[hidden]> writes:

> David Abrahams wrote:
>>
>> FWIW, Boost.Function is overkill for many simple cases. This might
>> be a case where the FS library should just provide a class with a
>> virtual function:
>>
>> struct checker
>> {
>> virtual ~checker() {}
>> virtual bool operator()( std string const& ) = 0;
>>
>> shared_ptr<checker> next; // suggested.
>> };
>
> Boost.Function makes things simpler for the user.
>
> enum check_type { check_posix, check_windows, ... };
> bool my_name_checker(std::string const & s, check_type t);
>
> Compare
>
> bind(my_name_checker, _1, check_posix)
>
> against

  <snip>

So I left out a few bits:

        struct checker_impl_base
        {
             virtual ~checker() {}
             virtual bool operator()( std string const& ) const = 0;
        };
        
        template <class F>
        struct checker_impl : checker_impl_base
        {
             checker_impl(F const& f)
               : m_f(f) {}
   
             bool operator()( std string const& s ) const
             {
                return m_f(s);
             }
        };

        struct checker
        {
             template <class F>
             checker(F const& f)
               : m_impl(new checker_impl<F>(f))
             {}

             bool operator()( std string const& s ) const
             {
                return (*m_impl)(s);
             }

             shared_ptr<checker_impl_base> m_impl;
             shared_ptr<checker_impl_base> m_next; // suggested.
        };

Still smaller (and probably a little more portable) than
Boost.Function.

> It is still possible to substitute a homegrown function<bool(string)> (or
> function<bool(char const *)>) equivalent for portability reasons, of
> course.

I guess that's done now?

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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