merge with trunk
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 01 Aug 2007 19:52:59 +0200
changeset 1014 a19d3c07c95d
parent 1004 870afde857bc (current diff)
parent 1013 3eb895b2032a (diff)
child 1015 dfe3c1e5a4b8
merge with trunk
src/simulator/event-id.cc
src/simulator/event-id.h
--- a/src/core/ptr.cc	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/core/ptr.cc	Wed Aug 01 19:52:59 2007 +0200
@@ -28,9 +28,14 @@
 
 namespace ns3 {
 
+template <typename T>
+void Foo (void) {}
+
+
 class NoCount : public Object
 {
 public:
+  NoCount (void (*fn) (void));
   NoCount (Callback<void> cb);
   ~NoCount ();
   void Nothing (void) const;
@@ -292,12 +297,22 @@
     callback ();
   }
 
+
 #if 0
   // as expected, fails compilation.
   {
     Ptr<const Object> p = Create<NoCount> (cb);
     Callback<void> callback = MakeCallback (&NoCount::Nothing, p);
   }
+  // local types are not allowed as arguments to a template.
+  {
+    class B
+    {
+    public:
+      B () {}
+    };
+    Foo<B> ();
+  }
 #endif
   
 
--- a/src/core/ptr.h	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/core/ptr.h	Wed Aug 01 19:52:59 2007 +0200
@@ -65,7 +65,7 @@
   template <typename U>
   friend U *PeekPointer (const Ptr<U> &p);
 
-  void Acquire (void) const;
+  inline void Acquire (void) const;
 public:
   /**
    * Create an empty smart pointer
@@ -81,7 +81,16 @@
    * same, so that object is deleted if no more references to it
    * remain.
    */
-  Ptr (T *ptr);
+  Ptr (T *ptr);  
+  /**
+   * \param ptr raw pointer to manage
+   * \param ref if set to true, this method calls Ref, otherwise,
+   *        it does not call Ref.
+   *    
+   * Create a smart pointer which points to the object pointed to by
+   * the input raw pointer ptr.
+   */
+  Ptr (T *ptr, bool ref);
   Ptr (Ptr const&o);
   // allow conversions from T to T const.
   template <typename U>
@@ -379,6 +388,16 @@
 }
 
 template <typename T>
+Ptr<T>::Ptr (T *ptr, bool ref) 
+  : m_ptr (ptr)
+{
+  if (ref)
+    {
+      Acquire ();
+    }
+}
+
+template <typename T>
 Ptr<T>::Ptr (Ptr const&o) 
   : m_ptr (PeekPointer (o))
 {
--- a/src/simulator/event-id.cc	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/event-id.cc	Wed Aug 01 19:52:59 2007 +0200
@@ -30,7 +30,7 @@
     m_uid (0)
 {}
   
-EventId::EventId (EventImpl *impl, uint64_t ts, uint32_t uid)
+EventId::EventId (const Ptr<EventImpl> &impl, uint64_t ts, uint32_t uid)
   : m_eventImpl (impl),
     m_ts (ts),
     m_uid (uid)
@@ -38,11 +38,7 @@
 void 
 EventId::Cancel (void)
 {
-  if (!IsExpired ())
-    {
-      m_eventImpl->Cancel ();
-      m_eventImpl = 0;
-    }
+  Simulator::Cancel (*this);
 }
 bool 
 EventId::IsExpired (void) const
@@ -55,9 +51,9 @@
   return !IsExpired ();
 }
 EventImpl *
-EventId::GetEventImpl (void) const
+EventId::PeekEventImpl (void) const
 {
-  return m_eventImpl;
+  return PeekPointer (m_eventImpl);
 }
 uint64_t 
 EventId::GetTs (void) const
--- a/src/simulator/event-id.h	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/event-id.h	Wed Aug 01 19:52:59 2007 +0200
@@ -22,6 +22,8 @@
 #define EVENT_ID_H
 
 #include <stdint.h>
