]> git.donarmstrong.com Git - lilypond.git/blob - lily/protected-scm.cc
Doc: Usage - tidy up of external.itely file
[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 // For static objects, this will be called at program exit.  With the
41 // state of the memory system unknown, we refrain from any cleanup
42 // actions outside of the object memory itself.
43
44 Protected_scm::~Protected_scm ()
45 {
46   object_ = SCM_UNDEFINED;
47 }
48
49 SCM Protected_scm::list_ = SCM_EOL;
50 SCM Protected_scm::last_ = SCM_EOL;
51
52 Protected_scm &
53 Protected_scm::operator = (SCM s)
54 {
55   if (SCM_CONSP (object_))
56     SCM_SETCAR (object_, s);
57   else if (SCM_IMP (s))
58     object_ = s;
59   else
60     {
61       s = scm_list_1 (s);
62       if (SCM_CONSP (last_))
63         SCM_SETCDR (last_, s);
64       else
65         list_ = scm_permanent_object (s);
66       last_ = object_ = s;
67     }
68
69   return *this;
70 }
71
72 Protected_scm &
73 Protected_scm::operator = (Protected_scm const &s)
74 {
75   return *this = (SCM) s;
76 }
77
78 Protected_scm::operator SCM () const
79 {
80   return SCM_CONSP (object_) ? SCM_CAR (object_) : object_;
81 }