r3920 - in trunk/birnet: . tests



Author: timj
Date: 2006-09-30 12:07:05 -0400 (Sat, 30 Sep 2006)
New Revision: 3920

Added:
   trunk/birnet/birnetsignal.cc
Modified:
   trunk/birnet/ChangeLog
   trunk/birnet/Makefile.am
   trunk/birnet/birnetsignal.hh
   trunk/birnet/birnetsignalslot.hh
   trunk/birnet/birnetutilsxx.cc
   trunk/birnet/birnetutilsxx.hh
   trunk/birnet/tests/infotest.cc
Log:
Sat Sep 30 18:00:13 2006  Tim Janik  <timj gtk org>

        * birnetsignal.hh, birnetsignal.cc: moved virtual methods from .hh to
        .cc to emit fewer typeinfos.

        * birnetutilsxx.hh, birnetutilsxx.cc: moved DataList::NodeBase, 
        Deletable and ReferenceCountImpl destructors into .cc file to emit
        fewer typeinfos. 
        added class VirtualTypeid and moved cxx_demangle() into this class.
        let Deletable derive from VirtualTypeid to have typeid methods available
        for allmost all virtual classes. renamed Deletable::DestructionHook to
        Deletable::DeletionHook for consistency.

        * tests/infotest.cc (test_virtual_typeid): test VirtualTypeid methods.




Modified: trunk/birnet/ChangeLog
===================================================================
--- trunk/birnet/ChangeLog	2006-09-28 23:11:46 UTC (rev 3919)
+++ trunk/birnet/ChangeLog	2006-09-30 16:07:05 UTC (rev 3920)
@@ -1,3 +1,18 @@
+Sat Sep 30 18:00:13 2006  Tim Janik  <timj gtk org>
+
+	* birnetsignal.hh, birnetsignal.cc: moved virtual methods from .hh to
+	.cc to emit fewer typeinfos.
+
+	* birnetutilsxx.hh, birnetutilsxx.cc: moved DataList::NodeBase, 
+	Deletable and ReferenceCountImpl destructors into .cc file to emit
+	fewer typeinfos. 
+	added class VirtualTypeid and moved cxx_demangle() into this class.
+	let Deletable derive from VirtualTypeid to have typeid methods available
+	for allmost all virtual classes. renamed Deletable::DestructionHook to
+	Deletable::DeletionHook for consistency.
+
+	* tests/infotest.cc (test_virtual_typeid): test VirtualTypeid methods.
+
 Thu Sep 28 01:01:55 2006  Tim Janik  <timj gtk org>
 
 	* birnetcore.h: introduce BIRNET_CONSTRUCTOR which wraps gcc's 

Modified: trunk/birnet/Makefile.am
===================================================================
--- trunk/birnet/Makefile.am	2006-09-28 23:11:46 UTC (rev 3919)
+++ trunk/birnet/Makefile.am	2006-09-30 16:07:05 UTC (rev 3920)
@@ -26,6 +26,7 @@
 	birnetcpu.c			\
 	birnetmsg.c			\
 	birnetring.c			\
+	birnetsignal.cc                 \
 	birnetthread.c			\
 	birnetthreadxx.cc		\
 	birnetutils.c			\

Added: trunk/birnet/birnetsignal.cc
===================================================================
--- trunk/birnet/birnetsignal.cc	2006-09-28 23:11:46 UTC (rev 3919)
+++ trunk/birnet/birnetsignal.cc	2006-09-30 16:07:05 UTC (rev 3920)
@@ -0,0 +1,49 @@
+/* BirnetSignal
+ * Copyright (C) 2005-2006 Tim Janik
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#include "birnetsignal.hh"
+
+namespace Birnet {
+namespace Signals {
+
+/* --- TrampolineLink --- */
+TrampolineLink::~TrampolineLink()
+{
+  if (next || prev)
+    {
+      next->prev = prev;
+      prev->next = next;
+      prev = next = NULL;
+    }
+}
+
+/* --- SignalBase --- */
+bool
+SignalBase::EmbeddedLink::operator== (const TrampolineLink &other) const
+{
+  return false;
+}
+
+void
+SignalBase::EmbeddedLink::delete_this ()
+{
+  /* not deleting, because this structure is always embedded as SignalBase::start */
+}
+
+} // Signals
+} // Birnet