+#include "ns3/ptr.h"
+#include "event-impl.h"
 
 namespace ns3 {
 
@@ -33,7 +35,7 @@
 class EventId {
 public:
   EventId ();
-  EventId (EventImpl *impl, uint64_t ts, uint32_t uid);
+  EventId (const Ptr<EventImpl> &impl, uint64_t ts, uint32_t uid);
   /**
    * This method is syntactic sugar for the ns3::Simulator::cancel
    * method.
@@ -51,12 +53,12 @@
    * they are supposed to be invoked only by
    * subclasses of the Scheduler base class.
    */
-  EventImpl *GetEventImpl (void) const;
+  EventImpl *PeekEventImpl (void) const;
   uint64_t GetTs (void) const;
   uint32_t GetUid (void) const;
 private:
   friend bool operator == (const EventId &a, const EventId &b);
-  EventImpl *m_eventImpl;
+  Ptr<EventImpl> m_eventImpl;
   uint64_t m_ts;
   uint32_t m_uid;
 };
--- a/src/simulator/event-impl.cc	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/event-impl.cc	Wed Aug 01 19:52:59 2007 +0200
@@ -29,7 +29,8 @@
 {}
 
 EventImpl::EventImpl ()
-  : m_cancel (false)
+  : m_cancel (false),
+    m_count (1)
 {}
 void 
 EventImpl::Invoke (void)
--- a/src/simulator/event-impl.h	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/event-impl.h	Wed Aug 01 19:52:59 2007 +0200
@@ -28,6 +28,8 @@
 class EventImpl {
 public:
   EventImpl ();
+  inline void Ref (void) const;
+  inline void Unref (void) const;
   virtual ~EventImpl () = 0;
   void Invoke (void);
   void Cancel (void);
@@ -37,8 +39,28 @@
 private:
   friend class Event;
   bool m_cancel;
+  mutable uint32_t m_count;
 };
 
 }; // namespace ns3
 
+namespace ns3 {
+
+void
+EventImpl::Ref (void) const
+{
+  m_count++;
+}
+void
+EventImpl::Unref (void) const
+{
+  m_count--;
+  if (m_count == 0)
+    {
+      delete this;
+    }
+}
+
+} // namespace ns3
+
 #endif /* EVENT_IMPL_H */
--- a/src/simulator/scheduler-heap.cc	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/scheduler-heap.cc	Wed Aug 01 19:52:59 2007 +0200
@@ -170,7 +170,7 @@
 }
 
 bool
-SchedulerHeap::RealIsEmpty (void) const
+SchedulerHeap::IsEmpty (void) const
 {
   return (m_heap.size () == 1)?true:false;
 }
@@ -223,9 +223,11 @@
 
 
 void
-SchedulerHeap::RealInsert (EventId id)
+SchedulerHeap::Insert (const EventId &id)
 {
-  EventImpl *event = id.GetEventImpl ();
+  // acquire single ref
+  EventImpl *event = id.PeekEventImpl ();
+  event->Ref ();
   Scheduler::EventKey key;
   key.m_ts = id.GetTs ();
   key.m_uid = id.GetUid ();
@@ -234,29 +236,34 @@
 }
 
 EventId
-SchedulerHeap::RealPeekNext (void) const
+SchedulerHeap::PeekNext (void) const
 {
   std::pair<EventImpl *,Scheduler::EventKey> next = m_heap[Root ()];
   return EventId (next.first, next.second.m_ts, next.second.m_uid);
 }
-void     
-SchedulerHeap::RealRemoveNext (void)
+EventId
+SchedulerHeap::RemoveNext (void)
 {
+  std::pair<EventImpl *,Scheduler::EventKey> next = m_heap[Root ()];
   Exch (Root (), Last ());
   m_heap.pop_back ();
   TopDown (Root ());
+  return EventId (Ptr<EventImpl> (next.first, false), next.second.m_ts, next.second.m_uid);
 }
 
 
 bool
