repoindex/lib/alpm/list.h

121 lines
2.3 KiB
C++

#ifndef ALPM_LIST_H
#define ALPM_LIST_H
#include <alpm.h>
#include <iterator>
namespace RepoIndex {
/*!
* \brief The ListIterator class is used to iterate Alpm::List instances.
*/
template <class T = const char *>
class AlpmListIterator : public std::iterator<std::output_iterator_tag, T>
{
public:
/*!
* \brief Constructs a new iterator for the specified ALPM list node.
*/
AlpmListIterator(alpm_list_t *i) :
m_ptr(i)
{}
/*!
* \brief Returns the data for the current list node.
*/
const T operator *() const
{
return static_cast<T>(m_ptr->data);
}
/*!
* \brief Returns the data for the current list node.
*/
T operator*()
{
return static_cast<T>(m_ptr->data);
}
/*!
* \brief Returns whether the iterator is valid.
*/
operator bool() const
{
return m_ptr != nullptr;
}
/*!
* \brief Moves to the next list node.
*/
AlpmListIterator &operator++()
{
m_ptr = m_ptr->next;
return *this;
}
/*!
* \brief Moves to the previous list node.
*/
AlpmListIterator &operator--()
{
m_ptr = m_ptr->prev;
return *this;
}
/*!
* \brief Returns the pointer to the current ALPM list node.
*/
alpm_list_t *ptr()
{
return m_ptr;
}
private:
mutable alpm_list_t *m_ptr;
};
/*!
* \brief The List class is a simple C++ wrapper around the ALPM list implementation.
*/
template <class T = const char *>
class AlpmList
{
public:
/*!
* \brief Constructs a list with the specified ALPM list node as the begin.
*/
AlpmList(alpm_list_t *begin) :
m_begin(begin)
{}
/*!
* \brief Returns an iterator for the first list node.
*/
AlpmListIterator<T> begin() const
{
return m_begin;
}
/*!
* \brief Returns an invalid iterator indicating the end of the list.
*/
AlpmListIterator<T> end() const
{
return AlpmListIterator<T>(nullptr);
}
private:
AlpmListIterator<T> m_begin;
};
typedef AlpmList<const char *> StringList;
typedef AlpmList<alpm_depend_t *> DependencyList;
typedef AlpmList<alpm_pkg_t *> PackageList;
typedef AlpmList<alpm_group_t *> GroupList;
typedef AlpmList<alpm_backup_t *> BackupList;
} // namespace Alpm
#endif // ALPM_LIST_H