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

    Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.

    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/SingleValueIterator.h"

//! This class defines a simple iterator to be used to iterate single values (or complex objects where no iterator has been implemented yet)
/** @since %Qore 0.8.6

    @par Example: SingleValueIterator basic usage
    @code{.py}
    auto val = 1;
    SingleValueIterator it(val);
    while (it.next()) {
        printf("iter: %n\n", it.getValue());
    }

    iter: 1
    @endcode

    Remember that input value is taken as a single token so result of the code above for a list as an input argument will be like this:

    @code{.py}
    auto val = (1, 2, 3);
    iter: list: (1, 2, 3)
    @endcode

 */
qclass SingleValueIterator [arg=SingleValueIterator* i; ns=Qore; vparent=AbstractIterator];

//! creates the single value iterator with the value passed as an argument
/** @param v the value to iterate

    @par Example:
    @code{.py}
SingleValueIterator i(v);
    @endcode
 */
SingleValueIterator::constructor(auto v) {
   self->setPrivate(CID_SINGLEVALUEITERATOR, new SingleValueIterator(v));
}

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

//! This method returns @ref True "True" and @ref False "False" alternately unless it has no value to iterate, in which case it returns only @ref False "False"
/** The iterator object should not be used after this method returns @ref False

    @return @ref True "True" and @ref False "False" alternately unless it has no value to iterate, in which case it returns only @ref False "False"

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

    @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 SingleValueIterator::next() {
   if (i->check(xsink))
      return false;
   return i->next();
}

//! returns the current value or throws an \c INVALID-ITERATOR exception if the iterator is invalid
/** @return the current value or throws an \c INVALID-ITERATOR exception if the iterator is invalid

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

    @throw INVALID-ITERATOR the iterator is not pointing at a valid element
 */
auto SingleValueIterator::getValue() [flags=RET_VALUE_ONLY] {
   return i->checkValid(xsink) ? QoreValue() : i->getValue();
}

//! returns @ref True "True" if the iterator is currently pointing at a valid element, @ref False "False" if not
/** @return @ref True "True" if the iterator is currently pointing at a valid element, @ref False "False" if not

    @par Example:
    @code{.py}
if (i.valid())
    printf("current value: %y\n", i.getValue());
    @endcode
 */
bool SingleValueIterator::valid() [flags=CONSTANT] {
   return i->valid();
}

//! Reset the iterator instance to its initial state
/** Reset the iterator instance to its initial state

   @par Example
   @code{.py}
i.reset();
   @endcode

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