e1c889ffe33b09967f3bcf798198ed825363fe05
[pspp-builds.git] / src / output / output-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <output/output-item-provider.h>
20
21 #include <assert.h>
22 #include <stdlib.h>
23
24 #include <libpspp/assertion.h>
25 #include <libpspp/cast.h>
26
27 #include "xalloc.h"
28 \f
29 /* Increases ITEM's reference count, indicating that it has an additional
30    owner.  An output item that is shared among multiple owners must not be
31    modified. */
32 struct output_item *
33 output_item_ref (const struct output_item *item_)
34 {
35   struct output_item *item = CONST_CAST (struct output_item *, item_);
36   item->ref_cnt++;
37   return item;
38 }
39
40 /* Decreases ITEM's reference count, indicating that it has one fewer owner.
41    If ITEM no longer has any owners, it is freed. */
42 void
43 output_item_unref (struct output_item *item)
44 {
45   if (item != NULL)
46     {
47       assert (item->ref_cnt > 0);
48       if (--item->ref_cnt == 0)
49         item->class->destroy (item);
50     }
51 }
52
53 /* Returns true if ITEM has more than one owner.  An output item that is shared
54    among multiple owners must not be modified. */
55 bool
56 output_item_is_shared (const struct output_item *item)
57 {
58   return item->ref_cnt > 1;
59 }
60 \f
61 /* Initializes ITEM as an output item of the specified CLASS, initially with a
62    reference count of 1.
63
64    An output item is an abstract class, that is, a plain output_item is not
65    useful on its own.  Thus, this function is normally called from the
66    initialization function of some subclass of output_item. */
67 void
68 output_item_init (struct output_item *item,
69                   const struct output_item_class *class)
70 {
71   item->class = class;
72   item->ref_cnt = 1;
73 }