-SchedulerHeap::RealRemove (EventId id)
+SchedulerHeap::Remove (const EventId &id)
 {
   uint32_t uid = id.GetUid ();
   for (uint32_t i = 1; i < m_heap.size (); i++)
     {
       if (uid == m_heap[i].second.m_uid)
         {
-          NS_ASSERT (m_heap[i].first == id.GetEventImpl ());
+          NS_ASSERT (m_heap[i].first == id.PeekEventImpl ());
+          std::pair<EventImpl *,Scheduler::EventKey> next = m_heap[i];
+          // release single ref
+          next.first->Unref ();
           Exch (i, Last ());
           m_heap.pop_back ();
           TopDown (i);
--- a/src/simulator/scheduler-heap.h	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/scheduler-heap.h	Wed Aug 01 19:52:59 2007 +0200
@@ -35,13 +35,13 @@
   SchedulerHeap ();
   virtual ~SchedulerHeap ();
 
+  virtual void Insert (const EventId &id);
+  virtual bool IsEmpty (void) const;
+  virtual EventId PeekNext (void) const;
+  virtual EventId RemoveNext (void);
+  virtual bool Remove (const EventId &ev);
+
 private:
-  virtual void RealInsert (EventId id);
-  virtual bool RealIsEmpty (void) const;
-  virtual EventId RealPeekNext (void) const;
-  virtual void RealRemoveNext (void);
-  virtual bool RealRemove (EventId ev);
-
   typedef std::vector<std::pair<EventImpl *, Scheduler::EventKey> > BinaryHeap;
 
   inline uint32_t Parent (uint32_t id) const;
--- a/src/simulator/scheduler-list.cc	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/scheduler-list.cc	Wed Aug 01 19:52:59 2007 +0200
@@ -67,10 +67,12 @@
 }
 
 void
-SchedulerList::RealInsert (EventId id)
+SchedulerList::Insert (const EventId &id)
 {
   Scheduler::EventKey key;
-  EventImpl *event = id.GetEventImpl ();
+  // acquire refcount on EventImpl
+  EventImpl *event = id.PeekEventImpl ();
+  event->Ref ();
   key.m_ts = id.GetTs ();
   key.m_uid = id.GetUid ();
   for (EventsI i = m_events.begin (); i != m_events.end (); i++) 
@@ -84,31 +86,35 @@
   m_events.push_back (std::make_pair (event, key));
 }
 bool 
-SchedulerList::RealIsEmpty (void) const
+SchedulerList::IsEmpty (void) const
 {
   return m_events.empty ();
 }
 EventId
-SchedulerList::RealPeekNext (void) const
+SchedulerList::PeekNext (void) const
 {
   std::pair<EventImpl *, EventKey> next = m_events.front ();
   return EventId (next.first, next.second.m_ts, next.second.m_uid);
 }
 
-void
-SchedulerList::RealRemoveNext (void)
+EventId
+SchedulerList::RemoveNext (void)
 {
+  std::pair<EventImpl *, EventKey> next = m_events.front ();
   m_events.pop_front ();
+  return EventId (Ptr<EventImpl> (next.first,false), next.second.m_ts, next.second.m_uid);
 }
 
 bool
-SchedulerList::RealRemove (EventId id)
+SchedulerList::Remove (const EventId &id)
 {
   for (EventsI i = m_events.begin (); i != m_events.end (); i++) 
     {
       if (i->second.m_uid == id.GetUid ())
         {
-          NS_ASSERT (id.GetEventImpl () == i->first);
+          NS_ASSERT (id.PeekEventImpl () == i->first);
+          // release single acquire ref.
+          i->first->Unref ();
           m_events.erase (i);
           return true;
         }
--- a/src/simulator/scheduler-list.h	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/scheduler-list.h	Wed Aug 01 19:52:59 2007 +0200
@@ -37,13 +37,13 @@
   SchedulerList ();
   virtual ~SchedulerList ();
 
+  virtual void Insert (const EventId &id);
+  virtual bool IsEmpty (void) const;
+  virtual EventId PeekNext (void) const;
+  virtual EventId RemoveNext (void);
+  virtual bool Remove (const EventId &ev);
+
  private:
-  virtual void RealInsert (EventId id);
-  virtual bool RealIsEmpty (void) const;
-  virtual EventId RealPeekNext (void) const;
-  virtual void RealRemoveNext (void);
-  virtual bool RealRemove (EventId ev);
-
   inline bool IsLower (Scheduler::EventKey const*a, Scheduler::EventKey const*b) const;
 
   typedef std::list<std::pair<EventImpl*, EventKey> > Events;
--- a/src/simulator/scheduler-map.cc	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/scheduler-map.cc	Wed Aug 01 19:52:59 2007 +0200
@@ -88,9 +88,11 @@
 
 
 void
-SchedulerMap::RealInsert (EventId id)
+SchedulerMap::Insert (const EventId &id)
 {
-  EventImpl *event = id.GetEventImpl ();
+  // acquire a single ref
+  EventImpl *event = id.PeekEventImpl ();
+  event->Ref ();
   Scheduler::EventKey key;
   key.m_ts = id.GetTs ();
   key.m_uid = id.GetUid ();
@@ -100,33 +102,37 @@
 }
 
 bool
-SchedulerMap::RealIsEmpty (void) const
+SchedulerMap::IsEmpty (void) const
 {
   return m_list.empty ();
 }
 
 EventId
-SchedulerMap::RealPeekNext (void) const
+SchedulerMap::PeekNext (void) const
 {
   EventMapCI i = m_list.begin ();
   NS_ASSERT (i != m_list.end ());
   
   return EventId (i->second, i->first.m_ts, i->first.m_uid);
 }
-void
-SchedulerMap::RealRemoveNext (void)
+EventId
+SchedulerMap::RemoveNext (void)
 {
+  EventMapCI i = m_list.begin ();
   m_list.erase (m_list.begin ());
+  return EventId (Ptr<EventImpl> (i->second, false), i->first.m_ts, i->first.m_uid);
 }
 
 bool
-SchedulerMap::RealRemove (EventId id)
+SchedulerMap::Remove (const EventId &id)
 {
   Scheduler::EventKey key;
   key.m_ts = id.GetTs ();
   key.m_uid = id.GetUid ();
   EventMapI i = m_list.find (key);
-  NS_ASSERT (i->second == id.GetEventImpl ());
+  NS_ASSERT (i->second == id.PeekEventImpl ());
+  // release single ref.
+  i->second->Unref ();
   m_list.erase (i);
   return true;
 }
--- a/src/simulator/scheduler-map.h	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/scheduler-map.h	Wed Aug 01 19:52:59 2007 +0200
@@ -36,12 +36,12 @@
   SchedulerMap ();
   virtual ~SchedulerMap ();
 
+  virtual void Insert (const EventId &id);
+  virtual bool IsEmpty (void) const;
+  virtual EventId PeekNext (void) const;
+  virtual EventId RemoveNext (void);
+  virtual bool Remove (const EventId &ev);
 private:
-  virtual void RealInsert (EventId id);
-  virtual bool RealIsEmpty (void) const;
-  virtual EventId RealPeekNext (void) const;
-  virtual void RealRemoveNext (void);
-  virtual bool RealRemove (EventId ev);
 
   class EventKeyCompare {
   public:
--- a/src/simulator/scheduler.cc	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/scheduler.cc	Wed Aug 01 19:52:59 2007 +0200
@@ -27,33 +27,4 @@
 Scheduler::~Scheduler () 
 {}
 
-void
-Scheduler::Insert (EventId id)
-{
-  return RealInsert (id);
-}
-bool 
-Scheduler::IsEmpty (void) const
-{
-  return RealIsEmpty ();
-}
-EventId
-Scheduler::PeekNext (void) const
-{
-  NS_ASSERT (!RealIsEmpty ());
-  return RealPeekNext ();
-}
-void 
-Scheduler::RemoveNext (void)
-{
-  NS_ASSERT (!RealIsEmpty ());
-  return RealRemoveNext ();
-}
-bool
-Scheduler::Remove (EventId id)
-{
-  NS_ASSERT (!RealIsEmpty ());
-  return RealRemove (id);
-}
-
 }; // namespace ns3
--- a/src/simulator/scheduler.h	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/scheduler.h	Wed Aug 01 19:52:59 2007 +0200
@@ -27,21 +27,18 @@
 
 namespace ns3 {
 
-class EventImpl;
-
 /**
  * \brief Maintain the event list
  *
  * This base class specifies the interface used to maintain the 
  * event list. If you want to provide a new event list scheduler, 
  * you need to create a subclass of this base class and implement 
- * all the private pure virtual methods defined here. Namely:
- *   - ns3::Scheduler::realInsert
- *   - ns3::Scheduler::realIsEmpty
- *   - ns3::Scheduler::realPeekNext
- *   - ns3::Scheduler::realPeekNextKey
- *   - ns3::Scheduler::realRemoveNext
- *   - ns3::Scheduler::realRemove
+ * all the pure virtual methods defined here. Namely:
+ *   - ns3::Scheduler::Insert
+ *   - ns3::Scheduler::IsEmpty
+ *   - ns3::Scheduler::PeekNext
+ *   - ns3::Scheduler::RemoveNext
+ *   - ns3::Scheduler::Remove
  *
  * If you need to provide a new event list scheduler without
  * editing the main simulator class, you need to also implement
@@ -60,13 +57,6 @@
 
   virtual ~Scheduler () = 0;
 
-  void Insert (EventId id);
-  bool IsEmpty (void) const;
-  EventId PeekNext (void) const;
-  void RemoveNext (void);
-  bool Remove (EventId);
-
-private:
   /**
    * \param event event to store in the event list
    * \param key timecode associated to this new event
@@ -74,23 +64,23 @@
    *
    * This method takes ownership of the event pointer.
    */
-  virtual void RealInsert (EventId id) = 0;
+  virtual void Insert (const EventId &id) = 0;
   /**
    * \returns true if the event list is empty and false otherwise.
    */
-  virtual bool RealIsEmpty (void) const = 0;
+  virtual bool IsEmpty (void) const = 0;
   /**
    * \returns a pointer to the next earliest event. The caller
    *      takes ownership of the returned pointer.
    *
    * This method cannot be invoked if the list is empty.
    */
-  virtual EventId RealPeekNext (void) const = 0;
+  virtual EventId PeekNext (void) const = 0;
   /**
    * This method cannot be invoked if the list is empty.
    * Remove the next earliest event from the event list.
    */
-  virtual void RealRemoveNext (void) = 0;
+  virtual EventId RemoveNext (void) = 0;
   /**
    * \param id the id of the event to remove
    * \param key the timecode of the event removed
@@ -99,7 +89,7 @@
    *
    * This methods cannot be invoked if the list is empty.
    */
-  virtual bool RealRemove (EventId id) = 0;
+  virtual bool Remove (const EventId &id) = 0;
 };
 
 }; // namespace ns3
--- a/src/simulator/simulator.cc	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/simulator.cc	Wed Aug 01 19:52:59 2007 +0200
@@ -23,6 +23,7 @@
 #include "scheduler.h"
 #include "event-impl.h"
 
+#include "ns3/ptr.h"
 #include "ns3/assert.h"
 #include "ns3/default-value.h"
 
@@ -61,12 +62,12 @@
   Time Next (void) const;
   void Stop (void);
   void StopAt (Time const &time);
-  EventId Schedule (Time const &time, EventImpl *event);
-  EventId ScheduleNow (EventImpl *event);
-  EventId ScheduleDestroy (EventImpl *event);
-  void Remove (EventId ev);
-  void Cancel (EventId &ev);
-  bool IsExpired (EventId ev);
+  EventId Schedule (Time const &time, const Ptr<EventImpl> &event);
+  EventId ScheduleNow (const Ptr<EventImpl> &event);
+  EventId ScheduleDestroy (const Ptr<EventImpl> &event);
+  void Remove (const EventId &ev);
+  void Cancel (const EventId &ev);
+  bool IsExpired (const EventId &ev);
   void Run (void);
   Time Now (void) const;
 
@@ -114,13 +115,12 @@
 {
   while (!m_destroyEvents.empty ()) 
     {
-      EventImpl *ev = m_destroyEvents.front ().GetEventImpl ();
+      Ptr<EventImpl> ev = m_destroyEvents.front ().PeekEventImpl ();
       m_destroyEvents.pop_front ();
       TRACE ("handle destroy " << ev);
       if (!ev->IsCancelled ())
         {
           ev->Invoke ();
-          delete ev;
         }
     }
   delete m_events;
@@ -138,8 +138,7 @@
 void
 SimulatorPrivate::ProcessOneEvent (void)
 {
-  EventId next = m_events->PeekNext ();
-  m_events->RemoveNext ();
+  EventId next = m_events->RemoveNext ();
 
   NS_ASSERT (next.GetTs () >= m_currentTs);
   --m_unscheduledEvents;
@@ -151,9 +150,8 @@
     {
       m_log << "e "<<next.GetUid () << " " << next.GetTs () << std::endl;
     }
-  EventImpl *event = next.GetEventImpl ();
+  EventImpl *event = next.PeekEventImpl ();
   event->Invoke ();
-  delete event;
 }
 
 bool 
@@ -204,7 +202,7 @@
   m_stopAt = at.GetTimeStep ();
 }
 EventId
-SimulatorPrivate::Schedule (Time const &time, EventImpl *event)
+SimulatorPrivate::Schedule (Time const &time, const Ptr<EventImpl> &event)
 {
   NS_ASSERT (time.IsPositive ());
   NS_ASSERT (time >= TimeStep (m_currentTs));
@@ -221,7 +219,7 @@
   return id;
 }
 EventId
-SimulatorPrivate::ScheduleNow (EventImpl *event)
+SimulatorPrivate::ScheduleNow (const Ptr<EventImpl> &event)
 {
   EventId id (event, m_currentTs, m_uid);
   if (m_logEnable) 
@@ -235,7 +233,7 @@
   return id;
 }
 EventId
-SimulatorPrivate::ScheduleDestroy (EventImpl *event)
+SimulatorPrivate::ScheduleDestroy (const Ptr<EventImpl> &event)
 {
   EventId id (event, m_currentTs, 2);
   m_destroyEvents.push_back (id);
@@ -255,7 +253,7 @@
 }
 
 void
-SimulatorPrivate::Remove (EventId ev)
+SimulatorPrivate::Remove (const EventId &ev)
 {
   if (ev.GetUid () == 2)
     {
@@ -275,7 +273,7 @@
       return;
     }
   m_events->Remove (ev);
-  delete ev.GetEventImpl ();
+  Cancel (ev);
 
   if (m_logEnable) 
     {
@@ -286,13 +284,16 @@
 }
 
 void
-SimulatorPrivate::Cancel (EventId &id)
+SimulatorPrivate::Cancel (const EventId &id)
 {
-  id.Cancel ();
+  if (!IsExpired (id))
+    {
+      id.PeekEventImpl ()->Cancel ();
+    }
 }
 
 bool
-SimulatorPrivate::IsExpired (const EventId ev)
+SimulatorPrivate::IsExpired (const EventId &ev)
 {
   if (ev.GetUid () == 2)
     {
@@ -306,11 +307,11 @@
          }
       return true;
     }
-  if (ev.GetEventImpl () == 0 ||
+  if (ev.PeekEventImpl () == 0 ||
       ev.GetTs () < m_currentTs ||
       (ev.GetTs () == m_currentTs &&
        ev.GetUid () <= m_currentUid) ||
-      ev.GetEventImpl ()->IsCancelled ()) 
+      ev.PeekEventImpl ()->IsCancelled ()) 
     {
       return true;
     }
@@ -411,39 +412,39 @@
   return GetPriv ()->Now ();
 }
 
-EventImpl *
+Ptr<EventImpl>
 Simulator::MakeEvent (void (*f) (void))
 {
     // zero arg version
   class EventFunctionImpl0 : public EventImpl {
   public:
-  	typedef void (*F)(void);
+    typedef void (*F)(void);
       
-  	EventFunctionImpl0 (F function) 
-  		: m_function (function)
-  	{}
-  	virtual ~EventFunctionImpl0 () {}
+    EventFunctionImpl0 (F function) 
+      : m_function (function)
+    {}
+    virtual ~EventFunctionImpl0 () {}
   protected:
-  	virtual void Notify (void) { 
-  		(*m_function) (); 
-      }
+    virtual void Notify (void) { 
+      (*m_function) (); 
+    }
   private:
   	F m_function;
   } *ev = new EventFunctionImpl0 (f);
-  return ev;
+  return Ptr<EventImpl> (ev, false);
 }
 EventId
-Simulator::Schedule (Time const &time, EventImpl *ev)
+Simulator::Schedule (Time const &time, const Ptr<EventImpl> &ev)
 {
   return GetPriv ()->Schedule (Now () + time, ev);
 }
 EventId
-Simulator::ScheduleNow (EventImpl *ev)
+Simulator::ScheduleNow (const Ptr<EventImpl> &ev)
 {
   return GetPriv ()->ScheduleNow (ev);
 }
 EventId
-Simulator::ScheduleDestroy (EventImpl *ev)
+Simulator::ScheduleDestroy (const Ptr<EventImpl> &ev)
 {
   return GetPriv ()->ScheduleDestroy (ev);
 }  
@@ -465,18 +466,18 @@
 
 
 void
-Simulator::Remove (EventId ev)
+Simulator::Remove (const EventId &ev)
 {
   return GetPriv ()->Remove (ev);
 }
 
 void
-Simulator::Cancel (EventId &ev)
+Simulator::Cancel (const EventId &ev)
 {
   return GetPriv ()->Cancel (ev);
 }
 bool 
-Simulator::IsExpired (EventId id)
+Simulator::IsExpired (const EventId &id)
 {
   return GetPriv ()->IsExpired (id);
 }
@@ -884,6 +885,22 @@
       ok = false;
     }
 
+  EventId anId = Simulator::ScheduleNow (&foo0);
+  EventId anotherId = anId;
+  if (anId.IsExpired () || anotherId.IsExpired ())
+    {
+      ok = false;
+    }
+  Simulator::Remove (anId);
+  if (!anId.IsExpired () || !anotherId.IsExpired ())
+    {
+      ok = false;
+    }
+
+  Simulator::Run ();
+  Simulator::Destroy ();
+  
+
   return ok;
 }
 
--- a/src/simulator/simulator.h	Wed Aug 01 09:38:55 2007 +0200
+++ b/src/simulator/simulator.h	Wed Aug 01 19:52:59 2007 +0200
@@ -534,7 +534,7 @@
    *
    * @param id the event to remove from the list of scheduled events.
    */
-  static void Remove (EventId id);
+  static void Remove (const EventId &id);
   /**
    * Set the cancel bit on this event: the event's associated function
    * will not be invoked when it expires. 
@@ -547,7 +547,7 @@
    * 
    * @param id the event to cancel
    */
-  static void Cancel (EventId &id);
+  static void Cancel (const EventId &id);
   /**
    * This method has O(1) complexity.
    * Note that it is not possible to test for the expiration of
@@ -560,7 +560,7 @@
    * @param id the event to test for expiration
    * @returns true if the event has expired, false otherwise.
    */
-  static bool IsExpired (const EventId id);
+  static bool IsExpired (const EventId &id);
   /**
    * Return the "current simulation time".
    */
@@ -570,49 +570,49 @@
   ~Simulator ();
 
   template <typename T, typename OBJ>
-  static EventImpl *MakeEvent (void (T::*mem_ptr) (void), OBJ obj);
+  static Ptr<EventImpl> MakeEvent (void (T::*mem_ptr) (void), OBJ obj);
   template <typename T, typename OBJ, 
             typename U1,
             typename T1>
-  static EventImpl *MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1);
+  static Ptr<EventImpl> MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1);
   template <typename T, typename OBJ, 
             typename U1, typename U2,
             typename T1, typename T2>
