/* SPDX-License-Identifier: LGPL-3.0-or-later */

/*
 * Copyright (C) 2023 Perry Werneck <perry.werneck@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

 /**
  * @brief Implements value iterator.
  */

 #ifdef HAVE_CONFIG_H
	#include <config.h>
 #endif // HAVE_CONFIG_H

 #include <smbios/defs.h>
 #include <smbios/value.h>
 #include <private/decoders.h>
 #include <private/data.h>

 #include <stdexcept>

 using namespace std;

 namespace SMBios {

	Value::Iterator::~Iterator() {
	}

	Value::Iterator::operator bool() const {
		return value.get() && *value;
	}

	bool Value::Iterator::operator==(const Iterator& rhs) const {

		if(value.get() && rhs.value.get()) {
			return *value == *rhs.value;
		}

		if(value.get() || rhs.value.get()) {
			return false;
		}

		return true;
	}

	Value::Iterator Value::Iterator::operator++(int) {

		if(!*this) {
			return *this;
		}

		Iterator it{value->clone()};
		operator++();

		return it;

	}

	Value::Iterator & Value::Iterator::operator++() {
		if(*this) {
			if(!value->next()) {
				value.reset();
			}
		}
		return *this;
	}

 }

