summaryrefslogtreecommitdiff
path: root/lib/nes/common/containers/path-view.hh
blob: 77410602e92b06ceb6c0be58dc5deb86067445d5 (plain)
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
#pragma once

#include "nes/common/types.hh"
#include <string_view>

namespace nes
{
	/// Non-owning copy of a path, useful for iterating over its components.
	class path_view
	{
		std::string_view path_;

	public:
		/// An iterator for the path components.
		class iterator
		{
			friend path_view;

			char const* current_{ nullptr }; // Start of the current component
			u32 component_length_{ 0 }; // Length of the current component
			u32 remaining_{ 0 }; // Number of characters remaining in the buffer, including the current component.

		public:
			auto operator==(iterator const& other) -> bool { return current_ == other.current_; }
			auto operator!=(iterator const& other) -> bool { return current_ != other.current_; }

			auto operator*() const -> std::string_view;
			auto operator++() -> iterator&;
			auto operator++(int) -> iterator;

		private:
			explicit iterator(char const* current, u32 const component_length, u32 const remaining)
				: current_{ current }
				, component_length_{ component_length }
				, remaining_{ remaining }
			{
			}
		};

		explicit path_view() = default;
		explicit path_view(std::string_view const path)
			: path_{ path }
		{
		}

		auto get_path() const -> std::string_view { return path_; }

		auto begin() const -> iterator;
		auto end() const -> iterator;
		auto is_empty() const -> bool { return begin() == end(); }
	};
} // namespace nes::app