summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/ui/inifile.h
blob: 68adca3c917a912a58e63563bfe02e86cef35ab1 (plain) (blame)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
// license:BSD-3-Clause
// copyright-holders:Maurizio Petrarota, Vas Crabb
/***************************************************************************

    ui/inifile.h

    UI INIs file manager.

***************************************************************************/
#ifndef MAME_FRONTEND_UI_INIFILE_H
#define MAME_FRONTEND_UI_INIFILE_H

#pragma once

#include "ui/utils.h"

#include <functional>
#include <set>
#include <tuple>
#include <type_traits>
#include <unordered_set>


//-------------------------------------------------
//  INIFILE MANAGER
//-------------------------------------------------

class ui_options;

class inifile_manager
{
public:
	// construction/destruction
	inifile_manager(ui_options &options);

	// load systems from category
	void load_ini_category(size_t file, size_t category, std::unordered_set<game_driver const *> &result) const;

	// getters
	size_t get_file_count() const { return m_ini_index.size(); }
	std::string const &get_file_name(size_t file) const { return m_ini_index[file].first; }
	size_t get_category_count(size_t file) const { return m_ini_index[file].second.size(); }
	std::string const &get_category_name(size_t file, size_t category) const { return m_ini_index[file].second[category].first; }

private:
	// ini file structure
	using categoryindex = std::vector<std::pair<std::string, int64_t>>;

	void init_category(std::string &&filename, emu_file &file);

	// internal state
	ui_options &m_options;
	std::vector<std::pair<std::string, categoryindex> > m_ini_index;

};

//-------------------------------------------------
//  FAVORITE MANAGER
//-------------------------------------------------

class favorite_manager
{
public:
	// construction/destruction
	favorite_manager(ui_options &options);

	// add
	void add_favorite_system(game_driver const &driver);
	void add_favorite_software(ui_software_info const &swinfo);
	void add_favorite(running_machine &machine);

	// check
	bool is_favorite_system(game_driver const &driver) const;
	bool is_favorite_software(ui_software_info const &swinfo) const;
	bool is_favorite_system_software(ui_software_info const &swinfo) const;
	bool is_favorite(running_machine &machine) const;

	// remove
	void remove_favorite_system(game_driver const &driver);
	void remove_favorite_software(ui_software_info const &swinfo);
	void remove_favorite(running_machine &machine);

	// walk
	template <typename T> void apply(T &&action)
	{
		for (auto const &item : m_favorites)
			action(item);
	}
	template <typename T> void apply_sorted(T &&action)
	{
		update_sorted();
		for (auto const &item : m_sorted)
			action(item.get());
	}

private:
	using running_software_key = std::tuple<game_driver const &, char const *, std::string const &>;

	struct favorite_compare
	{
		using is_transparent = std::true_type;

		bool operator()(ui_software_info const &lhs, ui_software_info const &rhs) const;
		bool operator()(ui_software_info const &lhs, game_driver const &rhs) const;
		bool operator()(game_driver const &lhs, ui_software_info const &rhs) const;
		bool operator()(ui_software_info const &lhs, running_software_key const &rhs) const;
		bool operator()(running_software_key const &lhs, ui_software_info const &rhs) const;
	};

	using favorites_set = std::set<ui_software_info, favorite_compare>;
	using sorted_favorites = std::vector<std::reference_wrapper<ui_software_info const> >;

	// implementation
	template <typename T> static void apply_running_machine(running_machine &machine, T &&action);
	template <typename T> void add_impl(T &&key);
	template <typename T> bool check_impl(T const &key) const;
	template <typename T> bool remove_impl(T const &key);
	void update_sorted();
	void save_favorites();

	// internal state
	ui_options &m_options;
	favorites_set m_favorites;
	sorted_favorites m_sorted;
	bool m_need_sort;
};

#endif  // MAME_FRONTEND_UI_INIFILE_H