From c62cf3c82d0d0a1f94a2eae0dffbecc84af6e5ae Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 12 Aug 2007 16:25:19 +0200 Subject: [PATCH] rewrite the ArrayTraceResolver to use iterators --- src/core/array-trace-resolver.h | 169 +++++++++++++++++++++----------- 1 file changed, 114 insertions(+), 55 deletions(-) diff --git a/src/core/array-trace-resolver.h b/src/core/array-trace-resolver.h index 547ff0009..3ba7147c5 100644 --- a/src/core/array-trace-resolver.h +++ b/src/core/array-trace-resolver.h @@ -25,85 +25,144 @@ #include #include "callback.h" #include "trace-resolver.h" +#include "object.h" namespace ns3 { /** * \brief a helper class to offer trace resolution for an array of objects. * \ingroup lowleveltracing + * + * \class ArrayTraceResolver + * + * An ArrayTraceResolver is a resolver which can match any input integer + * against an element in an array. The array is accessed using a + * pair iterators. Each element of the array is expected + * to be a subclass of the Object base class. + * + * When the Connect method is called, this trace resolver will + * automatically store in the TraceContext of each resolved object + * its index through an object of type INDEX specified during the + * instanciation of the ArrayTraceResolver template. */ -template +template class ArrayTraceResolver : public TraceResolver { public: - /** - * \param getSize callback which returns dynamically the size of underlying array - * \param get callback which returns any element in the underlying array - * - * Construct a trace resolver which can match any input integer - * against an element in an array. The array is accessed using a - * pair of callbacks. It is the responsability of the user to - * provide two such callbacks whose job is to adapt the array - * API to the resolver needs. Each element of the array is expected - * to provide a method named CreateTraceResolver which takes as - * only argument a reference to a const TraceContext and returns - * a pointer to a TraceResolver. i.e. the signature is: - * Ptr (*) (void) - */ - ArrayTraceResolver (Callback getSize, - Callback get); + ArrayTraceResolver (); + ~ArrayTraceResolver (); + /** + * \param begin an iterator which points to the start of the array. + * \param end an iterator which points to the end of the array. + */ + template + void SetIterators (T begin, T end); + + // inherited from TraceResolver virtual void Connect (std::string path, CallbackBase const &cb, const TraceContext &context); virtual void Disconnect (std::string path, CallbackBase const &cb); + private: - Callback m_getSize; - Callback m_get; + class IteratorBase + { + public: + virtual ~IteratorBase () {} + virtual void Next (void) = 0; + virtual bool HasNext (void) = 0; + virtual Ptr Get (void) = 0; + virtual void Rewind (void) = 0; + }; + IteratorBase *m_iter; }; }//namespace ns3 + +// implementation namespace ns3 { -template -ArrayTraceResolver::ArrayTraceResolver (Callback getSize, - Callback get) - : m_getSize (getSize), - m_get (get) +template +ArrayTraceResolver::ArrayTraceResolver () + : m_iter (0) {} -template -void -ArrayTraceResolver::Connect (std::string path, CallbackBase const &cb, const TraceContext &context) +template +ArrayTraceResolver::~ArrayTraceResolver () { - std::string id = GetElement (path); - std::string subpath = GetSubpath (path); - if (id == "*") - { - for (uint32_t i = 0; i < m_getSize (); i++) - { - TraceContext tmp = context; - INDEX index = i; - tmp.Add (index); - Ptr resolver = m_get (i)->CreateTraceResolver (); - resolver->Connect (subpath, cb, tmp); - } - } -} -template -void -ArrayTraceResolver::Disconnect (std::string path, CallbackBase const &cb) -{ - std::string id = GetElement (path); - std::string subpath = GetSubpath (path); - if (id == "*") - { - for (uint32_t i = 0; i < m_getSize (); i++) - { - Ptr resolver = m_get (i)->CreateTraceResolver (); - resolver->Disconnect (subpath, cb); - } - } + delete m_iter; } +template +template +void +ArrayTraceResolver::SetIterators (T begin, T end) +{ + class Iterator : public IteratorBase + { + public: + Iterator (T begin, T end) + : m_begin (begin), m_end (end), m_cur (begin) + {} + virtual void Next (void) + {m_cur++;} + virtual bool HasNext (void) + {return m_cur != m_end;} + virtual Ptr Get (void) + {return *m_cur;} + virtual void Rewind (void) + {m_cur = m_begin;} + private: + T m_begin; + T m_end; + T m_cur; + }; + delete m_iter; + m_iter = new Iterator (begin, end); +} + +template +void +ArrayTraceResolver::Connect (std::string path, CallbackBase const &cb, const TraceContext &context) +{ + if (path == "") + { + return; + } + std::string id = GetElement (path); + std::string subpath = GetSubpath (path); + if (id == "*") + { + uint32_t j = 0; + for (m_iter->Rewind (); m_iter->HasNext (); m_iter->Next ()) + { + TraceContext tmp = context; + INDEX index = j; + tmp.Add (index); + Ptr obj = m_iter->Get (); + obj->TraceConnect (subpath, cb, tmp); + j++; + } + } +} +template +void +ArrayTraceResolver::Disconnect (std::string path, CallbackBase const &cb) +{ + if (path == "") + { + return; + } + std::string id = GetElement (path); + std::string subpath = GetSubpath (path); + if (id == "*") + { + for (m_iter->Rewind (); m_iter->HasNext (); m_iter->Next ()) + { + Ptr obj = m_iter->Get (); + obj->TraceDisconnect (subpath, cb); + } + } +} }//namespace ns3