-  static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 a2);
+  static Ptr<EventImpl> MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 a2);
   template <typename T, typename OBJ, 
             typename U1, typename U2, typename U3,
             typename T1, typename T2, typename T3>
-  static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, T2 a2, T3 a3);
+  static Ptr<EventImpl> MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, T2 a2, T3 a3);
   template <typename T, typename OBJ, 
             typename U1, typename U2, typename U3, typename U4,
             typename T1, typename T2, typename T3, typename T4>
-  static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4);
+  static Ptr<EventImpl> MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4);
   template <typename T, typename OBJ, 
             typename U1, typename U2, typename U3, typename U4, typename U5,
             typename T1, typename T2, typename T3, typename T4, typename T5>
-  static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, 
+  static Ptr<EventImpl> MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, 
                                T1 a1, T2 a2, T3 a3, T4 a4, T5 a5);
-  static EventImpl *MakeEvent (void (*f) (void));
+  static Ptr<EventImpl> MakeEvent (void (*f) (void));
   template <typename U1, 
             typename T1>
-  static EventImpl *MakeEvent (void (*f) (U1), T1 a1);
+  static Ptr<EventImpl> MakeEvent (void (*f) (U1), T1 a1);
   template <typename U1, typename U2, 
             typename T1, typename T2>
-  static EventImpl *MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2);
+  static Ptr<EventImpl> MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2);
   template <typename U1, typename U2, typename U3,
             typename T1, typename T2, typename T3>
