summaryrefslogtreecommitdiff
path: root/lib/nes/common/path-view.hh
blob: 3d670bfc55b4fdd1df7cd3e889a6aa99f6049ab7 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once

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

namespace nes
{
	/// Non-owning copy of a path, consisting of multiple path components stored as string views.
	///
	/// This is similar in spirit to a string_view, but for collections of string_views instead of characters.
	class path_view
	{
		std::string_view const* components_{ nullptr };
		u32 component_count_{ 0 };

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

			std::string_view const* current_{ nullptr };
			u32 remaining_{ 0 };

		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 { return current_ ? *current_ : ""; }

			auto operator++() -> iterator&
			{
				if (remaining_ > 0)
				{
					current_ += 1;
					remaining_ -= 1;
				}
				return *this;
			}

			auto operator++(int) -> iterator
			{
				auto res = *this;
				++*this;
				return res;
			}

		private:
			explicit iterator() = default;
			explicit iterator(std::string_view const* current, u32 const remaining)
				: current_{ current }
				, remaining_{ remaining }
			{
			}
		};

		explicit path_view() = default;

		explicit path_view(std::string_view const* components, u32 const component_count)
			: components_{ components }
			, component_count_{ component_count }
		{
		}

		auto get_components() const -> std::string_view const* { return components_; }
		auto get_component_count() const -> u32 { return component_count_; }
		auto get_component(u32 const index) const -> std::string_view { return components_[index]; }

		auto begin() const -> iterator { return iterator{ components_, component_count_ }; }
		auto end() const -> iterator { return iterator{ components_ + component_count_, 0 }; }
	};
} // namespace nes::app