From dbb4524af169a9c15e5c7fcfddfa1a4b85dd288d Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Tue, 24 Jan 2006 13:15:30 +0000 Subject: [PATCH] * lily/music.cc (derived_mark): derive Music from Prob. * lily/paper-system.cc (LY_DEFINE): derive from Prob. * lily/prob.cc (Module): Implement Prob (Property Object), object with shared and r/w property alists. * lily/include/prob.hh (Module): new file. Declare Prob. * flower/string.cc: remove is_empty(). --- ChangeLog | 11 +++ flower/string.cc | 9 --- lily/GNUmakefile | 1 + lily/include/music.hh | 22 +++--- lily/include/paper-system.hh | 16 +---- lily/include/prob.hh | 47 +++++++++++++ lily/include/smobs.hh | 1 + lily/music-function.cc | 4 +- lily/music-scheme.cc | 8 +++ lily/music.cc | 120 ++++++++------------------------ lily/paper-system.cc | 65 +++--------------- lily/prob.cc | 129 +++++++++++++++++++++++++++++++++++ 12 files changed, 250 insertions(+), 183 deletions(-) create mode 100644 lily/include/prob.hh create mode 100644 lily/prob.cc diff --git a/ChangeLog b/ChangeLog index 34d15b998d..a33b524019 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2006-01-24 Han-Wen Nienhuys + * lily/music.cc (derived_mark): derive Music from Prob. + + * lily/paper-system.cc (LY_DEFINE): derive from Prob. + + * lily/prob.cc (Module): Implement Prob (Property Object), object + with shared and r/w property alists. + + * lily/include/prob.hh (Module): new file. Declare Prob. + + * flower/string.cc: remove is_empty(). + * flower/include/string.hh (class String): remove String:: qualifier. * buildscripts/analyse-cxx-log.py: new file. Read compile log to diff --git a/flower/string.cc b/flower/string.cc index 1c23618f39..e3e8057f7e 100644 --- a/flower/string.cc +++ b/flower/string.cc @@ -220,15 +220,6 @@ String::get_str0 () return strh_.get_str0 (); } -#ifndef DISALLOW_OLD_STRING -bool -String::is_empty () const -{ - return !length (); -} -#endif - - /** Do a signed comparison, analogous to memcmp; */ diff --git a/lily/GNUmakefile b/lily/GNUmakefile index 8d2e91a70a..2a51dc0474 100644 --- a/lily/GNUmakefile +++ b/lily/GNUmakefile @@ -26,6 +26,7 @@ ifeq ($(LINK_GXX_STATICALLY),yes) MODULE_LDFLAGS += -L$(outdir) -static-libgcc endif +CXXFLAGS += -Woverloaded-virtual # for profiling, link guile statically: diff --git a/lily/include/music.hh b/lily/include/music.hh index 140c95c02a..f278786847 100644 --- a/lily/include/music.hh +++ b/lily/include/music.hh @@ -13,10 +13,11 @@ #include "smobs.hh" #include "moment.hh" #include "pitch.hh" +#include "prob.hh" #define is_mus_type(x) internal_is_music_type (ly_symbol2scm (x)) -class Music +class Music : public Prob { public: Music (SCM init); @@ -26,17 +27,11 @@ public: Input *origin () const; void set_spot (Input); - SCM internal_get_property (SCM) const; - void internal_set_property (SCM, SCM val); - SCM internal_get_object (SCM) const; - void internal_set_object (SCM, SCM val); - SCM get_property_alist (bool mutble) const; bool internal_is_music_type (SCM) const; DECLARE_SCHEME_CALLBACK (relative_callback, (SCM, SCM)); Pitch to_relative_octave (Pitch); Pitch generic_to_relative_octave (Pitch); - String name () const; Moment get_length () const; Moment start_mom () const; void print () const; @@ -48,21 +43,20 @@ public: void compress (Moment factor); DECLARE_SCHEME_CALLBACK (duration_length_callback, (SCM)); + protected: - DECLARE_SMOBS (Music,); - - SCM immutable_property_alist_; - SCM mutable_property_alist_; + virtual SCM copy_mutable_properties () const; + virtual void type_check_assignment (SCM, SCM) const; + virtual void derived_mark () const; protected: SCM length_callback_; SCM start_callback_; friend SCM ly_extended_make_music (SCM, SCM); }; -DECLARE_TYPE_P (Music); -DECLARE_UNSMOB (Music, music); - +Music *unsmob_music (SCM); Music *make_music_by_name (SCM sym); SCM ly_music_deep_copy (SCM); +extern SCM ly_music_p_proc; #endif /* MUSIC_HH */ diff --git a/lily/include/paper-system.hh b/lily/include/paper-system.hh index 5df57abf03..a152f789b7 100644 --- a/lily/include/paper-system.hh +++ b/lily/include/paper-system.hh @@ -8,7 +8,7 @@ #ifndef PAPER_SYSTEM_HH #define PAPER_SYSTEM_HH -#include "stencil.hh" +#include "prob.hh" /* A formatted "system" (A block of titling also is a Paper_system) @@ -17,21 +17,11 @@ formatted content of the grob is put into a Paper_system. Page-breaking handles Paper_system objects. */ -class Paper_system +class Paper_system : public Prob { - DECLARE_SMOBS (Paper_system,); - SCM mutable_property_alist_; - SCM immutable_property_alist_; - - void init_vars (); public: - Paper_system (Stencil, SCM); - SCM internal_get_property (SCM sym) const; - void internal_set_property (SCM sym, SCM val); - Real break_before_penalty () const; }; - -DECLARE_UNSMOB (Paper_system, paper_system); +Paper_system*unsmob_paper_system (SCM); #endif /* PAPER_SYSTEM_HH */ diff --git a/lily/include/prob.hh b/lily/include/prob.hh new file mode 100644 index 0000000000..80827a67a3 --- /dev/null +++ b/lily/include/prob.hh @@ -0,0 +1,47 @@ +/* + property-object.hh -- declare Property_object + + source file of the GNU LilyPond music typesetter + + (c) 2004--2006 Jan Nieuwenhuizen +*/ + +#ifndef PROPERTY_OBJECT_HH +#define PROPERTY_OBJECT_HH + +#include "stencil.hh" + +/* + A formatted "system" (A block of titling also is a Property_object) + + To save memory, we don't keep around the System grobs, but put the + formatted content of the grob is put into a + Property_object. Page-breaking handles Property_object objects. +*/ +class Prob +{ + DECLARE_SMOBS (Prob,); + DECLARE_CLASSNAME(Prob); + + void init_vars (); +protected: + SCM mutable_property_alist_; + SCM immutable_property_alist_; + + virtual void derived_mark () const; + virtual SCM copy_mutable_properties () const; + virtual void type_check_assignment (SCM,SCM) const; + +public: + Prob (SCM); + Prob (Prob const &); + virtual String name () const; + + SCM get_property_alist (bool mutble) const; + SCM internal_get_property (SCM sym) const; + void internal_set_property (SCM sym, SCM val); +}; + +DECLARE_UNSMOB (Prob, prob); + +#endif /* PROPERTY_OBJECT_HH */ diff --git a/lily/include/smobs.hh b/lily/include/smobs.hh index ae90ae27dd..be08a073b0 100644 --- a/lily/include/smobs.hh +++ b/lily/include/smobs.hh @@ -10,6 +10,7 @@ #define SMOBS_HH #include "lily-guile.hh" +#include "virtual-methods.hh" /* Smobs are GUILEs mechanism of exporting C(++) objects to the Scheme diff --git a/lily/music-function.cc b/lily/music-function.cc index e077624fe6..4f49a05519 100644 --- a/lily/music-function.cc +++ b/lily/music-function.cc @@ -33,13 +33,15 @@ LY_DEFINE (ly_make_music_function, "ly:make-music-function", 2, 0, 0, "Its arguments. @code{signature} is a list containing either " "@code{ly:music?} predicates or other type predicates.") { + extern SCM ly_music_p_proc; + String str = ""; for (SCM s = signature; scm_is_pair (s); s = scm_cdr (s)) { if (str != "") str += "-"; - if (scm_car (s) == Music_type_p_proc) + if (scm_car (s) == ly_music_p_proc) str += "music"; else if (scm_car (s) == ly_lily_module_constant ("markup?")) str += "markup"; diff --git a/lily/music-scheme.cc b/lily/music-scheme.cc index 8d2119b653..4da2726782 100644 --- a/lily/music-scheme.cc +++ b/lily/music-scheme.cc @@ -62,6 +62,13 @@ LY_DEFINE (ly_make_music, "ly:make-music", return ms->unprotect (); } +LY_DEFINE (ly_music_p, "ly:music?", + 1, 0, 0, (SCM obj), + "Type predicate") +{ + return scm_from_bool (unsmob_music (obj)); +} + /* todo: property args */ LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties", 1, 0, 0, (SCM mus), @@ -214,3 +221,4 @@ LY_DEFINE (ly_transpose_key_alist, "ly:transpose-key-alist", } return scm_reverse_x (newlist, SCM_EOL); } + diff --git a/lily/music.cc b/lily/music.cc index fddd57fd0a..cba10da79c 100644 --- a/lily/music.cc +++ b/lily/music.cc @@ -31,23 +31,12 @@ Music::internal_is_music_type (SCM k) const return scm_c_memq (k, ifs) != SCM_BOOL_F; } -String -Music::name () const -{ - SCM nm = get_property ("name"); - if (scm_is_symbol (nm)) - return ly_symbol2string (nm); - else - return "Music"; -} - Music::Music (SCM init) + : Prob (init) { - self_scm_ = SCM_EOL; - immutable_property_alist_ = init; - mutable_property_alist_ = SCM_EOL; - smobify_self (); - + length_callback_ = SCM_EOL; + start_callback_ = SCM_EOL; + length_callback_ = get_property ("length-callback"); if (!ly_is_procedure (length_callback_)) length_callback_ = duration_length_callback_proc; @@ -55,38 +44,34 @@ Music::Music (SCM init) start_callback_ = get_property ("start-callback"); } -Music::Music (Music const &m) +void +Music::derived_mark () const { - immutable_property_alist_ = m.immutable_property_alist_; - mutable_property_alist_ = SCM_EOL; - self_scm_ = SCM_EOL; - - /* First we smobify_self, then we copy over the stuff. If we don't, - stack vars that hold the copy might be optimized away, meaning - that they won't be protected from GC. */ - smobify_self (); - mutable_property_alist_ = ly_music_deep_copy (m.mutable_property_alist_); - length_callback_ = m.length_callback_; - start_callback_ = m.start_callback_; - set_spot (*m.origin ()); + scm_gc_mark (length_callback_); + scm_gc_mark (start_callback_); } -Music::~Music () +SCM +Music::copy_mutable_properties () const { + return ly_music_deep_copy (mutable_property_alist_); } -SCM -Music::get_property_alist (bool m) const +void +Music::type_check_assignment (SCM s, SCM v) const { - return (m) ? mutable_property_alist_ : immutable_property_alist_; + if (!::type_check_assignment (s, v, ly_symbol2scm ("music-type?"))) + abort (); } -SCM -Music::mark_smob (SCM m) +Music::Music (Music const &m) + : Prob (m) { - Music *mus = (Music *) SCM_CELL_WORD_1 (m); - scm_gc_mark (mus->immutable_property_alist_); - return mus->mutable_property_alist_; + length_callback_ = m.length_callback_; + start_callback_ = m.start_callback_; + + /// why? + set_spot (*m.origin ()); } Moment @@ -132,25 +117,6 @@ print_alist (SCM a, SCM port) } } -int -Music::print_smob (SCM s, SCM p, scm_print_state*) -{ - scm_puts ("#get_property ("name"); - if (scm_is_symbol (nm) || scm_is_string (nm)) - scm_display (nm, p); - else - scm_puts ("Music", p); - - /* Printing properties takes a lot of time, especially during backtraces. - For inspecting, it is better to explicitly use an inspection - function. */ - - scm_puts (">", p); - return 1; -} Pitch Music::generic_to_relative_octave (Pitch last) @@ -253,42 +219,7 @@ Music::transpose (Pitch delta) set_property ("pitch-alist", ly_transpose_key_alist (pa, delta.smobbed_copy ())); } -IMPLEMENT_TYPE_P (Music, "ly:music?"); -IMPLEMENT_SMOBS (Music); -IMPLEMENT_DEFAULT_EQUAL_P (Music); -SCM -Music::internal_get_property (SCM sym) const -{ - SCM s = scm_sloppy_assq (sym, mutable_property_alist_); - if (s != SCM_BOOL_F) - return scm_cdr (s); - - s = scm_sloppy_assq (sym, immutable_property_alist_); - return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s); -} - -SCM -Music::internal_get_object (SCM s) const -{ - return internal_get_property (s); -} - -void -Music::internal_set_object (SCM s, SCM v) -{ - return internal_set_property (s, v); -} - -void -Music::internal_set_property (SCM s, SCM v) -{ - if (do_internal_type_checking_global) - if (!type_check_assignment (s, v, ly_symbol2scm ("music-type?"))) - abort (); - - mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v); -} void Music::set_spot (Input ip) @@ -327,3 +258,10 @@ Music::duration_length_callback (SCM m) mom = d->get_length (); return mom.smobbed_copy (); } + +Music * +unsmob_music (SCM m) +{ + return dynamic_cast (unsmob_prob (m)); +} + diff --git a/lily/paper-system.cc b/lily/paper-system.cc index b8fdfaa9d0..d5e8b914ba 100644 --- a/lily/paper-system.cc +++ b/lily/paper-system.cc @@ -9,18 +9,9 @@ #include "paper-system.hh" #include "item.hh" -#include "ly-smobs.icc" - -IMPLEMENT_SMOBS (Paper_system); -IMPLEMENT_TYPE_P (Paper_system, "ly:paper-system?"); -IMPLEMENT_DEFAULT_EQUAL_P (Paper_system); - Paper_system::Paper_system (Stencil s, SCM immutable_init) + : Prob (immutable_init) { - mutable_property_alist_ = SCM_EOL; - immutable_property_alist_ = immutable_init; - smobify_self (); - SCM yext = get_property ("Y-extent"); if (is_number_pair (yext)) @@ -31,56 +22,20 @@ Paper_system::Paper_system (Stencil s, SCM immutable_init) s = Stencil (b, s.expr ()); } - set_property ("stencil", s.smobbed_copy ()); -} - -Paper_system::~Paper_system () -{ -} - -SCM -Paper_system::mark_smob (SCM smob) -{ - Paper_system *system = (Paper_system *) SCM_CELL_WORD_1 (smob); - scm_gc_mark (system->mutable_property_alist_); - return system->immutable_property_alist_; -} - -int -Paper_system::print_smob (SCM smob, SCM port, scm_print_state*) -{ - Paper_system *p = (Paper_system *) SCM_CELL_WORD_1 (smob); - scm_puts ("#<", port); - scm_puts ("Paper_system", port); - scm_display (p->mutable_property_alist_, port); - scm_display (p->immutable_property_alist_, port); - - scm_puts (" >\n", port); - return 1; -} - -void -Paper_system::init_vars () -{ + set_property ("stencil", s.smobbed_copy ()); } -SCM -Paper_system::internal_get_property (SCM sym) const +Paper_system * +unsmob_paper_system (SCM x) { - /* - TODO: type checking - */ - SCM s = scm_sloppy_assq (sym, mutable_property_alist_); - if (s != SCM_BOOL_F) - return scm_cdr (s); - - s = scm_sloppy_assq (sym, immutable_property_alist_); - return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s); + Prob *prob = unsmob_prob (x); + return dynamic_cast (prob); } -void -Paper_system::internal_set_property (SCM sym, SCM val) +LY_DEFINE(ly_paper_system_p, "ly:paper-system?", + 1,0,0, (SCM obj), + "Type predicate.") { - mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, val); + return scm_from_bool (unsmob_paper_system (obj)); } diff --git a/lily/prob.cc b/lily/prob.cc new file mode 100644 index 0000000000..4dbd2858ab --- /dev/null +++ b/lily/prob.cc @@ -0,0 +1,129 @@ +/* + paper-system.cc -- implement Paper_system + + source file of the GNU LilyPond music typesetter + + (c) 2004--2006 Jan Nieuwenhuizen +*/ + +#include "prob.hh" +#include "main.hh" +#include "item.hh" + +#include "ly-smobs.icc" + +IMPLEMENT_SMOBS (Prob); +IMPLEMENT_TYPE_P (Prob, "ly:prob?"); +IMPLEMENT_DEFAULT_EQUAL_P (Prob); + +Prob::Prob (SCM immutable_init) +{ + self_scm_ = SCM_EOL; + mutable_property_alist_ = SCM_EOL; + immutable_property_alist_ = immutable_init; + smobify_self (); +} + +Prob::~Prob () +{ +} + +Prob::Prob (Prob const &src) +{ + immutable_property_alist_ = src.immutable_property_alist_; + mutable_property_alist_ = SCM_EOL; + self_scm_ = SCM_EOL; + + /* First we smobify_self, then we copy over the stuff. If we don't, + stack vars that hold the copy might be optimized away, meaning + that they won't be protected from GC. */ + smobify_self (); + mutable_property_alist_ = src.copy_mutable_properties (); +} + + +SCM +Prob::copy_mutable_properties () const +{ + return ly_deep_copy (mutable_property_alist_); +} + +void +Prob::derived_mark () const +{ +} + +SCM +Prob::mark_smob (SCM smob) +{ + Prob *system = (Prob *) SCM_CELL_WORD_1 (smob); + scm_gc_mark (system->mutable_property_alist_); + system->derived_mark (); + + return system->immutable_property_alist_; +} + +int +Prob::print_smob (SCM smob, SCM port, scm_print_state*) +{ + Prob *p = (Prob *) SCM_CELL_WORD_1 (smob); + scm_puts ("#<", port); + scm_puts ("Prob: ", port); + + + scm_puts (p->class_name (), port); + scm_display (p->mutable_property_alist_, port); + scm_display (p->immutable_property_alist_, port); + + scm_puts (" >\n", port); + return 1; +} + + + +SCM +Prob::internal_get_property (SCM sym) const +{ + /* + TODO: type checking + */ + SCM s = scm_sloppy_assq (sym, mutable_property_alist_); + if (s != SCM_BOOL_F) + return scm_cdr (s); + + s = scm_sloppy_assq (sym, immutable_property_alist_); + return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s); +} + +void +Prob::internal_set_property (SCM sym, SCM val) +{ + if (do_internal_type_checking_global) + type_check_assignment (sym, val); + + mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, val); +} + +void +Prob::type_check_assignment (SCM sym, SCM val) const +{ + (void) sym; + (void) val; +} + +SCM +Prob::get_property_alist (bool m) const +{ + return (m) ? mutable_property_alist_ : immutable_property_alist_; +} + +String +Prob::name () const +{ + SCM nm = get_property ("name"); + if (scm_is_symbol (nm)) + return ly_symbol2string (nm); + else + return this->class_name (); +} + -- 2.39.2