-  static EventImpl *MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3);
+  static Ptr<EventImpl> MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3);
   template <typename U1, typename U2, typename U3, typename U4,
             typename T1, typename T2, typename T3, typename T4>
-  static EventImpl *MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T4 a4);
+  static Ptr<EventImpl> MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T4 a4);
   template <typename U1, typename U2, typename U3, typename U4, typename U5,
             typename T1, typename T2, typename T3, typename T4, typename T5>
-  static EventImpl *MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5);
+  static Ptr<EventImpl> MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5);
 
   static SimulatorPrivate *GetPriv (void);
-  static EventId Schedule (Time const &time, EventImpl *event);
-  static EventId ScheduleDestroy (EventImpl *event);
-  static EventId ScheduleNow (EventImpl *event);
+  static EventId Schedule (Time const &time, const Ptr<EventImpl> &event);
+  static EventId ScheduleDestroy (const Ptr<EventImpl> &event);
+  static EventId ScheduleNow (const Ptr<EventImpl> &event);
   static SimulatorPrivate *m_priv;
 };
 
@@ -650,7 +650,7 @@
 };
 
 template <typename T, typename OBJ>
-EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (void), OBJ obj) 
+Ptr<EventImpl> Simulator::MakeEvent (void (T::*mem_ptr) (void), OBJ obj) 
 {
   // zero argument version
   class EventMemberImpl0 : public EventImpl {
@@ -667,15 +667,15 @@
     }
     OBJ m_obj;
     F m_function;
-  } *ev = new EventMemberImpl0 (obj, mem_ptr);
-  return ev;
+  } * ev = new EventMemberImpl0 (obj, mem_ptr);
+  return Ptr<EventImpl> (ev, false);
 }
 
 
 template <typename T, typename OBJ, 
           typename U1,
           typename T1>
-EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1)
+Ptr<EventImpl> Simulator::MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1)
 {
   // one argument version
   class EventMemberImpl1 : public EventImpl {
@@ -696,13 +696,13 @@
     F m_function;
     typename TypeTraits<T1>::ReferencedType m_a1;
   } *ev = new EventMemberImpl1 (obj, mem_ptr, a1);
-  return ev;
+  return Ptr<EventImpl> (ev, false);
 }
 
 template <typename T, typename OBJ, 
           typename U1, typename U2,
           typename T1, typename T2>
-EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 a2) 
+Ptr<EventImpl> Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 a2) 
 {
   // two argument version
   class EventMemberImpl2 : public EventImpl {
@@ -726,13 +726,13 @@
     typename TypeTraits<T1>::ReferencedType m_a1;
     typename TypeTraits<T2>::ReferencedType m_a2;
   } *ev = new EventMemberImpl2 (obj, mem_ptr, a1, a2);
-  return ev;
+  return Ptr<EventImpl> (ev, false);
 }
 
 template <typename T, typename OBJ, 
           typename U1, typename U2, typename U3,
           typename T1, typename T2, typename T3>
-EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, T2 a2, T3 a3) 
+Ptr<EventImpl> Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, T2 a2, T3 a3) 
 {
   // three argument version
   class EventMemberImpl3 : public EventImpl {
@@ -758,13 +758,13 @@
     typename TypeTraits<T2>::ReferencedType m_a2;
     typename TypeTraits<T3>::ReferencedType m_a3;
   } *ev = new EventMemberImpl3 (obj, mem_ptr, a1, a2, a3);
-  return ev;
+  return Ptr<EventImpl> (ev, false);
 }
 
 template <typename T, typename OBJ, 
           typename U1, typename U2, typename U3, typename U4,
           typename T1, typename T2, typename T3, typename T4>
-EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4) 
+Ptr<EventImpl> Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4) 
 {
   // four argument version
   class EventMemberImpl4 : public EventImpl {
@@ -792,13 +792,13 @@
     typename TypeTraits<T3>::ReferencedType m_a3;
     typename TypeTraits<T4>::ReferencedType m_a4;
   } *ev = new EventMemberImpl4 (obj, mem_ptr, a1, a2, a3, a4);
-  return ev;
+  return Ptr<EventImpl> (ev, false);
 }
 
 template <typename T, typename OBJ, 
           typename U1, typename U2, typename U3, typename U4, typename U5,
           typename T1, typename T2, typename T3, typename T4, typename T5>
-EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, 
+Ptr<EventImpl> Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, 
                                  T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) 
 {
   // five argument version
@@ -829,11 +829,11 @@
     typename TypeTraits<T4>::ReferencedType m_a4;
     typename TypeTraits<T5>::ReferencedType m_a5;
   } *ev = new EventMemberImpl5 (obj, mem_ptr, a1, a2, a3, a4, a5);
-  return ev;
+  return Ptr<EventImpl> (ev, false);
 }
 
 template <typename U1, typename T1>
-EventImpl *Simulator::MakeEvent (void (*f) (U1), T1 a1) 
+Ptr<EventImpl> Simulator::MakeEvent (void (*f) (U1), T1 a1) 
 {
   // one arg version
   class EventFunctionImpl1 : public EventImpl {
@@ -852,12 +852,12 @@
     }
     F m_function;
     typename TypeTraits<T1>::ReferencedType m_a1;
-  } *ev = new EventFunctionImpl1(f, a1);
-  return ev;
+  } *ev = new EventFunctionImpl1 (f, a1);
+  return Ptr<EventImpl> (ev, false);
 }
 
 template <typename U1, typename U2, typename T1, typename T2>
