From 398fa3282b47612afa5a0ff558d60bd8a463f13c Mon Sep 17 00:00:00 2001 From: Jonathon Jongsma Date: Tue, 10 Jul 2007 23:32:47 -0500 Subject: Add ability to use dynamic casting with the return values from more functions, including: Context::get_target() Context::get_target() const Context::get_source() Context::get_source() const Context::get_group_target() Context::get_group_target() const Context::pop_group() * tests/test-context.cc: a few additional tests to verify the const / non-const versions both work with dynamic casting. --- ChangeLog | 14 +++++++++ cairomm/context.cc | 80 +++++++++++++++++++++++++++++++++++++++++++++------ tests/test-context.cc | 32 +++++++++++++++++++++ 3 files changed, 117 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 09a0255..71f78eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2007-07-10 Jonathon Jongsma + + * cairomm/context.cc: add ability to use dynamic casting with the return + values from more functions, including: + Context::get_target() + Context::get_target() const + Context::get_source() + Context::get_source() const + Context::get_group_target() + Context::get_group_target() const + Context::pop_group() + * tests/test-context.cc: a few additional tests to verify the const / + non-const versions both work with dynamic casting. + 2007-07-10 Jonathon Jongsma * docs/reference/cairomm.css: Improve the documentation style a little bit diff --git a/cairomm/context.cc b/cairomm/context.cc index ea398fc..1b70701 100644 --- a/cairomm/context.cc +++ b/cairomm/context.cc @@ -18,6 +18,9 @@ #include #include +#include +#include +#include /* M_PI is defined in math.h in the case of Microsoft Visual C++ */ #if defined(_MSC_VER) @@ -538,10 +541,8 @@ Operator Context::get_operator() const return result; } -RefPtr Context::get_source() +static RefPtr get_pattern_wrapper (cairo_pattern_t* pattern) { - cairo_pattern_t* pattern = cairo_get_source(m_cobject); - check_object_status_and_throw_exception(*this); cairo_pattern_type_t pattern_type = cairo_pattern_get_type (pattern); switch (pattern_type) { @@ -562,11 +563,18 @@ RefPtr Context::get_source() } } +RefPtr Context::get_source() +{ + cairo_pattern_t* pattern = cairo_get_source(m_cobject); + check_object_status_and_throw_exception(*this); + return get_pattern_wrapper (pattern); +} + RefPtr Context::get_source() const { cairo_pattern_t* pattern = cairo_get_source(m_cobject); check_object_status_and_throw_exception(*this); - return RefPtr(new Pattern(pattern, false /* does not have reference */)); + return RefPtr::cast_const (get_pattern_wrapper (pattern)); } double Context::get_tolerance() const @@ -644,18 +652,72 @@ void Context::get_matrix(Matrix& matrix) check_object_status_and_throw_exception(*this); } +static +RefPtr get_surface_wrapper (cairo_surface_t* surface) +{ + cairo_surface_type_t surface_type = cairo_surface_get_type (surface); + switch (surface_type) + { + case CAIRO_SURFACE_TYPE_IMAGE: + return RefPtr(new ImageSurface(surface, false /* does not have reference */)); + break; +#if CAIRO_HAS_PDF_SURFACE + case CAIRO_SURFACE_TYPE_PDF: + return RefPtr(new PdfSurface(surface, false /* does not have reference */)); + break; +#endif +#if CAIRO_HAS_PS_SURFACE + case CAIRO_SURFACE_TYPE_PS: + return RefPtr(new PsSurface(surface, false /* does not have reference */)); + break; +#endif +#if CAIRO_HAS_XLIB_SURFACE + case CAIRO_SURFACE_TYPE_XLIB: + return RefPtr(new XlibSurface(surface, false /* does not have reference */)); + break; +#endif +#if CAIRO_HAS_GLITZ_SURFACE + case CAIRO_SURFACE_TYPE_GLITZ: + return RefPtr(new GlitzSurface(surface, false /* does not have reference */)); + break; +#endif +#if CAIRO_HAS_QUARTZ_SURFACE + case CAIRO_SURFACE_TYPE_QUARTZ: + return RefPtr(new QuartzSurface(surface, false /* does not have reference */)); + break; +#endif +#if CAIRO_HAS_WIN32_SURFACE + case CAIRO_SURFACE_TYPE_WIN32: + return RefPtr(new Win32Surface(surface, false /* does not have reference */)); + break; +#endif +#if CAIRO_HAS_SVG_SURFACE + case CAIRO_SURFACE_TYPE_SVG: + return RefPtr(new SvgSurface(surface, false /* does not have reference */)); + break; +#endif + // the following surfaces are not directly supported in cairomm yet + case CAIRO_SURFACE_TYPE_DIRECTFB: + case CAIRO_SURFACE_TYPE_OS2: + case CAIRO_SURFACE_TYPE_BEOS: + case CAIRO_SURFACE_TYPE_XCB: + default: + return RefPtr(new Surface(surface, false /* does not have reference */)); + } +} + RefPtr Context::get_target() { cairo_surface_t* surface = cairo_get_target(const_cast(m_cobject)); check_object_status_and_throw_exception(*this); - return RefPtr(new Surface(surface, false /* does not have reference */)); + return get_surface_wrapper (surface); } RefPtr Context::get_target() const { cairo_surface_t* surface = cairo_get_target(const_cast(m_cobject)); check_object_status_and_throw_exception(*this); - return RefPtr(new Surface(surface, false /* does not have reference */)); + return RefPtr::cast_const (get_surface_wrapper (surface)); } Path* Context::copy_path() const @@ -694,7 +756,7 @@ RefPtr Context::pop_group() { cairo_pattern_t* pattern = cairo_pop_group(m_cobject); check_object_status_and_throw_exception(*this); - return RefPtr(new Pattern(pattern)); + return get_pattern_wrapper(pattern); } void Context::pop_group_to_source() @@ -713,7 +775,7 @@ RefPtr Context::get_group_target() throw_exception(CAIRO_STATUS_NULL_POINTER); } - return RefPtr(new Surface(surface, false)); + return get_surface_wrapper(surface); } RefPtr Context::get_group_target() const @@ -726,7 +788,7 @@ RefPtr Context::get_group_target() const throw_exception(CAIRO_STATUS_NULL_POINTER); } - return RefPtr(new Surface(surface, false)); + return get_surface_wrapper(surface); } } //namespace Cairo diff --git a/tests/test-context.cc b/tests/test-context.cc index 1583842..9cfaec2 100644 --- a/tests/test-context.cc +++ b/tests/test-context.cc @@ -97,6 +97,12 @@ test_source () BOOST_CHECK_EQUAL (1.0, r); BOOST_CHECK_EQUAL (0.5, g); BOOST_CHECK_EQUAL (0.25, b); + + // now try for const objects.. + Cairo::RefPtr cr2 = cr; + Cairo::RefPtr retrieved_solid2 = + Cairo::RefPtr::cast_dynamic(cr2->get_source ()); + BOOST_REQUIRE (retrieved_solid2); } cr->set_source (gradient_pattern); @@ -288,6 +294,31 @@ test_current_point () BOOST_CHECK (y == 3.0); } +void +test_target () +{ + Cairo::RefPtr surf = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, 10, 10); \ + Cairo::RefPtr cr = Cairo::Context::create(surf); + + Cairo::RefPtr target_surface = + Cairo::RefPtr::cast_dynamic(cr->get_target ()); + Cairo::RefPtr bad_surface = + Cairo::RefPtr::cast_dynamic(cr->get_target ()); + BOOST_CHECK (target_surface); + BOOST_CHECK (!bad_surface); + + // now check for const objects... + Cairo::RefPtr cr2 = Cairo::Context::create(surf); + + Cairo::RefPtr target_surface2 = + Cairo::RefPtr::cast_dynamic(cr2->get_target ()); + Cairo::RefPtr bad_surface2 = + Cairo::RefPtr::cast_dynamic(cr2->get_target ()); + BOOST_CHECK (target_surface2); + BOOST_CHECK (!bad_surface2); + +} + test_suite* init_unit_test_suite(int argc, char* argv[]) { @@ -312,6 +343,7 @@ init_unit_test_suite(int argc, char* argv[]) test->add (BOOST_TEST_CASE (&test_draw)); test->add (BOOST_TEST_CASE (&test_clip)); test->add (BOOST_TEST_CASE (&test_current_point)); + test->add (BOOST_TEST_CASE (&test_target)); return test; } -- cgit v1.2.3