DynPtr: A pointer with implicit dynamic cast

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Calum Grant

    #1

    DynPtr: A pointer with implicit dynamic cast

    Here's a little utility I thought I'd share.

    (For those familiar with ATL::CComQIPtr, the concept is similar).


    ====

    #ifndef DYNPTR_H_INCLUD ED
    #define DYNPTR_H_INCLUD ED

    /*
    DynPtr - A pointer with an implicit dynamic-cast

    Purpose: When code requires a lot of dynamic_casting ,
    it can be difficult to read, so DynPtr is a pointer
    that implicitly
    dynamic_casts, to save the bother of writing dynamic_cast all
    the time.

    e.g.

    Integer *i = dynamic_cast<In teger*>(value);
    Integer *ci = dynamic_cast<co nst Integer*>(value );

    vs

    DynPtr<Integer> i(value);
    DynPtr<const Integer> ci(value);

    Written by Calum Grant. http://visula.org/calum
    Public domain - free of copyright.
    */

    template<class T>
    class DynPtr
    {
    T *p;
    public:
    DynPtr() : p(0) { }

    DynPtr(const DynPtr &q) : p(q.get()) { }
    explicit DynPtr(T *q) : p(q) { }

    template<class Q>
    explicit DynPtr(const DynPtr<Q> &q) : p(dynamic_cast< T*>(q.get())) { }

    template<class Q>
    explicit DynPtr(Q *q) : p(dynamic_cast< T*>(q)) { }

    DynPtr &operator=(cons t DynPtr &q) { p = q.get(); return *this; }
    DynPtr &operator=(T *p) { p = q; return *this; }

    template<class Q> DynPtr<T> &operator=(cons t DynPtr<Q> &q)
    {
    p = dynamic_cast<T* >(q.get());
    return *this;
    }

    template<class Q> DynPtr<T> &operator=(Q *q)
    {
    p = dynamic_cast<T* >(q);
    return *this;
    }

    operator bool() const { return p!=0; }
    T *operator->() const { return p; }
    T &operator*() const { return p; }
    T *get() const { return p; }
    };

    #endif
  • int2str@gmail.com

    #2
    Re: DynPtr: A pointer with implicit dynamic cast


    Calum Grant wrote:[color=blue]
    > template<class T>
    > class DynPtr
    > {
    > T *p;
    > public:
    > DynPtr() : p(0) { }
    >
    > DynPtr(const DynPtr &q) : p(q.get()) { }
    > explicit DynPtr(T *q) : p(q) { }
    >
    > template<class Q>
    > explicit DynPtr(const DynPtr<Q> &q) : p(dynamic_cast< T*>(q.get())) { }
    >
    > template<class Q>
    > explicit DynPtr(Q *q) : p(dynamic_cast< T*>(q)) { }
    >
    > DynPtr &operator=(cons t DynPtr &q) { p = q.get(); return *this; }
    > DynPtr &operator=(T *p) { p = q; return *this; }
    >
    > template<class Q> DynPtr<T> &operator=(cons t DynPtr<Q> &q)
    > {
    > p = dynamic_cast<T* >(q.get());
    > return *this;
    > }
    >
    > template<class Q> DynPtr<T> &operator=(Q *q)
    > {
    > p = dynamic_cast<T* >(q);
    > return *this;
    > }
    >
    > operator bool() const { return p!=0; }
    > T *operator->() const { return p; }
    > T &operator*() const { return p; }[/color]

    This doesn't look like it should compile.
    Did you mean 'return *p;' maybe?
    [color=blue]
    > T *get() const { return p; }
    > };
    >
    > #endif[/color]

    Cheers,
    Andre

    Comment

    • Neelesh Bodas

      #3
      Re: DynPtr: A pointer with implicit dynamic cast


      Calum Grant wrote:
      [color=blue]
      >
      > /*
      > DynPtr - A pointer with an implicit dynamic-cast
      >
      > Purpose: When code requires a lot of dynamic_casting ,[/color]

      IMHO, one should reconsider the design.

      Comment

      Working...