/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_HashReverseIterator.qpp HashReverseIterator class definition */
/*
  Qore Programming Language

  Copyright (C) 2003 - 2023 David Nichols

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS IN THE SOFTWARE.

  Note that the Qore library is released under a choice of three open-source
  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
  information.
*/

#include <qore/Qore.h>
#include "qore/intern/QoreObjectIterator.h"

extern QoreClass* QC_OBJECTITERATOR;

//! This class an iterator class for objects
/** Call ObjectReverseIterator::next() to iterate through the object in reverse order; do not use the iterator if ObjectReverseIterator::next() returns @ref False.
    An object can be iterated in reverse order by calling ObjectReverseIterator::prev() instead of ObjectReverseIterator::next()

    @par Example: ObjectReverseIterator basic usage
    @code{.py}
    class Class1 {
       public {
          int attr1;
          date attr2;
       }
       constructor() {
          attr1 = 1;
          attr2 = now();
       }
    }
    Class1 o();

    ObjectReverseIterator it(o);
    while (it.next()) {
        printf("iter: %n\n", it.getValue());
    }

    iter: 2013-04-17 16:31:03 Wed +02:00 (CEST)
    iter: 1
    @endcode

    @note
    - In general, the ObjectReverseIterator class is not designed to be accessed from multiple threads; it was created without locking for
      fast and efficient use when used from a single thread.  For methods that would be unsafe to use in another thread, any use of
      such methods in threads other than the thread where the constructor was called will cause an \c ITERATOR-THREAD-ERROR to be thrown.
    - ObjectReverseIterator is functionally equivalent to ObjectIterator, but the effect of ObjectReverseIterator::next() and ObjectReverseIterator::prev()
      are the opposite of ObjectIterator::next() and ObjectIterator::prev(); that is ObjectReverseIterator::next() will
      iterate through the object in reverse order, while ObjectReverseIterator::prev() iterates in forward order.
      Additionally the meanings of the return values for ObjectReverseIterator::first() and ObjectReverseIterator::last()
      are swapped in respect to ObjectIterator::first() and ObjectIterator::last().

    @see ObjectIterator
 */
qclass ObjectReverseIterator [arg=QoreObjectReverseIterator* i; ns=Qore; vparent=ObjectIterator];

//! Creates the object iterator object
/** @param o the object to iterate

    @par Example:
    @code{.py}
ObjectReverseIterator i(obj);
    @endcode
 */
ObjectReverseIterator::constructor(object o) {
   self->setPrivate(CID_OBJECTREVERSEITERATOR, new QoreObjectReverseIterator(o));
}

//! Creates an empty iterator object
/** @par Example:
    @code{.py}
*object obj = get_object_or_nothing();
ObjectReverseIterator i(obj);
    @endcode
 */
ObjectReverseIterator::constructor() {
   self->setPrivate(CID_OBJECTREVERSEITERATOR, new QoreObjectReverseIterator);
}

//! Creates a copy of the ObjectReverseIterator object, iterating the same object as the original and in the same position
/** @par Example:
    @code{.py}
ObjectReverseIterator ni = i.copy();
    @endcode
 */
ObjectReverseIterator::copy() {
   self->setPrivate(CID_OBJECTREVERSEITERATOR, new QoreObjectReverseIterator(*i));
}

//! Moves the current position to the previous element in the object; returns @ref False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the last element in the object if the object is not empty
/** This method will return @ref True again after it returns @ref False once if the object is not empty, otherwise it will always return @ref False.
    The iterator object should not be used after this method returns @ref False

    @return @ref False if there are no more elements in the object (in which case the iterator object is invalid and should not be used); @ref True if successful (meaning that the iterator object is valid)

    @par Example:
    @code{.py}
while (i.prev()) {
    printf(" + %y\n", i.getValue());
}
    @endcode

    @note ObjectReverseIterator::next() is the opposite of ObjectIterator::next(); it is functionally equivalent to ObjectIterator::prev();
    ObjectReverseIterator::next() iterates through the object in reverse order

    @throw ITERATOR-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object
 */
bool ObjectReverseIterator::next() {
   if (i->check(xsink))
      return QoreValue();
   return i->prev();
}

//! Moves the current position to the next element in the object; returns @ref False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the first element in the object if the object is not empty
/** This method will return @ref True again after it returns @ref False once if object is not empty, otherwise it will always return @ref False.
    The iterator object should not be used after this method returns @ref False

    @return @ref False if there are no more elements in the object (in which case the iterator object is invalid and should not be used); @ref True if successful (meaning that the iterator object is valid)

    @par Example:
    @code{.py}
while (i.next()) {
    printf(" + %y\n", i.getValue());
}
    @endcode

    @note ObjectReverseIterator::prev() is the opposite of ObjectIterator::prev(); it is functionally equivalent to ObjectIterator::next();
    ObjectReverseIterator::prev() iterates through the object in forward order

    @throw ITERATOR-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object
 */
bool ObjectReverseIterator::prev() {
   if (i->check(xsink))
      return QoreValue();
   return i->next();
}

//! returns @ref True if on the last element of the object
/** @return @ref True if on the last element of the object

    @par Example:
    @code{.py}
while (i.next()) {
    if (i.first())
        printf("START:\n");
}
    @endcode
 */
bool ObjectReverseIterator::first() [flags=CONSTANT] {
   return i->last();
}

//! returns @ref True if on the first element of the object
/** @return @ref True if on the first element of the object

    @par Example:
    @code{.py}
while (i.next()) {
    if (i.last())
        printf("END.\n");
}
    @endcode
 */
bool ObjectReverseIterator::last() [flags=CONSTANT] {
   return i->first();
}
