#define SMOBS_HH
#include "lily-guile.hh"
+#include "lily-proto.hh"
#include "warn.hh"
#include <string>
*/
+// Initialization class. Create a variable or static data member of
+// this type at global scope (or creation will happen too late for
+// Scheme initialization), initialising with a function to be called.
+// Reference somewhere (like in the constructor of the containing
+// 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 (void (*fun) (void)) : fun_ (fun), next_ (list_)
+ { list_ = this; }
+ static void init ();
+};
+
template <class Super>
class Smob_base
{
static scm_t_bits smob_tag_;
- static scm_t_bits init_id (void);
+ static Scm_init scm_init_;
+ static void init (void);
static string smob_name_;
static Super *unchecked_unsmob (SCM s)
{
return reinterpret_cast<Super *> (SCM_SMOB_DATA (s));
}
protected:
- // This is an initialization with side effect. It is called once,
- // the first time smob_tag is actually getting called. This
- // allocates and initializes the type before it is first used for
- // anything.
- static scm_t_bits smob_tag ()
- {
- static scm_t_bits tag = init_id ();
- return tag;
- }
+ // reference scm_init_ in smob_tag which is sure to be called. The
+ // constructor, in contrast, may not be called at all in classes
+ // like Smob1.
+ static scm_t_bits smob_tag () { (void) scm_init_; return smob_tag_; }
Smob_base () { }
static SCM register_ptr (Super *p);
static Super *unregister_ptr (SCM obj);
static const int smob_proc_signature_ = -1;
public:
- static void init (void)
- {
- // This is stupid, but without forcing initialization at the
- // Scheme startup hook stage, stuff like ly:undead? will not be
- // defined when the first Scheme files are loaded.
- //
- // So we provide an explicit initialization routine that can be
- // used with ADD_SCM_INIT_FUNC
- (void) smob_tag ();
- }
-#define ADD_SMOB_INIT(type) ADD_SCM_INIT_FUNC (Smob_init_ ## type, Smob_base<type>::init)
static bool is_smob (SCM s)
{
return SCM_SMOB_PREDICATE (smob_tag (), s);
}
};
+// 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> {
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)
{
unprotect_smob (s, &protection_cons_);
return s;
}
- SCM self_scm () const { return self_scm_; }
};
extern bool parsed_objects_should_be_dead;