-EventImpl *Simulator::MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2) 
+Ptr<EventImpl> Simulator::MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2) 
 {
   // two arg version
   class EventFunctionImpl2 : public EventImpl {
@@ -879,12 +879,12 @@
     typename TypeTraits<T1>::ReferencedType m_a1;
     typename TypeTraits<T2>::ReferencedType m_a2;
   } *ev = new EventFunctionImpl2 (f, a1, a2);
-  return ev;
+  return Ptr<EventImpl> (ev, false);
 }
 
 template <typename U1, typename U2, typename U3,
           typename T1, typename T2, typename T3>
-EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3)
+Ptr<EventImpl> Simulator::MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3)
 {
   // three arg version
   class EventFunctionImpl3 : public EventImpl {
@@ -908,12 +908,12 @@
     typename TypeTraits<T2>::ReferencedType m_a2;
     typename TypeTraits<T3>::ReferencedType m_a3;
   } *ev = new EventFunctionImpl3 (f, a1, a2, a3);
-  return ev;
+  return Ptr<EventImpl> (ev, false);
 }
 
 template <typename U1, typename U2, typename U3, typename U4,
           typename T1, typename T2, typename T3, typename T4>
-EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T4 a4) 
+Ptr<EventImpl> Simulator::MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T4 a4) 
 {
   // four arg version
   class EventFunctionImpl4 : public EventImpl {
@@ -939,12 +939,12 @@
     typename TypeTraits<T3>::ReferencedType m_a3;
     typename TypeTraits<T4>::ReferencedType m_a4;
   } *ev = new EventFunctionImpl4 (f, a1, a2, a3, a4);
-  return ev;
+  return Ptr<EventImpl> (ev, false);
 }
 
 template <typename U1, typename U2, typename U3, typename U4, typename U5,
           typename T1, typename T2, typename T3, typename T4, typename T5>
-EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) 
+Ptr<EventImpl> Simulator::MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) 
 {
   // five arg version
   class EventFunctionImpl5 : public EventImpl {
@@ -972,7 +972,7 @@
     typename TypeTraits<T4>::ReferencedType m_a4;
     typename TypeTraits<T5>::ReferencedType m_a5;
   } *ev = new EventFunctionImpl5 (f, a1, a2, a3, a4, a5);
-  return ev; 
+  return Ptr<EventImpl> (ev, false);
 }
 
 template <typename T, typename OBJ>