]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/include/smobs.hh
Issue 2787: Sanitize usage of -DDEBUG, -DNDEBUG and assert
[lilypond.git] / lily / include / smobs.hh
index eaf041312f9ae3a734816018366382c0ad6789e8..7097bb3d48c6605b4a08ff769d54bc80be9e0651 100644 (file)
@@ -1,7 +1,7 @@
 /*
   This file is part of LilyPond, the GNU music typesetter.
 
-  Copyright (C) 1999--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  Copyright (C) 1999--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
 
   LilyPond is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -21,6 +21,7 @@
 #define SMOBS_HH
 
 #include "lily-guile.hh"
+#include "lily-proto.hh"
 #include "warn.hh"
 #include <string>
 
 // class) to make sure the variable is actually instantiated.
 
 class Scm_init {
+  static const Scm_init * list_;
+  void (*const fun_)(void);
+  Scm_init const * const next_;
+  Scm_init ();          // don't use default constructor, don't define
+  Scm_init (const Scm_init &);  // don't define copy constructor
 public:
-  Scm_init () { }
-  Scm_init (void (*fun) (void))
-  {
-    add_scm_init_func (fun);
-  }
+  Scm_init (void (*fun) (void)) : fun_ (fun), next_ (list_)
+  { list_ = this; }
+  static void init ();
 };
 
 template <class Super>
@@ -231,6 +235,14 @@ public:
   }
 };
 
+// derived_unsmob includes a dynamic_cast:
+
+template <class T>
+inline T *derived_unsmob (SCM arg)
+{
+  return dynamic_cast<T *> (T::unsmob (arg));
+}
+
 // Simple smobs
 template <class Super>
 class Simple_smob : public Smob_base<Super> {
@@ -250,14 +262,25 @@ public:
 void protect_smob (SCM smob, SCM *prot_cons);
 void unprotect_smob (SCM smob, SCM *prot_cons);
 
+// The Smob_core class is not templated and contains material not
+// depending on the Super class.
+
+class Smob_core {
+protected:
+  SCM self_scm_;
+  Smob_core () : self_scm_ (SCM_UNDEFINED) { };
+public:
+  SCM self_scm () const { return self_scm_; }
+  Listener get_listener (SCM callback);
+};
+
 template <class Super>
-class Smob : public Smob_base<Super> {
+class Smob : public Smob_core, public Smob_base<Super> {
 private:
-  SCM self_scm_;
   SCM protection_cons_;
   Smob (const Smob<Super> &); // Do not define!  Not copyable!
 protected:
-  Smob () : self_scm_ (SCM_UNDEFINED), protection_cons_ (SCM_EOL) { };
+  Smob () : protection_cons_ (SCM_EOL) { };
 public:
   static size_t free_smob (SCM obj)
   {
@@ -266,23 +289,23 @@ public:
   }
   SCM unprotected_smobify_self ()
   {
-    self_scm_ = Smob_base<Super>::register_ptr (static_cast<Super *> (this));
-    return self_scm_;
+    SCM s = Smob_base<Super>::register_ptr (static_cast<Super *> (this));
+    self_scm_ = s;
+    return s;
   }
   void protect ()
   {
     protect_smob (self_scm_, &protection_cons_);
   }
+  void smobify_self () {
+    protect_smob (unprotected_smobify_self (), &protection_cons_);
+  }
   SCM unprotect ()
   {
-    unprotect_smob (self_scm_, &protection_cons_);
-    return self_scm_;
-  }
-  void smobify_self () {
-    self_scm_ = unprotected_smobify_self ();
-    protect ();
+    SCM s = self_scm_;
+    unprotect_smob (s, &protection_cons_);
+    return s;
   }
-  SCM self_scm () const { return self_scm_; }
 };
 
 extern bool parsed_objects_should_be_dead;
@@ -310,7 +333,7 @@ public:
 // least some, so they are apparently not protected in spite of being
 // included in the GC scans.  So it would appear that scanning smobs
 // is not equivalent to marking them.  Ugh.
-#if !defined(NDEBUG) && !GUILEV2
+#if defined(DEBUG) && !GUILEV2
 #define ASSERT_LIVE_IS_ALLOWED(arg)                                     \
   do {                                                                  \
     static parsed_dead pass_here;                                       \