RangeArg.hxx 3.27 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
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
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_PROTOCOL_RANGE_ARG_HXX
#define MPD_PROTOCOL_RANGE_ARG_HXX

#include <limits>

struct RangeArg {
	unsigned start, end;

28 29 30 31 32 33 34
	/**
	 * Construct an open-ended range starting at the given index.
	 */
	static constexpr RangeArg OpenEnded(unsigned start) noexcept {
		return { start, std::numeric_limits<unsigned>::max() };
	}

35
	static constexpr RangeArg All() noexcept {
36
		return OpenEnded(0);
37
	}
38

39 40 41 42 43 44 45
	/**
	 * Construct an instance describing exactly one index.
	 */
	static constexpr RangeArg Single(unsigned i) noexcept {
		return { i, i + 1 };
	}

46 47 48 49 50 51 52 53
	constexpr bool operator==(RangeArg other) const noexcept {
		return start == other.start && end == other.end;
	}

	constexpr bool operator!=(RangeArg other) const noexcept {
		return !(*this == other);
	}

54 55 56 57
	constexpr bool IsOpenEnded() const noexcept {
		return end == All().end;
	}

58 59 60
	constexpr bool IsAll() const noexcept {
		return *this == All();
	}
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	constexpr bool IsWellFormed() const noexcept {
		return start <= end;
	}

	/**
	 * Is this range empty?  A malformed range also counts as
	 * "empty" for this method.
	 */
	constexpr bool IsEmpty() const noexcept {
		return start >= end;
	}

	/**
	 * Check if the range contains at least this number of items.
	 * Unlike Count(), this allows the object to be malformed.
	 */
	constexpr bool HasAtLeast(unsigned n) const noexcept {
		return start + n <= end;
	}

82 83 84
	constexpr bool Contains(unsigned i) const noexcept {
		return i >= start && i < end;
	}
85 86 87 88 89 90 91 92

	/**
	 * Count the number of items covered by this range.  This requires the
	 * object to be well-formed.
	 */
	constexpr unsigned Count() const noexcept {
		return end - start;
	}
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

	/**
	 * Make sure that both start and end are within the given
	 * count.
	 */
	constexpr void ClipRelaxed(unsigned count) noexcept {
		if (end > count)
			end = count;

		if (start > end)
			start = end;
	}

	/**
	 * Check if the start index is valid and clip the end of the
	 * range.
	 *
	 * @return false if the start is out of range
	 */
	[[nodiscard]]
	constexpr bool CheckClip(unsigned count) noexcept {
		if (start > count)
			return false;

		if (end > count)
			end = count;

		return true;
	}

	/**
	 * Check if start and end index are valid and adjust the end if this
	 * is an open-ended range.
	 *
	 * @return false if start or end is out of range
	 */
	[[nodiscard]]
	constexpr bool CheckAdjustEnd(unsigned count) noexcept {
		if (start > count)
			return false;

		if (end > count) {
			if (!IsOpenEnded())
				return false;

			end = count;
		}

		return true;
	}
143 144 145
};

#endif