]> git.donarmstrong.com Git - lilypond.git/blob - lily/protected-scm.cc
Issue 4357/3: Run preparatory script replacing listener macros
[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 Protected_scm &
50 Protected_scm::operator = (SCM s)
51 {
52   if (SCM_CONSP (object_))
53     SCM_SETCAR (object_, s);
54   else
55     object_ = SCM_NIMP (s) ? scm_permanent_object (scm_list_1 (s)) : s;
56
57   return *this;
58 }
59
60 Protected_scm &
61 Protected_scm::operator = (Protected_scm const &s)
62 {
63   return *this = (SCM) s;
64 }
65
66 Protected_scm::operator SCM () const
67 {
68   return SCM_CONSP (object_) ? SCM_CAR (object_) : object_;
69 }