output-item: Collapse the inheritance hierarchy into a single struct.
[pspp] / src / output / chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2009, 2011 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 <assert.h>
20 #include <stdlib.h>
21
22 #include "libpspp/cast.h"
23 #include "libpspp/compiler.h"
24 #include "libpspp/str.h"
25 #include "output/chart-provider.h"
26 #include "output/output-item.h"
27
28 #include "gl/xalloc.h"
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32
33 struct chart *
34 chart_ref (const struct chart *chart_)
35 {
36   struct chart *chart = CONST_CAST (struct chart *, chart_);
37   assert (chart->ref_cnt > 0);
38   chart->ref_cnt++;
39   return chart;
40 }
41
42 void
43 chart_unref (struct chart *chart)
44 {
45   if (chart)
46     {
47       assert (chart->ref_cnt > 0);
48       if (!--chart->ref_cnt)
49         {
50           char *title = chart->title;
51           chart->class->destroy (chart);
52           free (title);
53         }
54     }
55 }
56
57 bool
58 chart_is_shared (const struct chart *chart)
59 {
60   assert (chart->ref_cnt > 0);
61   return chart->ref_cnt > 1;
62 }
63
64 /* Initializes CHART as a chart of the specified CLASS.  The new chart
65    initially has the specified TITLE, which may be NULL if no title is yet
66    available.  The caller retains ownership of TITLE.
67
68    A chart is abstract, that is, a plain chart is not useful on its own.  Thus,
69    this function is normally called from the initialization function of some
70    subclass of chart. */
71 void
72 chart_init (struct chart *chart, const struct chart_class *class,
73             const char *title)
74 {
75   *chart = (struct chart) {
76     .ref_cnt = 1,
77     .class = class,
78     .title = xstrdup_if_nonnull (title),
79   };
80 }
81
82 /* Returns CHART's title, which is a null pointer if no title has been set. */
83 const char *
84 chart_get_title (const struct chart *chart)
85 {
86   return chart->title;
87 }
88
89 /* Sets CHART's title to TITLE, replacing any previous title.  Specify NULL for
90    TITLE to clear any title from CHART.  The caller retains ownership of
91    TITLE.
92
93    This function may only be used on a chart that is unshared. */
94 void
95 chart_set_title (struct chart *chart, const char *title)
96 {
97   assert (!chart_is_shared (chart));
98   free (chart->title);
99   chart->title = xstrdup_if_nonnull (title);
100 }
101
102 /* Submits CHART to the configured output drivers, and transfers ownership to
103    the output subsystem. */
104 void
105 chart_submit (struct chart *chart)
106 {
107   if (chart)
108     output_item_submit (chart_item_create (chart));
109 }