view dep/animia/src/strategist.cc @ 151:54744a48a7d7

last commit part 2: struct init with {} is valid syntax, actually
author Paper <mrpapersonic@gmail.com>
date Tue, 14 Nov 2023 16:31:21 -0500
parents ffa535b6d630
children 8700806c2cc2
line wrap: on
line source

#include "animia/strategies.h"
#include "animia/util.h"
#include "animia/fd.h"
#include "animia.h"

namespace animia::internal {

class Strategist {
	public:
		Strategist(Result& result) : result_(result) {}

		bool ApplyStrategies();

	private:
		bool AddMedia(const MediaInfo media_information);

		bool ApplyOpenFilesStrategy();

		Result& result_;
};

bool Strategist::ApplyStrategies() {
	bool success = false;

	for (const auto strategy : result_.player.strategies) {
		switch (strategy) {
			case Strategy::OpenFiles:
				success |= ApplyOpenFilesStrategy();
				break;
		}
	}

	return success;
}

bool ApplyStrategies(std::vector<Result>& results) {
	bool success = false;

	for (auto& result : results) {
		Strategist strategist(result);
		success |= strategist.ApplyStrategies();
	}

	return success;
}

////////////////////////////////////////////////////////////////////////////////

bool Strategist::ApplyOpenFilesStrategy() {
	bool success = false;

	const std::set<pid_t> pids{result_.process.pid};
	std::vector<OpenFile> files;

	fd.EnumerateOpenFiles(pids, files);

	for (const auto& [pid, path] : files) {
		success |= AddMedia({MediaInfoType::File, path});
	}

	return success;
}

////////////////////////////////////////////////////////////////////////////////

bool Strategist::AddMedia(const MediaInfo media_information) {
	if (media_information.value.empty())
		return false;

	Media media;
	media.information.push_back(media_information);
	result_.media.push_back(std::move(media));

	return true;
}

}