]> git.donarmstrong.com Git - lilypond.git/blob - lily/scm-hash.cc
Issue 4360: Reorganize smob initialization to make it more reliable
[lilypond.git] / lily / scm-hash.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "scm-hash.hh"
21
22 #include <cstdio>
23 #include <algorithm>
24 using namespace std;
25
26 ADD_SMOB_INIT (Scheme_hash_table);
27
28
29 /*
30   Return: number of objects.
31 */
32 SCM
33 copy_handle (void *closure, SCM handle)
34 {
35   SCM tab = (SCM) closure;
36   scm_hashq_set_x (tab, scm_car (handle), scm_cdr (handle));
37   return tab;
38 }
39
40 static void
41 copy_scm_hashes (SCM dest, SCM src)
42 {
43   scm_internal_hash_for_each_handle ((scm_t_hash_handle_fn) &copy_handle,
44                                      dest, src);
45 }
46
47 Scheme_hash_table::Scheme_hash_table ()
48 {
49   hash_tab_ = SCM_EOL;
50   smobify_self ();
51   hash_tab_ = scm_c_make_hash_table (119);
52 }
53
54 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
55   : Smob<Scheme_hash_table> ()
56 {
57   hash_tab_ = SCM_EOL;
58   smobify_self ();
59   copy (src);
60 }
61
62 void
63 Scheme_hash_table::copy (Scheme_hash_table const &src)
64 {
65   if (&src == this)
66     return;
67
68   hash_tab_ = scm_c_make_hash_table (SCM_HASHTABLE_N_ITEMS (src.hash_tab_));
69   copy_scm_hashes (hash_tab_, src.hash_tab_);
70 }
71
72 Scheme_hash_table::~Scheme_hash_table ()
73 {
74 }
75
76 SCM
77 Scheme_hash_table::mark_smob ()
78 {
79   scm_gc_mark (hash_tab_);
80   return SCM_EOL;
81 }
82
83 int
84 Scheme_hash_table::print_smob (SCM p, scm_print_state *)
85 {
86   scm_puts ("#<Scheme_hash_table  ", p);
87   scm_display (hash_tab_, p);
88   scm_puts ("> ", p);
89   return 1;
90 }
91
92 bool
93 Scheme_hash_table::try_retrieve (SCM k, SCM *v)
94 {
95
96   SCM handle = scm_hashq_get_handle (hash_tab_, k);
97   if (scm_is_pair (handle))
98     {
99       *v = scm_cdr (handle);
100       return true;
101     }
102   else
103     return false;
104 }
105
106 bool
107 Scheme_hash_table::contains (SCM k) const
108 {
109   return scm_is_pair (scm_hashq_get_handle (hash_tab_, k));
110 }
111
112 void
113 Scheme_hash_table::set (SCM k, SCM v)
114 {
115   assert (scm_is_symbol (k));
116   SCM handle = scm_hashq_create_handle_x (hash_tab_, k, SCM_UNDEFINED);
117   scm_set_cdr_x (handle, v);
118 }
119
120 SCM
121 Scheme_hash_table::get (SCM k) const
122 {
123   /* SCM_UNSPECIFIED will stick out like a sore thumb, hopefully.
124   */
125   return scm_hashq_ref (hash_tab_, k, SCM_UNSPECIFIED);
126 }
127
128 void
129 Scheme_hash_table::remove (SCM k)
130 {
131   scm_hashq_remove_x (hash_tab_, k);
132 }
133
134 static SCM
135 collect_handles (void * /* closure */,
136                  SCM key,
137                  SCM value,
138                  SCM result)
139 {
140   return scm_acons (key, value, result);
141 }
142
143 SCM
144 Scheme_hash_table::to_alist () const
145 {
146   return scm_internal_hash_fold ((scm_t_hash_fold_fn) &collect_handles,
147                                  NULL, SCM_EOL, hash_tab_);
148 }