Boost logo

Boost :

From: Kevlin Henney (kevlin_at_[hidden])
Date: 2000-09-01 08:52:22


In message <006c01c01411$b4b78720$480524d4_at_pdimov>, Peter Dimov
<pdimov_at_[hidden]> writes
>I think that I'll change ref<T>::is<U> to return U (const) * instead of
>bool, to allow usage of the form
>
>if(U * p = rx.is<U>())
>{
>}
>
>being backward compatible with the current usage
>
>if(rx.is<U>())
>{
>}
>
>This covers the to_ptr case - I can see its usefulness immediately. The
>pattern is similar to the preferred dynamic_cast usage as described in D &
>E.

Although source-level compatible, I think the naming may be a little
misleading, but YMMV.

>I think that I'm sold on the implicit conversion to void const * too - this
>will replace my operator==(nil). Since ref<> explicitly provides all
>comparison operators, the conversion would do no harm.

Yes, and I think that the usage is more natural, and actually narrows
the interface the user must understand, ie I don't think they need now
be aware of nil.

>Would you mind showing some scenarios to demonstrate
>
>(1) typical copy_to() usage (my favourite spelling for this would be
>'extract')

copy_to makes sense when you already have a variable (or container, or
whatever) and you wish to assign to it, but you would rather not receive
an exception, ie either ignore it:

        any var;
        ...
        std::string text;
        var.copy_to(text);

Or act on it:

        if(!copy_to(text))
            ...

The alternatives leads to more user code and more duplication of type
names:

        try
        {
           text = any_cast<std::string>(var);
        }
        catch(std::bad_cast &)
        {
            ...
        }

Or:

        if(std::string * converted = var.to_ptr<std::string>(var))
            text = *converted;
        else
            ...

>(2) typical type_info() usage.

The following allows a simple sorting wrt type, and verification of
result:

        bool type_info_ordering(const any & lhs, const any & rhs)
        {
            return lhs.type_info().before(rhs.type_info());
        }

        void print_type_info(const any & to_print)
        {
            cout << to_print.type_info().name() << endl;
        }

        void sort_example()
        {
            vector<any> many;
            ... // set up with values
            sort(many.begin(), many.end(), type_info_ordering);
            for_each(many.begin(), many.end(), print_type_info);
        }

The following demonstrates matching/filtering:

        struct is_type : binary_function<any, const type_info *, bool>
        {
            bool operator()(
                const any & operand, const type_info * type) const
            {
                return operand.type_info() == *type;
            }
        };

        void find_example()
        {
            vector<any> many;
            ...
            vector<any>::iterator first_int =
                find_if(
                    many.begin(), many.end(),
                    bind2nd(is_type(), &typeid(int)));
            ...
        }

____________________________________________________________

  Kevlin Henney phone: +44 117 942 2990
  Curbralan Ltd mobile: +44 7801 073 508
  kevlin_at_[hidden] fax: +44 870 052 2289
____________________________________________________________


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