C

Qt Quick Ultralite swipegame Demo

#ifndef HIGHSCOREMODEL_H
#define HIGHSCOREMODEL_H
/****************************************************************************** ** ** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Ultralite module. ** ** $QT_BEGIN_LICENSE:COMM$ ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** $QT_END_LICENSE$ ** ******************************************************************************/
#include <qul/model.h> #include <vector> #include <array> #include <qul/private/optional.h> struct Highscore { int time; int tries; int score; }; struct sort_order { inline bool operator()(const Qul::Private::optional<Highscore> &lhs, const Qul::Private::optional<Highscore> &rhs) { if (!lhs) { return false; } if (!rhs) { return true; } return ((*lhs).score > (*rhs).score); } }; inline bool operator==(const Qul::Private::optional<Highscore> &lhs, const Qul::Private::optional<Highscore> &rhs) { if (!lhs && !rhs) { return true; } if ((!lhs && rhs) || (lhs && !rhs)) { return false; } return (*lhs).time == (*rhs).time && (*lhs).tries == (*rhs).tries && (*lhs).score == (*rhs).score; } inline bool operator>(const Qul::Private::optional<Highscore> &lhs, const Qul::Private::optional<Highscore> &rhs) { return sort_order{}(lhs, rhs); } inline bool operator<(const Qul::Private::optional<Highscore> &lhs, const Qul::Private::optional<Highscore> &rhs) { return !sort_order{}(lhs, rhs); } class HighscoreModel : public Qul::ListModel<Highscore> { public: HighscoreModel(); int count() const override; Highscore data(int index) const override; void addEntry(int time, int tries, int score); private: static constexpr std::uint8_t m_scoreboardLength{10U}; std::array<Qul::Private::optional<Highscore>, m_scoreboardLength> m_highscores; }; #endif // HIGHSCOREMODEL_H