element_group.hpp 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
#ifndef CVVISUAL_ELEMENTGROUP_HPP
#define CVVISUAL_ELEMENTGROUP_HPP

#include <functional>
#include <QList>
#include <QStringList>
#include <QString>
#include <stdint.h>
#include <stdexcept>


namespace cvv
{
namespace stfl
{

/**
 * @brief A group of elements with a title.
 */
template <class Element> class ElementGroup
{
public:
	/**
	 * @brief Contructs an empty ElementGroup.
	 */
	ElementGroup()
	{
	}

	/**
	 * @brief Constructs a new ElementGroup
	 * @param _titles title of this group, consisting of several sub titles
	 * @param _elements elements of this group
	 */
	ElementGroup(QStringList _titles, QList<Element> &_elements)
	    : titles{ _titles }, elements{ _elements }
	{
	}
	/**
	 * @brief Checks whether or not this group contains the element.
	 * @param element element to be checked
	 * @return does this group contain the given element?
	 */
	bool contains(const Element &element)
	{
		return this->elements.contains(element);
	}

	/**
	 * @brief Return the inherited elements.
	 * @return the interited elements
	 */
	QList<Element> getElements()
	{
		return this->elements;
	}

	/**
	 * @brief Returns the number of elements in this group.
	 * @return number of elements in this group
	 */
	size_t size() const
	{
		return this->elements.size();
	}

	/**
	 * @brief Returns the title (consisting of sub titles).
	 * @return the group title
	 */
	QStringList getTitles() const
	{
		return this->titles;
	}

	/**
	 * @brief Checks whether this an the given element group have the same
	 * titles.
	 * @param other given other element group
	 * @return Does this element group and the given have the same titles.
	 */
	bool hasSameTitles(ElementGroup<Element> &other)
	{
		for (auto title : other.getTitles())
		{
			if (!titles.contains(title))
			{
				return false;
			}
		}
		return other.getTitles().size() == titles.size();
	}

	/**
	 * @brief Checks whether this an the given element have the same list of
	 * elements.
	 * @param other given other element group
	 * @param elementCompFunc element comparison function that gets two
	 * elements passed
	 * and returns true if both can be considered equal
	 * @return Does this element group and the given have the same list of
	 * elements.
	 */
	bool
	hasSameElementList(ElementGroup<Element> &other,
	                   std::function<bool(const Element &, const Element &)>
	                       elementCompFunc)
	{
		if (other.getElements().size() != elements.size())
		{
			return false;
		}
		for (int i = 0; i < elements.size(); i++)
		{
			if (!elementCompFunc(elements[i], other.get(i)))
			{
				return false;
			}
		}
		return true;
	}

	/**
	 * @brief Get the element at the given index (in this group).
	 *
	 * @param index given index
	 * @return element at the given index
	 * @throws std::invalid_argument if no such element exists
	 */
	Element get(size_t index)
	{
		if (index >= size())
		{
			throw std::invalid_argument{
				"there is no call with this id"
			};
		}
		return this->elements[index];
	}

	/**
	 * @brief Remove the element at the given index.
	 * @param index given element index
	 */
	void removeElement(size_t index)
	{
		if (index < static_cast<size_t>(elements.size()))
		{
			elements.removeAt(index);
		}
	}

	/**
	 * @brief Sets the inhereted elements.
	 * @param newElements new elements of this group
	 */
	void setElements(QList<Element> newElements)
	{
		elements = newElements;
	}

private:
	QStringList titles;
	QList<Element> elements;
};
}
}
#endif