iterate over aggregated objects dynamically.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed Apr 09 17:35:18 2008 -0700 (22 months ago)
changeset 2937fc048c358357
parent 2936 d108e5f0bd3e
child 2938 621b23d5be5b
iterate over aggregated objects dynamically.
src/core/object.cc
src/core/object.h
     1.1 --- a/src/core/object.cc	Wed Apr 09 17:28:19 2008 -0700
     1.2 +++ b/src/core/object.cc	Wed Apr 09 17:35:18 2008 -0700
     1.3 @@ -37,6 +37,43 @@
     1.4  
     1.5  NS_OBJECT_ENSURE_REGISTERED (Object);
     1.6  
     1.7 +Object::AggregateIterator::AggregateIterator ()
     1.8 +  : m_first (0),
     1.9 +    m_current (0)
    1.10 +{}
    1.11 +
    1.12 +bool 
    1.13 +Object::AggregateIterator::HasNext (void) const
    1.14 +{
    1.15 +  if (m_current == 0 && m_first != 0)
    1.16 +    {
    1.17 +      return true;
    1.18 +    }
    1.19 +  if (m_current != 0 && m_current->m_next != PeekPointer (m_first))
    1.20 +    {
    1.21 +      return true;
    1.22 +    }
    1.23 +  return false;
    1.24 +}
    1.25 +Ptr<const Object> 
    1.26 +Object::AggregateIterator::Next (void)
    1.27 +{
    1.28 +  if (m_current == 0)
    1.29 +    {
    1.30 +      m_current = m_first;
    1.31 +    }
    1.32 +  else
    1.33 +    {
    1.34 +      m_current = m_current->m_next;
    1.35 +    }
    1.36 +  return m_current;
    1.37 +}
    1.38 +Object::AggregateIterator::AggregateIterator (Ptr<const Object> first)
    1.39 +  : m_first (first),
    1.40 +    m_current (0)
    1.41 +{}
    1.42 +
    1.43 +
    1.44  TypeId 
    1.45  Object::GetInstanceTypeId (void) const
    1.46  {
    1.47 @@ -122,6 +159,12 @@
    1.48    NS_ASSERT (o->CheckLoose ());
    1.49  }
    1.50  
    1.51 +Object::AggregateIterator 
    1.52 +Object::GetAggregateIterator (void) const
    1.53 +{
    1.54 +  return AggregateIterator (this);
    1.55 +}
    1.56 +
    1.57  void 
    1.58  Object::SetTypeId (TypeId tid)
    1.59  {
     2.1 --- a/src/core/object.h	Wed Apr 09 17:28:19 2008 -0700
     2.2 +++ b/src/core/object.h	Wed Apr 09 17:35:18 2008 -0700
     2.3 @@ -47,6 +47,20 @@
     2.4  public:
     2.5    static TypeId GetTypeId (void);
     2.6  
     2.7 +  class AggregateIterator
     2.8 +  {
     2.9 +  public:
    2.10 +    AggregateIterator ();
    2.11 +
    2.12 +    bool HasNext (void) const;
    2.13 +    Ptr<const Object> Next (void);
    2.14 +  private:
    2.15 +    friend class Object;
    2.16 +    AggregateIterator (Ptr<const Object> first);
    2.17 +    Ptr<const Object> m_first;
    2.18 +    Ptr<const Object> m_current;
    2.19 +  };
    2.20 +
    2.21    Object ();
    2.22    virtual ~Object ();
    2.23  
    2.24 @@ -100,6 +114,8 @@
    2.25     */
    2.26    void AggregateObject (Ptr<Object> other);
    2.27  
    2.28 +  AggregateIterator GetAggregateIterator (void) const;
    2.29 +
    2.30  protected:
    2.31    /**
    2.32     * This method is called by Object::Dispose or by the object's 
    2.33 @@ -139,6 +155,7 @@
    2.34    friend Ptr<T> CopyObject (Ptr<T> object);
    2.35  
    2.36    friend class ObjectFactory;
    2.37 +  friend class AggregateIterator;
    2.38  
    2.39    Ptr<Object> DoGetObject (TypeId tid) const;
    2.40    bool Check (void) const;