2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1999--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
23 #include "lily-guile.hh"
27 Smobs are GUILEs mechanism of exporting C(++) objects to the Scheme
28 world. They are documented in the GUILE manual.
31 In LilyPond, smobs are created from C++ objects through macros.
32 There are two types of smob objects.
34 1. Simple smobs are intended for simple objects like numbers:
35 immutable objects that can be copied without change of meaning.
37 To obtain an SCM version of a simple smob, use the member function
40 Simple smobs are created by adding the
41 DECLARE_SIMPLE_SMOBS(Classname) to the declaration
43 2. Complex smobs are objects that have an identity. These objects
44 carry this identity in the form of a self_scm () method, which is a
45 SCM pointer to the object itself.
47 The constructor for a complex smob should have 3 steps:
49 * initialize all SCM members to a non-immediate value (like SCM_EOL)
51 * call smobify_self ()
53 * initialize SCM members
57 Complex_smob::Complex_smob () {
58 scm_member_ = SCM_EOL;
60 scm_member_ = <..what you want to store..>
63 after construction, the self_scm () field of a complex smob is
64 protected from Garbage Collection. This protection should be
65 removed once the object is put into another (reachable) Scheme data
68 Complex_smob *p = new Complex_smob;
69 list = scm_cons (p->self_scm (), list);
70 scm_gc_unprotect_object (p->self_scm ());
72 Complex smobs are made with DECLARE_SMOBS (Classname) in the class
77 Common public methods to C++ smob objects:
79 unsmob (SCM x) - unpacks X and returns pointer to the C++ object, or 0
80 if it has the wrong type.
82 SCM equal_p (SCM a, SCM b) - compare A and B. Returns a Scheme boolean
87 For implementating a class, the following should be provided
89 - an equal_p () function (a default is in the
90 IMPLEMENT_DEFAULT_EQUAL_P macro in ly-smobs.icc)
92 - mark_smob () function, that calls scm_gc_mark () on all Scheme
95 - a print_smob () function, that displays a representation for
98 - A call to one of the IMPLEMENT_SMOBS or IMPLEMENT_SIMPLE_SMOBS macros
99 from file "ly-smobs.icc"
102 #define DECLARE_SIMPLE_SMOBS(CL) \
104 SCM smobbed_copy () const; \
105 DECLARE_BASE_SMOBS (CL)
107 #define DECLARE_BASE_SMOBS(CL) \
108 friend class Non_existent_class; \
110 static const char* smob_name_; \
111 static scm_t_bits smob_tag_; \
112 static SCM mark_smob (SCM); \
113 static size_t free_smob (SCM s); \
114 static int print_smob (SCM s, SCM p, scm_print_state*); \
116 static SCM equal_p (SCM a, SCM b); \
117 static CL *unsmob (SCM s) __attribute__((pure)) \
119 if (SCM_NIMP (s) && SCM_CELL_TYPE (s) == smob_tag_) \
120 return (CL *) SCM_CELL_WORD_1 (s); \
124 static SCM smob_p (SCM); \
125 static void init_smobs (); \
128 #define DECLARE_SMOBS(CL) \
129 DECLARE_BASE_SMOBS (CL) \
132 SCM unprotected_smobify_self (); \
134 void smobify_self (); \
136 SCM protection_cons_; \
140 SCM self_scm () const { return self_scm_; } \
143 #define DECLARE_UNSMOB(CL, name) \
145 unsmob_ ## name (SCM s) \
147 return CL::unsmob (s); \
150 #define DECLARE_TYPE_P(CL) extern SCM CL ## _type_p_proc
152 void protect_smob (SCM smob, SCM *prot_cons);
153 void unprotect_smob (SCM smob, SCM *prot_cons);
155 extern bool parsed_objects_should_be_dead;
158 #define ASSERT_LIVE_IS_ALLOWED() \
160 static bool passed_here_once;\
161 if (parsed_objects_should_be_dead && !passed_here_once) { \
162 ::programming_error (string ("Parsed object should be dead: ") + __PRETTY_FUNCTION__ ); \
163 passed_here_once = true;\
168 #define ASSERT_LIVE_IS_ALLOWED() do { } \
172 #endif /* SMOBS_HH */