From c583337927c3dff61d7a3cf19aa4fda96c4d0e96 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 12 Jan 2023 23:11:18 +0100 Subject: Added method to retrieve default/first item from repository --- universe/src/JsonRepository.cpp | 20 ++++++++++++++++++++ universe/src/JsonRepository.h | 2 ++ 2 files changed, 22 insertions(+) (limited to 'universe/src') diff --git a/universe/src/JsonRepository.cpp b/universe/src/JsonRepository.cpp index e8c8ed3..0100eb6 100644 --- a/universe/src/JsonRepository.cpp +++ b/universe/src/JsonRepository.cpp @@ -82,6 +82,16 @@ JsonRepository::JsonRepository(const std::string& path) ShipType +JsonRepository::ship_type() const +{ + const auto it = ships.begin(); + if (it == ships.end()) + throw NotFound("any/default"); + return it->second; +} + + +ShipType JsonRepository::ship_type(const std::string& id) const try { return ships.at(id); @@ -92,6 +102,16 @@ catch (const std::out_of_range&) { TurretType +JsonRepository::turret_type() const +{ + const auto it = turrets.begin(); + if (it == turrets.end()) + throw NotFound("any/default"); + return it->second; +} + + +TurretType JsonRepository::turret_type(const std::string& id) const try { return turrets.at(id); diff --git a/universe/src/JsonRepository.h b/universe/src/JsonRepository.h index ea6bf46..13df7e4 100644 --- a/universe/src/JsonRepository.h +++ b/universe/src/JsonRepository.h @@ -19,7 +19,9 @@ class JsonRepository : public Repository { public: explicit JsonRepository(const std::string& path); + ShipType ship_type() const override; ShipType ship_type(const std::string& id) const override; + TurretType turret_type() const override; TurretType turret_type(const std::string& id) const override; void for_ship_types(std::function func) const; void for_turret_types(std::function func) const; -- cgit v1.1