PLearn 0.1
Public Member Functions | Protected Attributes
PLearn::ObjectOptionsIterator Class Reference

An ObjectOptionsIterator iterates across all accessible sub-objects of a given PLearn::Object. More...

#include <ObjectGraphIterator.h>

Collaboration diagram for PLearn::ObjectOptionsIterator:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ObjectOptionsIterator ()
 Default constructor: invalid iterator.
 ObjectOptionsIterator (const Object *root, bool ignore_nontraversable=false, bool skip_nulls=true)
 Iterate along all first-level sub-objects of the given object.
bool operator== (const ObjectOptionsIterator &rhs) const
 Equality testing.
bool operator!= (const ObjectOptionsIterator &rhs) const
 Inequality testing.
const Objectoperator* () const
 Dereference: access the current sub-object.
string getCurrentOptionName () const
 Return the name of the current option such that code of the following form always succeeds:
OptionBase::flag_t getCurrentOptionFlags () const
 Return the flags associated with the current option (e.g.
const ObjectOptionsIteratoroperator++ ()
ObjectOptionsIterator operator++ (int)
 Post-increment: implemented in terms of pre-increment.

Protected Attributes

bool m_invalid
 If true: invalid state.
bool m_ignore_nontraversable
 If true: traverse nontraversable.
bool m_skip_nulls
 If true: never return null ptrs.
const Objectm_object
 Object we are pointing to.
const OptionListm_options
 OptionList of that object.
int m_cur_option
 Which option we're at.
int m_cur_index
 If cur_option is indexable, current index within option.
int m_max_index
 If cur_option is indexable, maximum possible inxex+1.

Detailed Description

An ObjectOptionsIterator iterates across all accessible sub-objects of a given PLearn::Object.

The iteration is carried out by going through the list of options within the object, and traversing vectors (and other indexable options, such as maps, eventually) as appropriate.

The iterator is a model of the STL ForwardIterator. Default-constructing this object makes an "invalid" iterator which is always considered the end.

This class skips options that are marked with the "nontraversable" flag.

Definition at line 78 of file ObjectGraphIterator.h.


Constructor & Destructor Documentation

PLearn::ObjectOptionsIterator::ObjectOptionsIterator ( )

Default constructor: invalid iterator.

Definition at line 60 of file ObjectGraphIterator.cc.

PLearn::ObjectOptionsIterator::ObjectOptionsIterator ( const Object root,
bool  ignore_nontraversable = false,
bool  skip_nulls = true 
)

Iterate along all first-level sub-objects of the given object.

Parameters:
rootObject on which to iterate
ignore_nontraversableIf true, even the options with the "nontraversable" flag are traversed
skip_nullsIf true, null sub-objects are skipped (default); in some instances, it's necesary to iterate along ALL options, including null objects, whereupon you specify false.

Definition at line 72 of file ObjectGraphIterator.cc.

References PLearn::Object::getOptionList(), m_cur_option, m_invalid, m_object, m_options, and PLASSERT.

    : m_invalid(true),
      m_ignore_nontraversable(ignore_nontraversable),
      m_skip_nulls(skip_nulls),
      m_object(root),
      m_options(0),
      m_cur_option(),
      m_cur_index(),
      m_max_index()
{
    if (m_object) {
        m_options = &m_object->getOptionList();
        PLASSERT( m_options );

        if (m_options->size() > 0) {
            m_invalid = false;
            m_cur_option = -1;
            ++(*this);                           // find first valid option
        }
    }
}

Here is the call graph for this function:


Member Function Documentation

OptionBase::flag_t PLearn::ObjectOptionsIterator::getCurrentOptionFlags ( ) const

Return the flags associated with the current option (e.g.

OptionBase::buildoption, etc.)

Definition at line 131 of file ObjectGraphIterator.cc.

References m_cur_option, m_invalid, m_object, m_options, and PLASSERT.

{
    PLASSERT( !m_invalid && m_object && m_options );
    return (*m_options)[m_cur_option]->flags();
}
string PLearn::ObjectOptionsIterator::getCurrentOptionName ( ) const

Return the name of the current option such that code of the following form always succeeds:

  root->getOption(current_option_name)

Definition at line 121 of file ObjectGraphIterator.cc.

References m_cur_index, m_cur_option, m_invalid, m_max_index, m_object, m_options, PLASSERT, and PLearn::tostring().

{
    PLASSERT( !m_invalid && m_object && m_options );
    string optionname = (*m_options)[m_cur_option]->optionname();
    if (m_max_index > 0)
        optionname += '[' + tostring(m_cur_index) + ']';
    return optionname;
}

Here is the call graph for this function:

bool PLearn::ObjectOptionsIterator::operator!= ( const ObjectOptionsIterator rhs) const [inline]

Inequality testing.

Definition at line 108 of file ObjectGraphIterator.h.

    {
        return ! (*this == rhs);
    }
const Object * PLearn::ObjectOptionsIterator::operator* ( ) const

Dereference: access the current sub-object.

Note that if this object MAY be null if the option is indeed null.

Definition at line 109 of file ObjectGraphIterator.cc.

References m_cur_index, m_cur_option, m_invalid, m_max_index, m_object, m_options, and PLASSERT.

{
    PLASSERT( !m_invalid && m_object && m_options );

    if (m_max_index > 0)
        return (*m_options)[m_cur_option]->getIndexedObject(m_object,
                                                            m_cur_index);
    else
        return (*m_options)[m_cur_option]->getAsObject(m_object);
}
const ObjectOptionsIterator & PLearn::ObjectOptionsIterator::operator++ ( )

Definition at line 138 of file ObjectGraphIterator.cc.

References m_cur_index, m_cur_option, m_ignore_nontraversable, m_invalid, m_max_index, m_object, m_options, m_skip_nulls, n, PLearn::OptionBase::nontraversable, and PLASSERT.

{
    PLASSERT( !m_invalid && m_object && m_options );

    do {
        // Start by considering current iteration within an indexable option
        if (m_max_index > 0) {
            ++m_cur_index;
            PLASSERT( m_cur_index <= m_max_index );
            if (m_cur_index < m_max_index)
                return *this;
        }

        // Otherwise find next option accessible as object, and that is not
        // marked with the 'nontraversable' flag (assuming
        // 'ignore_nontraversable' is not set).  Go to invalid state if cannot
        // find any
        const int n = m_options->size();
        for ( ++m_cur_option ; m_cur_option < n ; ++m_cur_option ) {
            if (      (*m_options)[m_cur_option]->isAccessibleAsObject() &&
                 ( m_ignore_nontraversable ||
                   ! ((*m_options)[m_cur_option]->flags() & OptionBase::nontraversable)) )
            {
                m_cur_index = 0;
                m_max_index = (*m_options)[m_cur_option]->indexableSize(m_object);
                break;
            }
        }

        m_invalid = (m_cur_option >= n);

        // Condition below: keep incrementing as long as we are hitting a null
        // object (and skipping nulls is requested), or an empty array/vector
    } while (!m_invalid &&
             ( (!m_max_index && m_skip_nulls && !**this) ||
               ( m_max_index && m_cur_index >= m_max_index ) ));

    return *this;
}
ObjectOptionsIterator PLearn::ObjectOptionsIterator::operator++ ( int  ) [inline]

Post-increment: implemented in terms of pre-increment.

Note that this is expensive; don't use if not necessary.

Definition at line 138 of file ObjectGraphIterator.h.

    {
        ObjectOptionsIterator old(*this);
        ++(*this);
        return old;
    }
bool PLearn::ObjectOptionsIterator::operator== ( const ObjectOptionsIterator rhs) const

Equality testing.

Definition at line 97 of file ObjectGraphIterator.cc.

References m_cur_index, m_cur_option, m_invalid, m_max_index, m_object, and m_options.

{
    return (m_invalid && rhs.m_invalid)      ||
        (! m_invalid                         &&
         m_object      == rhs.m_object       &&
         m_options     == rhs.m_options      &&
         m_cur_option  == rhs.m_cur_option   &&
         m_cur_index   == rhs.m_cur_index    &&
         m_max_index   == rhs.m_max_index);
}

Member Data Documentation

If cur_option is indexable, current index within option.

Definition at line 152 of file ObjectGraphIterator.h.

Referenced by getCurrentOptionName(), operator*(), operator++(), and operator==().

If true: traverse nontraversable.

Definition at line 147 of file ObjectGraphIterator.h.

Referenced by operator++().

If true: invalid state.

Definition at line 146 of file ObjectGraphIterator.h.

Referenced by getCurrentOptionFlags(), getCurrentOptionName(), ObjectOptionsIterator(), operator*(), operator++(), and operator==().

If cur_option is indexable, maximum possible inxex+1.

Definition at line 154 of file ObjectGraphIterator.h.

Referenced by getCurrentOptionName(), operator*(), operator++(), and operator==().

OptionList of that object.

Definition at line 150 of file ObjectGraphIterator.h.

Referenced by getCurrentOptionFlags(), getCurrentOptionName(), ObjectOptionsIterator(), operator*(), operator++(), and operator==().

If true: never return null ptrs.

Definition at line 148 of file ObjectGraphIterator.h.

Referenced by operator++().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines