Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

# painter_holder.hpp

    00001 // painter_holder.hpp
    00002
    00003 // Copyright (C) 2004 Steven Weiß (steven11@gmx.de)
    00004 //
    00005 // Permission to copy, use, sell and distribute this software is granted
    00006 // provided this copyright notice appears in all copies.
    00007 // Permission to modify the code and to distribute modified code is granted
    00008 // provided this copyright notice appears in all copies, and a notice
    00009 // that the code was modified is included with the copyright notice.
    00010 //
    00011 // This software is provided "as is" without express or implied warranty,
    00012 // and with no claim as to its suitability for any purpose.
    00013
    00014
    00015 #pragma once
    00016 #include <background_wnd/painter_base.hpp>
    00017 #include <background_wnd/detail/general_exception.hpp>
    00018
    00019
    00020 namespace win32 { namespace gui {
    00021
    00025 struct painter_holder
    00026 {
    00027 private:
    00028      std::auto_ptr<painter_base> pPainter_;
    00029      void set_raw_painter(painter_base* pPainter);
    00030
    00031 public:
    00032      // exceptions
    00033      typedef bg_wnd::general_exception<painter_holder, 0> no_painter;
    00034      typedef bg_wnd::general_exception<painter_holder, 1> invalid_painter_cast;
    00035
    00036
    00037      // the big 4
    00038      painter_holder(painter_base* pPainter = NULL);
    00039      painter_holder(const painter_holder& holder);
    00040      virtual ~painter_holder();
    00041      const painter_holder& operator= (const painter_holder& holder);
    00042
    00043      // operations
    00044      void release_painter();
    00045      void set_painter(const painter_base& painter);
    00046      void swap(painter_holder& holder);
    00047
    00048      // attributes
    00049      painter_base& get_painter() const;
    00050      bool painter_available() const;
    00051
    00052 public:
    00053      // template-memberfunctions
    00054
    00056      template <class Painter> Painter& get_painter() const
    00057      {
    00058           Painter* p = dynamic_cast<Painter*> (&get_painter());
    00059           if (p == NULL)                                    // dynamic type of p isn't Painter -> throw
    00060                throw invalid_painter_cast(*this);
    00061           return *p;
    00062      }
    00063
    00065      template <class Painter> void set_painter()
    00066      {
    00067           set_raw_painter(new Painter());
    00068      }
    00069
    00072      template <class Painter> void set_painter(typename Painter::param0 param)
    00073      {
    00074           set_raw_painter(new Painter(param));
    00075      }
    00076
    00078      template <class Painter> void set_painter(typename Painter::param0 param0, typename Painter::param1 param1)
    00079      {
    00080           set_raw_painter(new Painter(param0, param1));
    00081      }
    00082 };
    00083
    00084
    00085 } }  // namespace win32::gui

| by Steven Weiss. You can contact me at steven11@gmx.de.