Modified: trunk/birnet/birnetsignal.hh
===================================================================
--- trunk/birnet/birnetsignal.hh	2006-09-28 23:11:46 UTC (rev 3919)
+++ trunk/birnet/birnetsignal.hh	2006-09-30 16:07:05 UTC (rev 3920)
@@ -1,5 +1,5 @@
 /* BirnetSignal
- * Copyright (C) 2005 Tim Janik
+ * Copyright (C) 2005-2006 Tim Janik
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -39,33 +39,22 @@
   TrampolineLink *next, *prev;
   uint            callable : 1;
   uint            with_emitter : 1;
-  explicit        TrampolineLink() :
-    next (NULL), prev (NULL), callable (true), with_emitter (false)
-  {}
+  explicit        TrampolineLink() : next (NULL), prev (NULL), callable (true), with_emitter (false) {}
   virtual bool    operator== (const TrampolineLink &other) const = 0;
-  virtual         ~TrampolineLink()
-  {
-    if (next || prev)
-      {
-        next->prev = prev;
-        prev->next = next;
-        prev = next = NULL;
-      }
-  }
+  virtual        ~TrampolineLink();
   BIRNET_PRIVATE_CLASS_COPY (TrampolineLink);
 };
 
 /* --- SignalBase --- */
-struct SignalBase {
-private:
+class SignalBase {
   class EmbeddedLink : public TrampolineLink {
-    virtual bool operator== (const TrampolineLink &other) const { return false; }
-    virtual void delete_this ()         { /* embedded */ }
+    virtual bool operator== (const TrampolineLink &other) const;
+    virtual void delete_this ();
   public:
     void         check_last_ref() const { BIRNET_ASSERT (ref_count() == 1); }
   };
 protected:
-  EmbeddedLink      start;
+  EmbeddedLink   start;
   void
   connect_link (TrampolineLink *link,
                 bool            with_emitter = false)
@@ -97,7 +86,7 @@
   }
   template<class Emission> struct Iterator;
 public:
-  explicit SignalBase ()
+  SignalBase ()
   {
     start.next = &start;
     start.prev = &start;

Modified: trunk/birnet/birnetsignalslot.hh
===================================================================
--- trunk/birnet/birnetsignalslot.hh	2006-09-28 23:11:46 UTC (rev 3919)
+++ trunk/birnet/birnetsignalslot.hh	2006-09-30 16:07:05 UTC (rev 3920)
@@ -48,7 +48,7 @@
   {}
 };
 template<class Class, typename R0, typename A1, typename A2, typename A3>
-class MethodTrampoline3 : public Trampoline3 <R0, A1, A2, A3>, public virtual Deletable::DestructionHook {
+class MethodTrampoline3 : public Trampoline3 <R0, A1, A2, A3>, public virtual Deletable::DeletionHook {
   friend void FIXME_dummy_friend_for_gcc33();
   typedef R0 (Class::*Method) (A1, A2, A3);
   Class *instance;
@@ -83,7 +83,7 @@
   {}
 };
 template<class Class, typename R0, typename A1, typename A2, typename A3, typename Data>
-class DataMethodTrampoline3 : public Trampoline3 <R0, A1, A2, A3>, public virtual Deletable::DestructionHook {
+class DataMethodTrampoline3 : public Trampoline3 <R0, A1, A2, A3>, public virtual Deletable::DeletionHook {
   friend void FIXME_dummy_friend_for_gcc33();
   typedef R0 (Class::*Method) (A1, A2, A3, Data);
   Class *instance; Method method; Data data;

Modified: trunk/birnet/birnetutilsxx.cc
===================================================================
--- trunk/birnet/birnetutilsxx.cc	2006-09-28 23:11:46 UTC (rev 3919)
+++ trunk/birnet/birnetutilsxx.cc	2006-09-30 16:07:05 UTC (rev 3920)
@@ -111,10 +111,25 @@
 
 namespace Birnet {
 
-/* --- demangling --- */
+/* --- VirtualTypeid --- */
+VirtualTypeid::~VirtualTypeid ()
+{ /* virtual destructor ensures vtable */ }
+
 String
-cxx_demangle (const char *mangled_identifier)
+VirtualTypeid::typeid_name ()
 {
+  return typeid (*this).name();
+}
+
+String
+VirtualTypeid::typeid_pretty_name ()
+{
+  return cxx_demangle (typeid (*this).name());
+}
+
+String
+VirtualTypeid::cxx_demangle (const char *mangled_identifier)
+{
   int status = 0;
   char *malloced_result = abi::__cxa_demangle (mangled_identifier, NULL, NULL, &status);
   String result = malloced_result && !status ? malloced_result : mangled_identifier;
@@ -122,6 +137,7 @@
     free (malloced_result);
   return result;
 }
+
 /* --- InitHooks --- */
 static InitHook *init_hooks = NULL;
 
@@ -245,21 +261,25 @@
 } // Path
 
 /* --- Deletable --- */
+Deletable::~Deletable ()
+{
+  invoke_deletion_hooks();
+}
 
 /**
  * @param deletable     possible Deletable* handle
  * @return              TRUE if the hook was added
  *
- * Adds the destruction hook to @a deletable if it is non NULL.
- * The destruction hook is asserted to be so far uninstalled.
+ * Adds the deletion hook to @a deletable if it is non NULL.
+ * The deletion hook is asserted to be so far uninstalled.
  * This function is MT-safe and may be called from any thread.
  */
 bool
-Deletable::DestructionHook::deletable_add_hook (Deletable *deletable)
+Deletable::DeletionHook::deletable_add_hook (Deletable *deletable)
 {
   if (deletable)
     {
-      deletable->add_destruction_hook (this);
+      deletable->add_deletion_hook (this);
       return true;
     }
   return false;
@@ -269,16 +289,16 @@
  * @param deletable     possible Deletable* handle
  * @return              TRUE if the hook was removed
  *
- * Removes the destruction hook from @a deletable if it is non NULL.
- * The destruction hook is asserted to be installed on @a deletable.
+ * Removes the deletion hook from @a deletable if it is non NULL.
+ * The deletion hook is asserted to be installed on @a deletable.
  * This function is MT-safe and may be called from any thread.
  */
 bool
-Deletable::DestructionHook::deletable_remove_hook (Deletable *deletable)
+Deletable::DeletionHook::deletable_remove_hook (Deletable *deletable)
 {
   if (deletable)
     {
-      deletable->remove_destruction_hook (this);
+      deletable->remove_deletion_hook (this);
       return true;
     }
   return false;
@@ -286,24 +306,24 @@
 
 static struct {
   Mutex                                            mutex;
-  std::map<Deletable*,Deletable::DestructionHook*> dmap;
+  std::map<Deletable*,Deletable::DeletionHook*> dmap;
 } deletable_maps[19]; /* use prime size for hashing, sum up to roughly 1k (use 83 for 4k) */
 
 /**
- * @param hook  valid destruction hook
+ * @param hook  valid deletion hook
  *
- * Add an uninstalled destruction hook to the deletable.
+ * Add an uninstalled deletion hook to the deletable.
  * This function is MT-safe and may be called from any thread.
  */
 void
-Deletable::add_destruction_hook (DestructionHook *hook)
+Deletable::add_deletion_hook (DeletionHook *hook)
 {
   uint32 hashv = ((gsize) (void*) this) % (sizeof (deletable_maps) / sizeof (deletable_maps[0]));
   deletable_maps[hashv].mutex.lock();
   BIRNET_ASSERT (hook);
   BIRNET_ASSERT (!hook->next);
   BIRNET_ASSERT (!hook->prev);
-  std::map<Deletable*,DestructionHook*>::iterator it;
+  std::map<Deletable*,DeletionHook*>::iterator it;
   it = deletable_maps[hashv].dmap.find (this);
   if (it != deletable_maps[hashv].dmap.end())
     {
@@ -319,13 +339,13 @@
 }
 
 /**
- * @param hook  valid destruction hook
+ * @param hook  valid deletion hook
  *
- * Remove a previously added destruction hook.
+ * Remove a previously added deletion hook.
  * This function is MT-safe and may be called from any thread.
  */
 void
-Deletable::remove_destruction_hook (DestructionHook *hook)
+Deletable::remove_deletion_hook (DeletionHook *hook)
 {
   uint32 hashv = ((gsize) (void*) this) % (sizeof (deletable_maps) / sizeof (deletable_maps[0]));
   deletable_maps[hashv].mutex.lock();
@@ -336,7 +356,7 @@
     hook->prev->next = hook->next;
   else
     {
-      std::map<Deletable*,DestructionHook*>::iterator it;
+      std::map<Deletable*,DeletionHook*>::iterator it;
       it = deletable_maps[hashv].dmap.find (this);
       BIRNET_ASSERT (it != deletable_maps[hashv].dmap.end());
       BIRNET_ASSERT (it->second == hook);
@@ -349,18 +369,18 @@
 }
 
 /**
- * Invoke all destruction hooks installed on this deletable.
+ * Invoke all deletion hooks installed on this deletable.
  */
 void
-Deletable::invoke_destruction_hooks()
+Deletable::invoke_deletion_hooks()
 {
   uint32 hashv = ((gsize) (void*) this) % (sizeof (deletable_maps) / sizeof (deletable_maps[0]));
   while (TRUE)
     {
       /* lookup hook list */
       deletable_maps[hashv].mutex.lock();
-      std::map<Deletable*,DestructionHook*>::iterator it;
-      DestructionHook *hooks;
+      std::map<Deletable*,DeletionHook*>::iterator it;
+      DeletionHook *hooks;
       it = deletable_maps[hashv].dmap.find (this);
       if (it != deletable_maps[hashv].dmap.end())
         {
@@ -376,7 +396,7 @@
       /* process hooks */
       while (hooks)
         {
-          DestructionHook *hook = hooks;
+          DeletionHook *hook = hooks;
           hooks = hook->next;
           if (hooks)
             hooks->prev = NULL;
@@ -388,8 +408,26 @@
     }
 }
 
+/* --- ReferenceCountImpl --- */
+void
+ReferenceCountImpl::finalize ()
+{}
 
+void
+ReferenceCountImpl::delete_this ()
+{
+  delete this;
+}
+
+ReferenceCountImpl::~ReferenceCountImpl ()
+{
+  BIRNET_ASSERT (ref_count() == 0);
+}
+
 /* --- DataList --- */
+DataList::NodeBase::~NodeBase ()
+{}
+
 void
 DataList::set_data (NodeBase *node)
 {

Modified: trunk/birnet/birnetutilsxx.hh
===================================================================
--- trunk/birnet/birnetutilsxx.hh	2006-09-28 23:11:46 UTC (rev 3919)
+++ trunk/birnet/birnetutilsxx.hh	2006-09-30 16:07:05 UTC (rev 3920)
@@ -56,7 +56,14 @@
 typedef std::string String;
 using std::vector;
 using std::map;       
-String  cxx_demangle    (const char  *mangled_identifier);
+class VirtualTypeid {
+protected:
+  virtual      ~VirtualTypeid      ();
+public:
+  String        typeid_name        ();
+  String        typeid_pretty_name ();
+  static String cxx_demangle       (const char *mangled_identifier);
+};
 
 /* --- private copy constructor and assignment operator --- */
 #define BIRNET_PRIVATE_CLASS_COPY(Class)        private: Class (const Class&); Class& operator= (const Class&);
@@ -106,25 +113,25 @@
 }
 
 /* --- Deletable --- */
-struct Deletable {
-  class DestructionHook {
-    DestructionHook    *prev;
-    DestructionHook    *next;
+struct Deletable : public virtual VirtualTypeid {
+  class DeletionHook {
+    DeletionHook    *prev;
+    DeletionHook    *next;
     friend class Deletable;
   public:
-    explicit            DestructionHook       () : prev (NULL), next (NULL) {}
-    virtual void        deletable_dispose     (Deletable &deletable) = 0;
-    bool                deletable_add_hook    (void      *any)              { return false; }
-    bool                deletable_add_hook    (Deletable *deletable);
-    bool                deletable_remove_hook (void      *any)              { return false; }
-    bool                deletable_remove_hook (Deletable *deletable);
+    explicit     DeletionHook          () : prev (NULL), next (NULL) {}
+    virtual void deletable_dispose     (Deletable &deletable) = 0;
+    bool         deletable_add_hook    (void      *any)              { return false; }
+    bool         deletable_add_hook    (Deletable *deletable);
+    bool         deletable_remove_hook (void      *any)              { return false; }
+    bool         deletable_remove_hook (Deletable *deletable);
   };
 private:
-  void    add_destruction_hook     (DestructionHook *hook);
-  void    remove_destruction_hook  (DestructionHook *hook);
+  void           add_deletion_hook     (DeletionHook *hook);
+  void           remove_deletion_hook  (DeletionHook *hook);
 protected:
-  void    invoke_destruction_hooks ();
-  virtual ~Deletable() { invoke_destruction_hooks(); }
+  void           invoke_deletion_hooks ();
+  virtual       ~Deletable             ();
 };
 
 /* --- ReferenceCountImpl --- */
@@ -222,19 +229,9 @@
   template<class Obj> static void sink     (Obj &obj) { obj.ref_sink(); obj.unref(); }
   template<class Obj> static void sink     (Obj *obj) { obj->ref_sink(); obj->unref(); }
 protected:
-  virtual void
-  finalize()
-  {}
-  virtual void
-  delete_this()
-  {
-    delete this;
-  }
-  virtual
-  ~ReferenceCountImpl()
-  {
-    BIRNET_ASSERT (ref_count() == 0);
-  }
+  virtual void finalize           ();
+  virtual void delete_this        ();
+  virtual     ~ReferenceCountImpl ();
 };
 template<class Obj> static Obj& ref      (Obj &obj) { obj.ref();       return obj; }
 template<class Obj> static Obj* ref      (Obj *obj) { obj->ref();      return obj; }
@@ -332,19 +329,16 @@
   protected:
     NodeBase      *next;
     DataKey<void> *key;
-    explicit       NodeBase (DataKey<void> *k) :
-      next (NULL),
-      key (k)
-    {}
-    virtual        ~NodeBase () {}
+    explicit       NodeBase (DataKey<void> *k) : next (NULL), key (k) {}
+    virtual       ~NodeBase ();
     friend         class DataList;
   };
   template<typename T>
   class Node : public NodeBase {
     T data;
   public:
-    T   get_data ()     { return data; }
-    T   swap     (T d)  { T result = data; data = d; return result; }
+    T        get_data ()     { return data; }
+    T        swap     (T d)  { T result = data; data = d; return result; }
     virtual ~Node()
     {
       if (key)
@@ -353,8 +347,8 @@
           dkey->destroy (data);
         }
     }
-    Node (DataKey<T> *k,
-          T           d) :
+    explicit Node (DataKey<T> *k,
+                   T           d) :
       NodeBase (reinterpret_cast<DataKey<void>*> (k)),
       data (d)
     {}

Modified: trunk/birnet/tests/infotest.cc
===================================================================
--- trunk/birnet/tests/infotest.cc	2006-09-28 23:11:46 UTC (rev 3919)
+++ trunk/birnet/tests/infotest.cc	2006-09-30 16:07:05 UTC (rev 3920)
@@ -104,6 +104,21 @@
   TDONE();
 }
 
+static void
+test_virtual_typeid()
+{
+  struct TypeA : public virtual VirtualTypeid {};
+  struct TypeB : public virtual VirtualTypeid {};
+  TSTART ("VirtualTypeid");
+  TypeA a;
+  TypeB b;
+  TOK();
+  TASSERT (a.typeid_name() != b.typeid_name());
+  TASSERT (strstr (a.typeid_pretty_name().c_str(), "TypeA") != NULL);
+  TASSERT (strstr (b.typeid_pretty_name().c_str(), "TypeB") != NULL);
+  TDONE();
+}
+
 int
 main (int   argc,
       char *argv[])
@@ -114,7 +129,8 @@
   test_paths();
   test_zintern();
   test_files (argv[0]);
-
+  test_virtual_typeid();
+  
   return 0;
 }
 




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]