diff --git a/src/core/ptr.cc b/src/core/ptr.cc index 10e63c606..cee3f59e2 100644 --- a/src/core/ptr.cc +++ b/src/core/ptr.cc @@ -28,9 +28,14 @@ namespace ns3 { +template +void Foo (void) {} + + class NoCount : public Object { public: + NoCount (void (*fn) (void)); NoCount (Callback cb); ~NoCount (); void Nothing (void) const; @@ -292,12 +297,22 @@ PtrTest::RunTests (void) callback (); } + #if 0 // as expected, fails compilation. { Ptr p = Create (cb); Callback callback = MakeCallback (&NoCount::Nothing, p); } + // local types are not allowed as arguments to a template. + { + class B + { + public: + B () {} + }; + Foo (); + } #endif diff --git a/src/core/ptr.h b/src/core/ptr.h index f5a52292e..f2436dd63 100644 --- a/src/core/ptr.h +++ b/src/core/ptr.h @@ -65,7 +65,7 @@ private: template friend U *PeekPointer (const Ptr &p); - void Acquire (void) const; + inline void Acquire (void) const; public: /** * Create an empty smart pointer @@ -81,7 +81,16 @@ public: * 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 @@ -378,6 +387,16 @@ Ptr::Ptr (T *ptr) Acquire (); } +template +Ptr::Ptr (T *ptr, bool ref) + : m_ptr (ptr) +{ + if (ref) + { + Acquire (); + } +} + template Ptr::Ptr (Ptr const&o) : m_ptr (PeekPointer (o)) diff --git a/src/simulator/event-id.cc b/src/simulator/event-id.cc index 1c2915037..ca3766503 100644 --- a/src/simulator/event-id.cc +++ b/src/simulator/event-id.cc @@ -30,7 +30,7 @@ EventId::EventId () m_uid (0) {} -EventId::EventId (EventImpl *impl, uint64_t ts, uint32_t uid) +EventId::EventId (const Ptr &impl, uint64_t ts, uint32_t uid) : m_eventImpl (impl), m_ts (ts), m_uid (uid) @@ -38,11 +38,7 @@ EventId::EventId (EventImpl *impl, uint64_t ts, uint32_t uid) void EventId::Cancel (void) { - if (!IsExpired ()) - { - m_eventImpl->Cancel (); - m_eventImpl = 0; - } + Simulator::Cancel (*this); } bool EventId::IsExpired (void) const @@ -55,9 +51,9 @@ EventId::IsRunning (void) const return !IsExpired (); } EventImpl * -EventId::GetEventImpl (void) const +EventId::PeekEventImpl (void) const { - return m_eventImpl; + return PeekPointer (m_eventImpl); } uint64_t EventId::GetTs (void) const diff --git a/src/simulator/event-id.h b/src/simulator/event-id.h index 52371faa1..efa74d930 100644 --- a/src/simulator/event-id.h +++ b/src/simulator/event-id.h @@ -22,6 +22,8 @@ #define EVENT_ID_H #include +#include "ns3/ptr.h" +#include "event-impl.h" namespace ns3 { @@ -33,7 +35,7 @@ class EventImpl; class EventId { public: EventId (); - EventId (EventImpl *impl, uint64_t ts, uint32_t uid); + EventId (const Ptr &impl, uint64_t ts, uint32_t uid); /** * This method is syntactic sugar for the ns3::Simulator::cancel * method. @@ -51,12 +53,12 @@ public: * 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 m_eventImpl; uint64_t m_ts; uint32_t m_uid; }; diff --git a/src/simulator/event-impl.cc b/src/simulator/event-impl.cc index fb6c7b1ad..9c89bb52f 100644 --- a/src/simulator/event-impl.cc +++ b/src/simulator/event-impl.cc @@ -29,7 +29,8 @@ EventImpl::~EventImpl () {} EventImpl::EventImpl () - : m_cancel (false) + : m_cancel (false), + m_count (1) {} void EventImpl::Invoke (void) diff --git a/src/simulator/event-impl.h b/src/simulator/event-impl.h index fd7d54e2d..51ac1e7a1 100644 --- a/src/simulator/event-impl.h +++ b/src/simulator/event-impl.h @@ -28,6 +28,8 @@ namespace ns3 { 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 @@ protected: 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 */ diff --git a/src/simulator/scheduler-heap.cc b/src/simulator/scheduler-heap.cc index 0339dfed1..f924c70f4 100644 --- a/src/simulator/scheduler-heap.cc +++ b/src/simulator/scheduler-heap.cc @@ -170,7 +170,7 @@ SchedulerHeap::Smallest (uint32_t a, uint32_t b) const } bool -SchedulerHeap::RealIsEmpty (void) const +SchedulerHeap::IsEmpty (void) const { return (m_heap.size () == 1)?true:false; } @@ -223,9 +223,11 @@ SchedulerHeap::TopDown (uint32_t start) 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 @@ SchedulerHeap::RealInsert (EventId id) } EventId -SchedulerHeap::RealPeekNext (void) const +SchedulerHeap::PeekNext (void) const { std::pair 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 next = m_heap[Root ()]; Exch (Root (), Last ()); m_heap.pop_back (); TopDown (Root ()); + return EventId (Ptr (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 next = m_heap[i]; + // release single ref + next.first->Unref (); Exch (i, Last ()); m_heap.pop_back (); TopDown (i); diff --git a/src/simulator/scheduler-heap.h b/src/simulator/scheduler-heap.h index c57fe1368..fbc6e1ca1 100644 --- a/src/simulator/scheduler-heap.h +++ b/src/simulator/scheduler-heap.h @@ -35,13 +35,13 @@ public: SchedulerHeap (); virtual ~SchedulerHeap (); -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); + 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: typedef std::vector > BinaryHeap; inline uint32_t Parent (uint32_t id) const; diff --git a/src/simulator/scheduler-list.cc b/src/simulator/scheduler-list.cc index ba3960458..c3de7891a 100644 --- a/src/simulator/scheduler-list.cc +++ b/src/simulator/scheduler-list.cc @@ -67,10 +67,12 @@ SchedulerList::IsLower (Scheduler::EventKey const*a, Scheduler::EventKey const*b } 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 @@ SchedulerList::RealInsert (EventId id) 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 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 next = m_events.front (); m_events.pop_front (); + return EventId (Ptr (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; } diff --git a/src/simulator/scheduler-list.h b/src/simulator/scheduler-list.h index cbce5e68f..306d19270 100644 --- a/src/simulator/scheduler-list.h +++ b/src/simulator/scheduler-list.h @@ -37,13 +37,13 @@ class SchedulerList : public Scheduler { SchedulerList (); virtual ~SchedulerList (); - 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); + 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: inline bool IsLower (Scheduler::EventKey const*a, Scheduler::EventKey const*b) const; typedef std::list > Events; diff --git a/src/simulator/scheduler-map.cc b/src/simulator/scheduler-map.cc index 59be61ca7..f413ed830 100644 --- a/src/simulator/scheduler-map.cc +++ b/src/simulator/scheduler-map.cc @@ -88,9 +88,11 @@ SchedulerMap::EventKeyCompare::operator () (struct EventKey const&a, struct Even 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 @@ SchedulerMap::RealInsert (EventId id) } 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 (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; } diff --git a/src/simulator/scheduler-map.h b/src/simulator/scheduler-map.h index b8fb2cfa5..dbbc7fdbd 100644 --- a/src/simulator/scheduler-map.h +++ b/src/simulator/scheduler-map.h @@ -36,12 +36,12 @@ public: 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: diff --git a/src/simulator/scheduler.cc b/src/simulator/scheduler.cc index f8190bb39..4ba1d01b6 100644 --- a/src/simulator/scheduler.cc +++ b/src/simulator/scheduler.cc @@ -27,33 +27,4 @@ namespace ns3 { 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 diff --git a/src/simulator/scheduler.h b/src/simulator/scheduler.h index 0df7a77df..33917a870 100644 --- a/src/simulator/scheduler.h +++ b/src/simulator/scheduler.h @@ -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 @@ class Scheduler { 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 @@ private: * * 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 @@ private: * * 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 diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index c133cb5a4..8190aeb79 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -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 @@ public: 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 &event); + EventId ScheduleNow (const Ptr &event); + EventId ScheduleDestroy (const Ptr &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 @@ SimulatorPrivate::~SimulatorPrivate () { while (!m_destroyEvents.empty ()) { - EventImpl *ev = m_destroyEvents.front ().GetEventImpl (); + Ptr 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 @@ SimulatorPrivate::EnableLogTo (char const *filename) 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 @@ SimulatorPrivate::ProcessOneEvent (void) { m_log << "e "<Invoke (); - delete event; } bool @@ -204,7 +202,7 @@ SimulatorPrivate::StopAt (Time const &at) m_stopAt = at.GetTimeStep (); } EventId -SimulatorPrivate::Schedule (Time const &time, EventImpl *event) +SimulatorPrivate::Schedule (Time const &time, const Ptr &event) { NS_ASSERT (time.IsPositive ()); NS_ASSERT (time >= TimeStep (m_currentTs)); @@ -221,7 +219,7 @@ SimulatorPrivate::Schedule (Time const &time, EventImpl *event) return id; } EventId -SimulatorPrivate::ScheduleNow (EventImpl *event) +SimulatorPrivate::ScheduleNow (const Ptr &event) { EventId id (event, m_currentTs, m_uid); if (m_logEnable) @@ -235,7 +233,7 @@ SimulatorPrivate::ScheduleNow (EventImpl *event) return id; } EventId -SimulatorPrivate::ScheduleDestroy (EventImpl *event) +SimulatorPrivate::ScheduleDestroy (const Ptr &event) { EventId id (event, m_currentTs, 2); m_destroyEvents.push_back (id); @@ -255,7 +253,7 @@ SimulatorPrivate::Now (void) const } void -SimulatorPrivate::Remove (EventId ev) +SimulatorPrivate::Remove (const EventId &ev) { if (ev.GetUid () == 2) { @@ -275,7 +273,7 @@ SimulatorPrivate::Remove (EventId ev) return; } m_events->Remove (ev); - delete ev.GetEventImpl (); + Cancel (ev); if (m_logEnable) { @@ -286,13 +284,16 @@ SimulatorPrivate::Remove (EventId ev) } 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 @@ SimulatorPrivate::IsExpired (const EventId ev) } 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 @@ Simulator::Now (void) return GetPriv ()->Now (); } -EventImpl * +Ptr 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 (ev, false); } EventId -Simulator::Schedule (Time const &time, EventImpl *ev) +Simulator::Schedule (Time const &time, const Ptr &ev) { return GetPriv ()->Schedule (Now () + time, ev); } EventId -Simulator::ScheduleNow (EventImpl *ev) +Simulator::ScheduleNow (const Ptr &ev) { return GetPriv ()->ScheduleNow (ev); } EventId -Simulator::ScheduleDestroy (EventImpl *ev) +Simulator::ScheduleDestroy (const Ptr &ev) { return GetPriv ()->ScheduleDestroy (ev); } @@ -465,18 +466,18 @@ Simulator::ScheduleDestroy (void (*f) (void)) 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 @@ SimulatorTests::RunTests (void) 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; } diff --git a/src/simulator/simulator.h b/src/simulator/simulator.h index a7eea7600..9c9cd201e 100644 --- a/src/simulator/simulator.h +++ b/src/simulator/simulator.h @@ -534,7 +534,7 @@ public: * * @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 @@ public: * * @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 @@ public: * @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 @@ private: ~Simulator (); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (void), OBJ obj); + static Ptr MakeEvent (void (T::*mem_ptr) (void), OBJ obj); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1); + static Ptr MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 a2); + static Ptr MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 a2); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, T2 a2, T3 a3); + static Ptr MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, T2 a2, T3 a3); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4); + static Ptr MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, + static Ptr 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 MakeEvent (void (*f) (void)); template - static EventImpl *MakeEvent (void (*f) (U1), T1 a1); + static Ptr MakeEvent (void (*f) (U1), T1 a1); template - static EventImpl *MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2); + static Ptr MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2); template - static EventImpl *MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3); + static Ptr MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3); template - static EventImpl *MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T4 a4); + static Ptr MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T4 a4); template - static EventImpl *MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); + static Ptr 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 &event); + static EventId ScheduleDestroy (const Ptr &event); + static EventId ScheduleNow (const Ptr &event); static SimulatorPrivate *m_priv; }; @@ -650,7 +650,7 @@ struct EventMemberImplObjTraits }; template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (void), OBJ obj) +Ptr Simulator::MakeEvent (void (T::*mem_ptr) (void), OBJ obj) { // zero argument version class EventMemberImpl0 : public EventImpl { @@ -667,15 +667,15 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (void), OBJ obj) } OBJ m_obj; F m_function; - } *ev = new EventMemberImpl0 (obj, mem_ptr); - return ev; + } * ev = new EventMemberImpl0 (obj, mem_ptr); + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1) +Ptr Simulator::MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1) { // one argument version class EventMemberImpl1 : public EventImpl { @@ -696,13 +696,13 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1) F m_function; typename TypeTraits::ReferencedType m_a1; } *ev = new EventMemberImpl1 (obj, mem_ptr, a1); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 a2) +Ptr 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 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 typename TypeTraits::ReferencedType m_a1; typename TypeTraits::ReferencedType m_a2; } *ev = new EventMemberImpl2 (obj, mem_ptr, a1, a2); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, T2 a2, T3 a3) +Ptr 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 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, typename TypeTraits::ReferencedType m_a2; typename TypeTraits::ReferencedType m_a3; } *ev = new EventMemberImpl3 (obj, mem_ptr, a1, a2, a3); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4) +Ptr 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 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a typename TypeTraits::ReferencedType m_a3; typename TypeTraits::ReferencedType m_a4; } *ev = new EventMemberImpl4 (obj, mem_ptr, a1, a2, a3, a4); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, +Ptr 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 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, typename TypeTraits::ReferencedType m_a4; typename TypeTraits::ReferencedType m_a5; } *ev = new EventMemberImpl5 (obj, mem_ptr, a1, a2, a3, a4, a5); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (*f) (U1), T1 a1) +Ptr Simulator::MakeEvent (void (*f) (U1), T1 a1) { // one arg version class EventFunctionImpl1 : public EventImpl { @@ -852,12 +852,12 @@ EventImpl *Simulator::MakeEvent (void (*f) (U1), T1 a1) } F m_function; typename TypeTraits::ReferencedType m_a1; - } *ev = new EventFunctionImpl1(f, a1); - return ev; + } *ev = new EventFunctionImpl1 (f, a1); + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2) +Ptr Simulator::MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2) { // two arg version class EventFunctionImpl2 : public EventImpl { @@ -879,12 +879,12 @@ EventImpl *Simulator::MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2) typename TypeTraits::ReferencedType m_a1; typename TypeTraits::ReferencedType m_a2; } *ev = new EventFunctionImpl2 (f, a1, a2); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3) +Ptr Simulator::MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3) { // three arg version class EventFunctionImpl3 : public EventImpl { @@ -908,12 +908,12 @@ EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3) typename TypeTraits::ReferencedType m_a2; typename TypeTraits::ReferencedType m_a3; } *ev = new EventFunctionImpl3 (f, a1, a2, a3); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T4 a4) +Ptr 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 @@ EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T typename TypeTraits::ReferencedType m_a3; typename TypeTraits::ReferencedType m_a4; } *ev = new EventFunctionImpl4 (f, a1, a2, a3, a4); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) +Ptr 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 @@ EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3 typename TypeTraits::ReferencedType m_a4; typename TypeTraits::ReferencedType m_a5; } *ev = new EventFunctionImpl5 (f, a1, a2, a3, a4, a5); - return ev; + return Ptr (ev, false); } template