diff options
author | Carl Worth <cworth@cworth.org> | 2005-03-10 08:59:11 +0000 |
---|---|---|
committer | Carl Worth <cworth@cworth.org> | 2005-03-10 08:59:11 +0000 |
commit | c67ba13a6e715fa3bd4e785ed72c093f791e2774 (patch) | |
tree | 1e4207d696902ea1050dcf5b0353590c1870cbd6 | |
parent | 12abad4759286d8642cd02f258fba0749ea20b8b (diff) |
Originally: 2005-02-24 Carl Worth <cworth@cworth.org>
Add const qualifier to cairo_user_data_key_t arguments.
Originally: 2005-02-15 Kristian Høgsberg <krh@redhat.com>
Add these two functions to set and get user data on an surface.
Function prototypes for new functions.
Test case for user data functions.
-rw-r--r-- | ChangeLog | 18 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-atsui.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-ft.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-glitz.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-matrix.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-pattern.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-pdf.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-png.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-ps.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-quartz.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-surface.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-xcb.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo-xlib.sgml | 3 | ||||
-rw-r--r-- | doc/public/tmpl/cairo.sgml | 48 | ||||
-rw-r--r-- | src/cairo-surface.c | 90 | ||||
-rw-r--r-- | src/cairo.h | 33 | ||||
-rw-r--r-- | src/cairo_surface.c | 90 | ||||
-rw-r--r-- | src/cairoint.h | 1 | ||||
-rw-r--r-- | test/.cvsignore | 3 | ||||
-rw-r--r-- | test/Makefile.am | 5 | ||||
-rw-r--r-- | test/user-data.c | 70 | ||||
-rw-r--r-- | test/user_data.c | 70 |
22 files changed, 444 insertions, 20 deletions
@@ -1,3 +1,21 @@ +2005-03-10 Carl Worth <cworth@cworth.org> + + Originally: 2005-02-24 Carl Worth <cworth@cworth.org> + + * src/cairo_surface.c (cairo_surface_set_user_data) + (cairo_surface_get_user_data): + * src/cairo.h: Add const qualifier to cairo_user_data_key_t + arguments. + + Originally: 2005-02-15 Kristian Høgsberg <krh@redhat.com> + + * src/cairo_surface.c (cairo_surface_get_data, cairo_surface_set_data): + Add these two functions to set and get user data on an surface. + + * src/cairo.h: Function prototypes for new functions. + + * test/user_data.c: Test case for user data functions. + 2005-03-09 Carl Worth <cworth@cworth.org> * test/cairo_test.c (cairo_test): Move filename initialization up diff --git a/doc/public/tmpl/cairo-atsui.sgml b/doc/public/tmpl/cairo-atsui.sgml index 0d957ecdf..a47ee8c68 100644 --- a/doc/public/tmpl/cairo-atsui.sgml +++ b/doc/public/tmpl/cairo-atsui.sgml @@ -14,6 +14,9 @@ ATSUI Fonts </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### FUNCTION cairo_atsui_font_create ##### --> <para> diff --git a/doc/public/tmpl/cairo-ft.sgml b/doc/public/tmpl/cairo-ft.sgml index bcf52ac34..c41a114a0 100644 --- a/doc/public/tmpl/cairo-ft.sgml +++ b/doc/public/tmpl/cairo-ft.sgml @@ -14,6 +14,9 @@ FreeType Fonts </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### FUNCTION cairo_ft_font_create ##### --> <para> diff --git a/doc/public/tmpl/cairo-glitz.sgml b/doc/public/tmpl/cairo-glitz.sgml index 101eb9e3e..cb8caf76a 100644 --- a/doc/public/tmpl/cairo-glitz.sgml +++ b/doc/public/tmpl/cairo-glitz.sgml @@ -14,6 +14,9 @@ Glitz backend </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### FUNCTION cairo_set_target_glitz ##### --> <para> diff --git a/doc/public/tmpl/cairo-matrix.sgml b/doc/public/tmpl/cairo-matrix.sgml index dc24c5754..1c9fc9ad3 100644 --- a/doc/public/tmpl/cairo-matrix.sgml +++ b/doc/public/tmpl/cairo-matrix.sgml @@ -38,6 +38,9 @@ cairo_matrix_t </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### TYPEDEF cairo_matrix_t ##### --> <para> diff --git a/doc/public/tmpl/cairo-pattern.sgml b/doc/public/tmpl/cairo-pattern.sgml index 84728212b..4687bd6c8 100644 --- a/doc/public/tmpl/cairo-pattern.sgml +++ b/doc/public/tmpl/cairo-pattern.sgml @@ -14,6 +14,9 @@ cairo_pattern_t </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### TYPEDEF cairo_pattern_t ##### --> <para> diff --git a/doc/public/tmpl/cairo-pdf.sgml b/doc/public/tmpl/cairo-pdf.sgml index e627c236e..24fcd9fce 100644 --- a/doc/public/tmpl/cairo-pdf.sgml +++ b/doc/public/tmpl/cairo-pdf.sgml @@ -14,6 +14,9 @@ PDF Backend </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### FUNCTION cairo_set_target_pdf ##### --> <para> diff --git a/doc/public/tmpl/cairo-png.sgml b/doc/public/tmpl/cairo-png.sgml index d4d5a66b5..f4704d45b 100644 --- a/doc/public/tmpl/cairo-png.sgml +++ b/doc/public/tmpl/cairo-png.sgml @@ -14,6 +14,9 @@ PNG Backend </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### FUNCTION cairo_set_target_png ##### --> <para> diff --git a/doc/public/tmpl/cairo-ps.sgml b/doc/public/tmpl/cairo-ps.sgml index 70b2e7e8f..3fe540904 100644 --- a/doc/public/tmpl/cairo-ps.sgml +++ b/doc/public/tmpl/cairo-ps.sgml @@ -14,6 +14,9 @@ PS Backend </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### FUNCTION cairo_set_target_ps ##### --> <para> diff --git a/doc/public/tmpl/cairo-quartz.sgml b/doc/public/tmpl/cairo-quartz.sgml index 04c9bc61f..44dd75c9f 100644 --- a/doc/public/tmpl/cairo-quartz.sgml +++ b/doc/public/tmpl/cairo-quartz.sgml @@ -14,6 +14,9 @@ Quartz Backend </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### FUNCTION cairo_set_target_quartz_context ##### --> <para> diff --git a/doc/public/tmpl/cairo-surface.sgml b/doc/public/tmpl/cairo-surface.sgml index 2f8ad470c..7b93a7c5e 100644 --- a/doc/public/tmpl/cairo-surface.sgml +++ b/doc/public/tmpl/cairo-surface.sgml @@ -14,6 +14,9 @@ cairo_surface_t </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### TYPEDEF cairo_surface_t ##### --> <para> diff --git a/doc/public/tmpl/cairo-xcb.sgml b/doc/public/tmpl/cairo-xcb.sgml index e5e1ee912..58d8e1558 100644 --- a/doc/public/tmpl/cairo-xcb.sgml +++ b/doc/public/tmpl/cairo-xcb.sgml @@ -14,6 +14,9 @@ XCB Backend </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### FUNCTION cairo_set_target_xcb ##### --> <para> diff --git a/doc/public/tmpl/cairo-xlib.sgml b/doc/public/tmpl/cairo-xlib.sgml index b18e76aae..d14a53827 100644 --- a/doc/public/tmpl/cairo-xlib.sgml +++ b/doc/public/tmpl/cairo-xlib.sgml @@ -14,6 +14,9 @@ XLib Backend </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### FUNCTION cairo_set_target_drawable ##### --> <para> diff --git a/doc/public/tmpl/cairo.sgml b/doc/public/tmpl/cairo.sgml index a9e195c73..88d688162 100644 --- a/doc/public/tmpl/cairo.sgml +++ b/doc/public/tmpl/cairo.sgml @@ -24,6 +24,9 @@ Drawing contexts. </para> +<!-- ##### SECTION Stability_Level ##### --> + + <!-- ##### TYPEDEF cairo_t ##### --> <para> @@ -688,7 +691,7 @@ Drawing contexts. @num_glyphs: -<!-- ##### FUNCTION cairo_current_font ##### --> +<!-- ##### FUNCTION cairo_get_font ##### --> <para> </para> @@ -697,7 +700,7 @@ Drawing contexts. @Returns: -<!-- ##### FUNCTION cairo_current_font_extents ##### --> +<!-- ##### FUNCTION cairo_get_font_extents ##### --> <para> </para> @@ -771,6 +774,17 @@ Drawing contexts. @font: +<!-- ##### FUNCTION cairo_font_extents ##### --> +<para> + +</para> + +@font: +@font_matrix: +@extents: +@Returns: + + <!-- ##### FUNCTION cairo_font_glyph_extents ##### --> <para> @@ -794,7 +808,7 @@ Drawing contexts. @height: -<!-- ##### FUNCTION cairo_current_operator ##### --> +<!-- ##### FUNCTION cairo_get_operator ##### --> <para> </para> @@ -803,7 +817,7 @@ Drawing contexts. @Returns: -<!-- ##### FUNCTION cairo_current_rgb_color ##### --> +<!-- ##### FUNCTION cairo_get_rgb_color ##### --> <para> </para> @@ -814,7 +828,7 @@ Drawing contexts. @blue: -<!-- ##### FUNCTION cairo_current_pattern ##### --> +<!-- ##### FUNCTION cairo_get_pattern ##### --> <para> </para> @@ -823,7 +837,7 @@ Drawing contexts. @Returns: -<!-- ##### FUNCTION cairo_current_alpha ##### --> +<!-- ##### FUNCTION cairo_get_alpha ##### --> <para> </para> @@ -832,7 +846,7 @@ Drawing contexts. @Returns: -<!-- ##### FUNCTION cairo_current_tolerance ##### --> +<!-- ##### FUNCTION cairo_get_tolerance ##### --> <para> </para> @@ -841,7 +855,7 @@ Drawing contexts. @Returns: -<!-- ##### FUNCTION cairo_current_point ##### --> +<!-- ##### FUNCTION cairo_get_current_point ##### --> <para> </para> @@ -851,7 +865,7 @@ Drawing contexts. @y: -<!-- ##### FUNCTION cairo_current_fill_rule ##### --> +<!-- ##### FUNCTION cairo_get_fill_rule ##### --> <para> </para> @@ -860,7 +874,7 @@ Drawing contexts. @Returns: -<!-- ##### FUNCTION cairo_current_line_width ##### --> +<!-- ##### FUNCTION cairo_get_line_width ##### --> <para> </para> @@ -869,7 +883,7 @@ Drawing contexts. @Returns: -<!-- ##### FUNCTION cairo_current_line_cap ##### --> +<!-- ##### FUNCTION cairo_get_line_cap ##### --> <para> </para> @@ -878,7 +892,7 @@ Drawing contexts. @Returns: -<!-- ##### FUNCTION cairo_current_line_join ##### --> +<!-- ##### FUNCTION cairo_get_line_join ##### --> <para> </para> @@ -887,7 +901,7 @@ Drawing contexts. @Returns: -<!-- ##### FUNCTION cairo_current_miter_limit ##### --> +<!-- ##### FUNCTION cairo_get_miter_limit ##### --> <para> </para> @@ -896,7 +910,7 @@ Drawing contexts. @Returns: -<!-- ##### FUNCTION cairo_current_matrix ##### --> +<!-- ##### FUNCTION cairo_get_matrix ##### --> <para> </para> @@ -905,7 +919,7 @@ Drawing contexts. @matrix: -<!-- ##### FUNCTION cairo_current_target_surface ##### --> +<!-- ##### FUNCTION cairo_get_target_surface ##### --> <para> </para> @@ -914,7 +928,7 @@ Drawing contexts. @Returns: -<!-- ##### FUNCTION cairo_current_path ##### --> +<!-- ##### FUNCTION cairo_get_path ##### --> <para> </para> @@ -927,7 +941,7 @@ Drawing contexts. @closure: -<!-- ##### FUNCTION cairo_current_path_flat ##### --> +<!-- ##### FUNCTION cairo_get_path_flat ##### --> <para> </para> diff --git a/src/cairo-surface.c b/src/cairo-surface.c index 330d58b1e..06a1c2f82 100644 --- a/src/cairo-surface.c +++ b/src/cairo-surface.c @@ -38,6 +38,12 @@ #include "cairoint.h" +typedef struct { + const cairo_user_data_key_t *key; + void *user_data; + cairo_destroy_func_t destroy; +} cairo_user_data_slot_t; + void _cairo_surface_init (cairo_surface_t *surface, const cairo_surface_backend_t *backend) @@ -45,6 +51,8 @@ _cairo_surface_init (cairo_surface_t *surface, surface->backend = backend; surface->ref_count = 1; + _cairo_array_init (&surface->user_data_slots, + sizeof (cairo_user_data_slot_t)); _cairo_matrix_init (&surface->matrix); surface->filter = CAIRO_FILTER_NEAREST; @@ -145,6 +153,88 @@ cairo_surface_destroy (cairo_surface_t *surface) } slim_hidden_def(cairo_surface_destroy); +/** + * cairo_surface_get_user_data: + * @surface: a #cairo_surface_t + * @key: the address of the #cairo_user_data_key_t the user data was + * attached to + * + * Return user data previously attached to @surface using the specified + * key. If no user data has been attached with the given key this + * function returns %NULL. + * + * Return value: the user data previously attached or %NULL. + **/ +void * +cairo_surface_get_user_data (cairo_surface_t *surface, + const cairo_user_data_key_t *key) +{ + int i, num_slots; + cairo_user_data_slot_t *slots; + + num_slots = surface->user_data_slots.num_elements; + slots = (cairo_user_data_slot_t *) surface->user_data_slots.elements; + for (i = 0; i < num_slots; i++) { + if (slots[i].key == key) + return slots[i].user_data; + } + + return NULL; +} + +/** + * cairo_surface_set_user_data: + * @surface: a #cairo_surface_t + * @key: the address of a #cairo_user_data_key_t to attach the user data to + * @user_data: the user data to attach to the surface + * @destroy: a #cairo_destroy_func_t which will be called when the + * surface is destroyed or when new user data is attached using the + * same key. + * + * Attach user data to @surface. To remove user data from a surface, + * call this function with the key that was used to set it and %NULL + * for @data. + * + * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a + * slot could not be allocated for the user data. + **/ +cairo_status_t +cairo_surface_set_user_data (cairo_surface_t *surface, + const cairo_user_data_key_t *key, + void *user_data, + cairo_destroy_func_t destroy) +{ + int i, num_slots; + cairo_user_data_slot_t *slots, *s; + + s = NULL; + num_slots = surface->user_data_slots.num_elements; + slots = (cairo_user_data_slot_t *) surface->user_data_slots.elements; + for (i = 0; i < num_slots; i++) { + if (slots[i].key == key) { + if (slots[i].user_data != NULL) + slots[i].destroy (slots[i].user_data); + s = &slots[i]; + break; + } + if (slots[i].user_data == NULL) { + s = &slots[i]; + break; + } + } + + if (s == NULL) + s = _cairo_array_append (&surface->user_data_slots, NULL, 1); + if (s == NULL) + return CAIRO_STATUS_NO_MEMORY; + + s->key = key; + s->user_data = user_data; + s->destroy = destroy; + + return CAIRO_STATUS_SUCCESS; +} + double _cairo_surface_pixels_per_inch (cairo_surface_t *surface) { diff --git a/src/cairo.h b/src/cairo.h index 2f1065320..d2743b65d 100644 --- a/src/cairo.h +++ b/src/cairo.h @@ -705,6 +705,15 @@ cairo_status (cairo_t *cr); const char * cairo_status_string (cairo_t *cr); +/** + * cairo_destroy_func_t + * + * #cairo_destroy_func_t the type of function which is called when a + * data element is destroyed. It is passed the pointer to the data + * element and should free any memory and resources allocated for it. + */ +typedef void (*cairo_destroy_func_t) (void *data); + /* Surface manipulation */ /* XXX: We may want to rename this function in light of the new virtualized surface backends... */ @@ -759,6 +768,30 @@ cairo_surface_set_filter (cairo_surface_t *surface, cairo_filter_t filter); cairo_filter_t cairo_surface_get_filter (cairo_surface_t *surface); + +/** + * cairo_user_data_key_t + * + * #cairo_user_data_key_t is used for attaching user data to cairo + * data structures. The actual contents of the struct is never used, + * and there is no need to initialize the object; only the unique + * address of a #cairo_data_key_t object is used. Typically, you + * would just use the address of a static #cairo_data_key_t object. + */ +typedef struct _cairo_user_data_key { + int unused; +} cairo_user_data_key_t; + +void * +cairo_surface_get_user_data (cairo_surface_t *surface, + const cairo_user_data_key_t *key); + +cairo_status_t +cairo_surface_set_user_data (cairo_surface_t *surface, + const cairo_user_data_key_t *key, + void *data, + cairo_destroy_func_t destroy); + /* Image-surface functions */ cairo_surface_t * diff --git a/src/cairo_surface.c b/src/cairo_surface.c index 330d58b1e..06a1c2f82 100644 --- a/src/cairo_surface.c +++ b/src/cairo_surface.c @@ -38,6 +38,12 @@ #include "cairoint.h" +typedef struct { + const cairo_user_data_key_t *key; + void *user_data; + cairo_destroy_func_t destroy; +} cairo_user_data_slot_t; + void _cairo_surface_init (cairo_surface_t *surface, const cairo_surface_backend_t *backend) @@ -45,6 +51,8 @@ _cairo_surface_init (cairo_surface_t *surface, surface->backend = backend; surface->ref_count = 1; + _cairo_array_init (&surface->user_data_slots, + sizeof (cairo_user_data_slot_t)); _cairo_matrix_init (&surface->matrix); surface->filter = CAIRO_FILTER_NEAREST; @@ -145,6 +153,88 @@ cairo_surface_destroy (cairo_surface_t *surface) } slim_hidden_def(cairo_surface_destroy); +/** + * cairo_surface_get_user_data: + * @surface: a #cairo_surface_t + * @key: the address of the #cairo_user_data_key_t the user data was + * attached to + * + * Return user data previously attached to @surface using the specified + * key. If no user data has been attached with the given key this + * function returns %NULL. + * + * Return value: the user data previously attached or %NULL. + **/ +void * +cairo_surface_get_user_data (cairo_surface_t *surface, + const cairo_user_data_key_t *key) +{ + int i, num_slots; + cairo_user_data_slot_t *slots; + + num_slots = surface->user_data_slots.num_elements; + slots = (cairo_user_data_slot_t *) surface->user_data_slots.elements; + for (i = 0; i < num_slots; i++) { + if (slots[i].key == key) + return slots[i].user_data; + } + + return NULL; +} + +/** + * cairo_surface_set_user_data: + * @surface: a #cairo_surface_t + * @key: the address of a #cairo_user_data_key_t to attach the user data to + * @user_data: the user data to attach to the surface + * @destroy: a #cairo_destroy_func_t which will be called when the + * surface is destroyed or when new user data is attached using the + * same key. + * + * Attach user data to @surface. To remove user data from a surface, + * call this function with the key that was used to set it and %NULL + * for @data. + * + * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a + * slot could not be allocated for the user data. + **/ +cairo_status_t +cairo_surface_set_user_data (cairo_surface_t *surface, + const cairo_user_data_key_t *key, + void *user_data, + cairo_destroy_func_t destroy) +{ + int i, num_slots; + cairo_user_data_slot_t *slots, *s; + + s = NULL; + num_slots = surface->user_data_slots.num_elements; + slots = (cairo_user_data_slot_t *) surface->user_data_slots.elements; + for (i = 0; i < num_slots; i++) { + if (slots[i].key == key) { + if (slots[i].user_data != NULL) + slots[i].destroy (slots[i].user_data); + s = &slots[i]; + break; + } + if (slots[i].user_data == NULL) { + s = &slots[i]; + break; + } + } + + if (s == NULL) + s = _cairo_array_append (&surface->user_data_slots, NULL, 1); + if (s == NULL) + return CAIRO_STATUS_NO_MEMORY; + + s->key = key; + s->user_data = user_data; + s->destroy = destroy; + + return CAIRO_STATUS_SUCCESS; +} + double _cairo_surface_pixels_per_inch (cairo_surface_t *surface) { diff --git a/src/cairoint.h b/src/cairoint.h index 55af0422d..6afaece04 100644 --- a/src/cairoint.h +++ b/src/cairoint.h @@ -666,6 +666,7 @@ struct _cairo_surface { const cairo_surface_backend_t *backend; unsigned int ref_count; + cairo_array_t user_data_slots; cairo_matrix_t matrix; cairo_filter_t filter; diff --git a/test/.cvsignore b/test/.cvsignore index 6194e2678..5aa262f12 100644 --- a/test/.cvsignore +++ b/test/.cvsignore @@ -2,6 +2,7 @@ .libs Makefile Makefile.in +clip_twice coverage fill_rule imagediff @@ -11,7 +12,7 @@ move_to_show_surface pixman_rotate text_cache_crash text_rotate -clip_twice +user_data *-out.png *-diff.png diff --git a/test/Makefile.am b/test/Makefile.am index c3c21c02c..e2a2d3489 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -10,8 +10,8 @@ linear_gradient \ move_to_show_surface \ pixman_rotate \ text_cache_crash \ -text_rotate - +text_rotate \ +user_data # And all new tests go here too. I really don't like having to repeat # this list. Anyone know a good way to avoid it? Can I use a wildcard @@ -79,6 +79,7 @@ move_to_show_surface_SOURCES = move_to_show_surface.c $(cairo_test_lib) pixman_rotate_SOURCES = pixman_rotate.c $(cairo_test_lib) text_cache_crash_SOURCES = text_cache_crash.c $(cairo_test_lib) text_rotate_SOURCES = text_rotate.c $(cairo_test_lib) +user_data_SOURCES = user_data.c noinst_PROGRAMS = imagediff imagediff_SOURCES = imagediff.c $(cairo_test_lib) diff --git a/test/user-data.c b/test/user-data.c new file mode 100644 index 000000000..45dc868f3 --- /dev/null +++ b/test/user-data.c @@ -0,0 +1,70 @@ +/* + * Copyright © 2005 Red Hat, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software + * and its documentation for any purpose is hereby granted without + * fee, provided that the above copyright notice appear in all copies + * and that both that copyright notice and this permission notice + * appear in supporting documentation, and that the name of + * Red Hat, Inc. not be used in advertising or publicity pertaining to + * distribution of the software without specific, written prior + * permission. Red Hat, Inc. makes no representations about the + * suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL, + * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR + * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Author: Kristian Høgsberg <krh@redhat.com> + */ + +#include <cairo.h> +#include <assert.h> +#include <stdlib.h> + +static void +destroy_data1 (void *p) +{ + *(int *) p = 1; +} + +static void +destroy_data2 (void *p) +{ + *(int *) p = 2; +} + +int +main (void) +{ + cairo_surface_t *surface; + static const cairo_user_data_key_t key1, key2; + int data1, data2; + + data1 = 0; + data2 = 0; + surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1); + assert (cairo_surface_set_user_data (surface, &key1, &data1, destroy_data1) + == CAIRO_STATUS_SUCCESS); + assert (cairo_surface_set_user_data (surface, &key2, &data2, destroy_data2) + == CAIRO_STATUS_SUCCESS); + assert (cairo_surface_get_user_data (surface, &key1) == &data1); + assert (cairo_surface_set_user_data (surface, &key1, NULL, NULL) + == CAIRO_STATUS_SUCCESS); + assert (data1 == 1); + assert (data2 == 0); + + data1 = 0; + assert (cairo_surface_set_user_data (surface, &key1, &data1, destroy_data1) + == CAIRO_STATUS_SUCCESS); + cairo_surface_destroy (surface); + assert (data1 = 1); + assert (data2 = 2); + + return 0; +} diff --git a/test/user_data.c b/test/user_data.c new file mode 100644 index 000000000..45dc868f3 --- /dev/null +++ b/test/user_data.c @@ -0,0 +1,70 @@ +/* + * Copyright © 2005 Red Hat, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software + * and its documentation for any purpose is hereby granted without + * fee, provided that the above copyright notice appear in all copies + * and that both that copyright notice and this permission notice + * appear in supporting documentation, and that the name of + * Red Hat, Inc. not be used in advertising or publicity pertaining to + * distribution of the software without specific, written prior + * permission. Red Hat, Inc. makes no representations about the + * suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL, + * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR + * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Author: Kristian Høgsberg <krh@redhat.com> + */ + +#include <cairo.h> +#include <assert.h> +#include <stdlib.h> + +static void +destroy_data1 (void *p) +{ + *(int *) p = 1; +} + +static void +destroy_data2 (void *p) +{ + *(int *) p = 2; +} + +int +main (void) +{ + cairo_surface_t *surface; + static const cairo_user_data_key_t key1, key2; + int data1, data2; + + data1 = 0; + data2 = 0; + surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1); + assert (cairo_surface_set_user_data (surface, &key1, &data1, destroy_data1) + == CAIRO_STATUS_SUCCESS); + assert (cairo_surface_set_user_data (surface, &key2, &data2, destroy_data2) + == CAIRO_STATUS_SUCCESS); + assert (cairo_surface_get_user_data (surface, &key1) == &data1); + assert (cairo_surface_set_user_data (surface, &key1, NULL, NULL) + == CAIRO_STATUS_SUCCESS); + assert (data1 == 1); + assert (data2 == 0); + + data1 = 0; + assert (cairo_surface_set_user_data (surface, &key1, &data1, destroy_data1) + == CAIRO_STATUS_SUCCESS); + cairo_surface_destroy (surface); + assert (data1 = 1); + assert (data2 = 2); + + return 0; +} |