]> git.donarmstrong.com Git - lilypond.git/blob - lily/protected-scm.cc
Web-ja: update introduction
[lilypond.git] / lily / protected-scm.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--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 "protected-scm.hh"
21
22 // We store the data in the car of a cons cell: it is faster to keep
23 // only one object protected during the life time of Protected_scm
24 // than several.
25
26 Protected_scm::Protected_scm ()
27   : object_ (SCM_UNDEFINED)
28 {
29 }
30
31 Protected_scm::Protected_scm (SCM s)
32   : object_ (s)
33 {
34   // Only allow immediate objects at construction time.  Protected_scm
35   // is intended for variables of static duration, and creating
36   // non-immediate objects when GUILE is not yet up is a bad idea.
37   assert (SCM_IMP (s));
38 }
39
40 SCM Protected_scm::list_ = SCM_EOL;
41 SCM Protected_scm::last_ = SCM_EOL;
42
43 void
44 Protected_scm::protectify (SCM s)
45 {
46   s = scm_list_1 (s);
47   if (SCM_CONSP (last_))
48     SCM_SETCDR (last_, s);
49   else
50     list_ = scm_permanent_object (s);
51   last_ = object_ = s;
52 }
53
54 Protected_scm &
55 Protected_scm::operator = (SCM s)
56 {
57   if (SCM_CONSP (object_))
58     SCM_SETCAR (object_, s);
59   else if (SCM_IMP (s))
60     object_ = s;
61   else
62     protectify (s);
63
64   return *this;
65 }
66
67 Protected_scm &
68 Protected_scm::operator = (Protected_scm const &s)
69 {
70   return *this = (SCM) s;
71 }
72
73 Protected_scm::operator SCM const & () const
74 {
75   if (SCM_CONSP (object_))
76     return *SCM_CARLOC (object_);
77   return object_;
78 }
79
80 Protected_scm::operator SCM & ()
81 {
82   // The reference may be used to overwrite an immediate value with a
83   // non-immediate one, so we _have_ to create full protection.
84   if (!SCM_CONSP (object_))
85     protectify (object_);
86
87   return *SCM_CARLOC (object_);
88 }
89
90 bool
91 Protected_scm::is_bound () const
92 {
93   if (SCM_CONSP (object_))
94     return !SCM_UNBNDP (SCM_CAR (object_));
95   return !SCM_UNBNDP (object_);
96 }