From: Ben Pfaff Date: Sun, 20 Mar 2011 18:35:11 +0000 (-0700) Subject: dataset: Use similar form to dictionary code for callbacks, and document. X-Git-Tag: v0.7.7~10 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a9b46fb9e208c694e39d6f173bfa6fe631a30129;p=pspp-builds.git dataset: Use similar form to dictionary code for callbacks, and document. --- diff --git a/src/data/dataset.c b/src/data/dataset.c index 1729c2d0..482e6d93 100644 --- a/src/data/dataset.c +++ b/src/data/dataset.c @@ -60,11 +60,6 @@ struct dataset { struct trns_chain *temporary_trns_chain; struct dictionary *dict; - /* Callback which occurs whenever the transformation chain(s) have - been modified */ - transformation_change_callback_func *xform_callback; - void *xform_callback_aux; - /* If true, cases are discarded instead of being written to sink. */ bool discard_output; @@ -98,29 +93,27 @@ struct dataset { bool ok; /* Error status. */ struct casereader_shim *shim; /* Shim on proc_open() casereader. */ - void (*callback) (void *); /* Callback for when the dataset changes */ + const struct dataset_callbacks *callbacks; void *cb_data; /* Default encoding for reading syntax files. */ char *syntax_encoding; }; +static void dataset_changed__ (struct dataset *); +static void dataset_transformations_changed__ (struct dataset *, + bool non_empty); + static void add_case_limit_trns (struct dataset *ds); static void add_filter_trns (struct dataset *ds); static void update_last_proc_invocation (struct dataset *ds); -static void -dataset_set_unsaved (const struct dataset *ds) -{ - if (ds->callback) ds->callback (ds->cb_data); -} - static void dict_callback (struct dictionary *d UNUSED, void *ds_) { struct dataset *ds = ds_; - dataset_set_unsaved (ds); + dataset_changed__ (ds); } /* Creates and returns a new dataset. The dataset initially has an empty @@ -151,9 +144,7 @@ dataset_destroy (struct dataset *ds) dict_destroy (ds->dict); caseinit_destroy (ds->caseinit); trns_chain_destroy (ds->permanent_trns_chain); - - if ( ds->xform_callback) - ds->xform_callback (false, ds->xform_callback_aux); + dataset_transformations_changed__ (ds, false); free (ds->syntax_encoding); free (ds); } @@ -238,11 +229,13 @@ dataset_steal_source (struct dataset *ds) return reader; } - + void -dataset_set_callback (struct dataset *ds, void (*cb) (void *), void *cb_data) +dataset_set_callbacks (struct dataset *ds, + const struct dataset_callbacks *callbacks, + void *cb_data) { - ds->callback = cb; + ds->callbacks = callbacks; ds->cb_data = cb_data; } @@ -258,7 +251,7 @@ dataset_get_default_syntax_encoding (const struct dataset *ds) { return ds->syntax_encoding; } - + /* Returns the last time the data was read. */ time_t time_of_last_procedure (struct dataset *ds) @@ -490,7 +483,7 @@ proc_commit (struct dataset *ds) assert (ds->proc_state == PROC_CLOSED); ds->proc_state = PROC_COMMITTED; - dataset_set_unsaved (ds); + dataset_changed__ (ds); /* Free memory for lagged cases. */ while (!deque_is_empty (&ds->lag)) @@ -572,9 +565,7 @@ proc_capture_transformations (struct dataset *ds) assert (ds->temporary_trns_chain == NULL); chain = ds->permanent_trns_chain; ds->cur_trns_chain = ds->permanent_trns_chain = trns_chain_create (); - - if ( ds->xform_callback) - ds->xform_callback (false, ds->xform_callback_aux); + dataset_transformations_changed__ (ds, false); return chain; } @@ -586,8 +577,7 @@ void add_transformation (struct dataset *ds, trns_proc_func *proc, trns_free_func *free, void *aux) { trns_chain_append (ds->cur_trns_chain, NULL, proc, free, aux); - if ( ds->xform_callback) - ds->xform_callback (true, ds->xform_callback_aux); + dataset_transformations_changed__ (ds, true); } /* Adds a transformation that processes a case with PROC and @@ -602,9 +592,7 @@ add_transformation_with_finalizer (struct dataset *ds, trns_free_func *free, void *aux) { trns_chain_append (ds->cur_trns_chain, finalize, proc, free, aux); - - if ( ds->xform_callback) - ds->xform_callback (true, ds->xform_callback_aux); + dataset_transformations_changed__ (ds, true); } /* Returns the index of the next transformation. @@ -639,9 +627,7 @@ proc_start_temporary_transformations (struct dataset *ds) trns_chain_finalize (ds->permanent_trns_chain); ds->temporary_trns_chain = ds->cur_trns_chain = trns_chain_create (); - - if ( ds->xform_callback) - ds->xform_callback (true, ds->xform_callback_aux); + dataset_transformations_changed__ (ds, true); } } @@ -681,11 +667,8 @@ proc_cancel_temporary_transformations (struct dataset *ds) trns_chain_destroy (ds->temporary_trns_chain); ds->temporary_trns_chain = NULL; - - if ( ds->xform_callback) - ds->xform_callback (!trns_chain_is_empty (ds->permanent_trns_chain), - ds->xform_callback_aux); - + dataset_transformations_changed__ ( + ds, !trns_chain_is_empty (ds->permanent_trns_chain)); return true; } else @@ -703,22 +686,11 @@ proc_cancel_all_transformations (struct dataset *ds) ok = trns_chain_destroy (ds->temporary_trns_chain) && ok; ds->permanent_trns_chain = ds->cur_trns_chain = trns_chain_create (); ds->temporary_trns_chain = NULL; - if ( ds->xform_callback) - ds->xform_callback (false, ds->xform_callback_aux); + dataset_transformations_changed__ (ds, false); return ok; } - -void -dataset_add_transform_change_callback (struct dataset *ds, - transformation_change_callback_func *cb, - void *aux) -{ - ds->xform_callback = cb; - ds->xform_callback_aux = aux; -} - /* Causes output from the next procedure to be discarded, instead of being preserved for use as input for the next procedure. */ void @@ -828,3 +800,17 @@ dataset_need_lag (struct dataset *ds, int n_before) { ds->n_lag = MAX (ds->n_lag, n_before); } + +static void +dataset_changed__ (struct dataset *ds) +{ + if (ds->callbacks != NULL && ds->callbacks->changed != NULL) + ds->callbacks->changed (ds->cb_data); +} + +static void +dataset_transformations_changed__ (struct dataset *ds, bool non_empty) +{ + if (ds->callbacks != NULL && ds->callbacks->transformations_changed != NULL) + ds->callbacks->transformations_changed (non_empty, ds->cb_data); +} diff --git a/src/data/dataset.h b/src/data/dataset.h index 7aa8b581..b2aa8bcf 100644 --- a/src/data/dataset.h +++ b/src/data/dataset.h @@ -40,10 +40,24 @@ bool dataset_has_source (const struct dataset *ds); bool dataset_set_source (struct dataset *, struct casereader *); struct casereader *dataset_steal_source (struct dataset *); -void dataset_set_callback (struct dataset *, void (*cb) (void *), void *); - void dataset_set_default_syntax_encoding (struct dataset *, const char *); const char *dataset_get_default_syntax_encoding (const struct dataset *); + +struct dataset_callbacks + { + /* Called whenever a procedure completes execution or whenever the + dictionary within the dataset is modified (though not when it is + replaced by a new dictionary). */ + void (*changed) (void *aux); + + /* Called whenever a transformation is added or removed. NON_EMPTY is true + if after the change there is at least one transformation, false if there + are no transformations. */ + void (*transformations_changed) (bool non_empty, void *aux); + }; + +void dataset_set_callbacks (struct dataset *, const struct dataset_callbacks *, + void *aux); /* Transformations. */ @@ -65,12 +79,6 @@ bool proc_cancel_temporary_transformations (struct dataset *ds); /* Procedures. */ -typedef void transformation_change_callback_func (bool non_empty, void *aux); - - -void dataset_add_transform_change_callback (struct dataset *, - transformation_change_callback_func *, void *); - void proc_discard_output (struct dataset *ds); bool proc_execute (struct dataset *ds); diff --git a/src/ui/gui/psppire-data-window.c b/src/ui/gui/psppire-data-window.c index 24798cec..9a3470ae 100644 --- a/src/ui/gui/psppire-data-window.c +++ b/src/ui/gui/psppire-data-window.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2008, 2009, 2010 Free Software Foundation + Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -940,6 +940,12 @@ connect_action (PsppireDataWindow *dw, const char *action_name, static void psppire_data_window_init (PsppireDataWindow *de) { + static const struct dataset_callbacks cbs = + { + set_unsaved, /* changed */ + transformation_change_callback, /* transformations_changed */ + }; + PsppireVarStore *vs; PsppireDict *dict = NULL; @@ -966,7 +972,7 @@ psppire_data_window_init (PsppireDataWindow *de) g_signal_connect_swapped (the_data_store, "cases-deleted", G_CALLBACK (set_unsaved), de); - dataset_set_callback (the_dataset, set_unsaved, de); + dataset_set_callbacks (the_dataset, &cbs, de); connect_help (de->builder); @@ -988,11 +994,6 @@ psppire_data_window_init (PsppireDataWindow *de) g_signal_connect_swapped (de->data_editor, "data-available-changed", G_CALLBACK (set_paste_menuitem_sensitivity), de); - dataset_add_transform_change_callback (the_dataset, - transformation_change_callback, - de); - - vs = the_var_store; g_assert(vs); /* Traps a possible bug in w32 build */