Renamed kami::exception to kami::error to avoid conflicts with std::expection

This commit is contained in:
James P. Howard, II
2022-09-16 16:32:13 -04:00
parent 7a7db768ed
commit 80ac9571a7
11 changed files with 44 additions and 37 deletions

View File

@@ -2,14 +2,15 @@
// Created by James Howard on 9/9/22. // Created by James Howard on 9/9/22.
// //
#ifndef KAMI_EXCEPTION_H #ifndef KAMI_ERROR_H
//! @cond SuppressGuard //! @cond SuppressGuard
#define KAMI_EXCEPTION_H #define KAMI_ERROR_H
//! @endcond
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
namespace kami::exception { namespace kami::error {
class AgentNotFound : public std::logic_error { class AgentNotFound : public std::logic_error {
public: public:
@@ -18,6 +19,13 @@ namespace kami::exception {
explicit AgentNotFound(const std::string &s) : std::logic_error(s) {}; explicit AgentNotFound(const std::string &s) : std::logic_error(s) {};
}; };
class InvalidCoordinates : public std::domain_error {
public:
explicit InvalidCoordinates(const char *s) : std::domain_error(s) {};
explicit InvalidCoordinates(const std::string &s) : std::domain_error(s) {};
};
class InvalidOption : public std::invalid_argument { class InvalidOption : public std::invalid_argument {
public: public:
explicit InvalidOption(const char *s) : std::invalid_argument(s) {}; explicit InvalidOption(const char *s) : std::invalid_argument(s) {};
@@ -28,17 +36,15 @@ namespace kami::exception {
class LocationUnavailable : public std::domain_error { class LocationUnavailable : public std::domain_error {
public: public:
explicit LocationUnavailable(const char *s) : std::domain_error(s) {}; explicit LocationUnavailable(const char *s) : std::domain_error(s) {};
explicit LocationUnavailable(const std::string &s) : std::domain_error(s) {}; explicit LocationUnavailable(const std::string &s) : std::domain_error(s) {};
}; };
class ResourceNotAvailable : public std::logic_error { class ResourceNotAvailable : public std::logic_error {
public: public:
explicit ResourceNotAvailable(const char *s) : std::logic_error(s) {}; explicit ResourceNotAvailable(const char *s) : std::logic_error(s) {};
explicit ResourceNotAvailable(const std::string &s) : std::logic_error(s) {}; explicit ResourceNotAvailable(const std::string &s) : std::logic_error(s) {};
}; };
} }
#endif //KAMI_EXCEPTION_H #endif //KAMI_ERROR_H

View File

@@ -38,7 +38,7 @@
#include <vector> #include <vector>
#include <kami/domain.h> #include <kami/domain.h>
#include <kami/exception.h> #include <kami/error.h>
#include <kami/grid.h> #include <kami/grid.h>
#include <kami/kami.h> #include <kami/kami.h>

View File

@@ -34,7 +34,7 @@
#include <kami/agent.h> #include <kami/agent.h>
#include <kami/domain.h> #include <kami/domain.h>
#include <kami/exception.h> #include <kami/error.h>
#include <kami/grid1d.h> #include <kami/grid1d.h>
namespace kami { namespace kami {
@@ -103,7 +103,8 @@ namespace kami {
return agent_id; return agent_id;
} }
throw exception::AgentNotFound(""); throw error::AgentNotFound(
fmt::format("Agent {} not found at location {}", agent_id.to_string(), coord.to_string()));
} }
bool Grid1D::is_location_valid(const GridCoord1D &coord) const { bool Grid1D::is_location_valid(const GridCoord1D &coord) const {
@@ -148,7 +149,7 @@ namespace kami {
auto agent_ids = std::make_shared<std::set<AgentID>>(); auto agent_ids = std::make_shared<std::set<AgentID>>();
if (!is_location_valid(coord)) if (!is_location_valid(coord))
throw exception::LocationUnavailable(fmt::format("Coordinates {} are invalid", coord.to_string())); throw error::LocationUnavailable(fmt::format("Coordinates {} are invalid", coord.to_string()));
if (is_location_empty(coord)) if (is_location_empty(coord))
return agent_ids; return agent_ids;
@@ -168,7 +169,7 @@ namespace kami {
GridCoord1D Grid1D::get_location_by_agent(const AgentID &agent_id) const { GridCoord1D Grid1D::get_location_by_agent(const AgentID &agent_id) const {
auto coord = _agent_index->find(agent_id); auto coord = _agent_index->find(agent_id);
if (coord == _agent_index->end()) if (coord == _agent_index->end())
throw exception::AgentNotFound(fmt::format("Agent {} not found on grid", agent_id.to_string())); throw error::AgentNotFound(fmt::format("Agent {} not found on grid", agent_id.to_string()));
return coord->second; return coord->second;
} }

View File

@@ -33,7 +33,7 @@
#include <kami/agent.h> #include <kami/agent.h>
#include <kami/domain.h> #include <kami/domain.h>
#include <kami/exception.h> #include <kami/error.h>
#include <kami/grid2d.h> #include <kami/grid2d.h>
namespace kami { namespace kami {
@@ -64,7 +64,7 @@ namespace kami {
case GridDistanceType::Euclidean: case GridDistanceType::Euclidean:
return distance_euclidean(p); return distance_euclidean(p);
default: default:
throw exception::InvalidOption("Unknown distance type given"); throw error::InvalidOption("Unknown distance type given");
} }
} }
@@ -134,7 +134,7 @@ namespace kami {
return agent_id; return agent_id;
} }
throw exception::AgentNotFound("Agent not found"); throw error::AgentNotFound("Agent not found on grid");
} }
bool Grid2D::is_location_valid(const GridCoord2D &coord) const { bool Grid2D::is_location_valid(const GridCoord2D &coord) const {
@@ -174,7 +174,7 @@ namespace kami {
directions = directions_moore; directions = directions_moore;
break; break;
default: default:
throw exception::InvalidOption( throw error::InvalidOption(
fmt::format("Invalid neighborhood type {} given", (unsigned int) neighborhood_type)); fmt::format("Invalid neighborhood type {} given", (unsigned int) neighborhood_type));
} }
@@ -195,7 +195,7 @@ namespace kami {
auto agent_ids = std::make_shared<std::set<AgentID>>(); auto agent_ids = std::make_shared<std::set<AgentID>>();
if (!is_location_valid(coord)) if (!is_location_valid(coord))
throw exception::LocationUnavailable(fmt::format("Coordinates {} are invalid", coord.to_string())); throw error::LocationUnavailable(fmt::format("Coordinates {} are invalid", coord.to_string()));
if (is_location_empty(coord)) if (is_location_empty(coord))
return agent_ids; return agent_ids;
@@ -219,7 +219,7 @@ namespace kami {
GridCoord2D Grid2D::get_location_by_agent(const AgentID &agent_id) const { GridCoord2D Grid2D::get_location_by_agent(const AgentID &agent_id) const {
auto coord = _agent_index->find(agent_id); auto coord = _agent_index->find(agent_id);
if (coord == _agent_index->end()) if (coord == _agent_index->end())
throw exception::AgentNotFound(fmt::format("Agent {} not found on grid", agent_id.to_string())); throw error::AgentNotFound(fmt::format("Agent {} not found on grid", agent_id.to_string()));
return coord->second; return coord->second;
} }

View File

@@ -26,7 +26,7 @@
#include <memory> #include <memory>
#include <utility> #include <utility>
#include <kami/exception.h> #include <kami/error.h>
#include <kami/model.h> #include <kami/model.h>
#include <kami/scheduler.h> #include <kami/scheduler.h>
@@ -34,7 +34,7 @@ namespace kami {
std::shared_ptr<Domain> Model::get_domain() { std::shared_ptr<Domain> Model::get_domain() {
if (_domain == nullptr) if (_domain == nullptr)
throw exception::ResourceNotAvailable("Domain not found in model"); throw error::ResourceNotAvailable("Domain not found in model");
return _domain; return _domain;
} }
@@ -45,7 +45,7 @@ namespace kami {
std::shared_ptr<Population> Model::get_population() { std::shared_ptr<Population> Model::get_population() {
if (_pop == nullptr) if (_pop == nullptr)
throw exception::ResourceNotAvailable("Population not found in model"); throw error::ResourceNotAvailable("Population not found in model");
return _pop; return _pop;
} }
@@ -56,7 +56,7 @@ namespace kami {
std::shared_ptr<Scheduler> Model::get_scheduler() { std::shared_ptr<Scheduler> Model::get_scheduler() {
if (_sched == nullptr) if (_sched == nullptr)
throw exception::ResourceNotAvailable("Scheduler not found in model"); throw error::ResourceNotAvailable("Scheduler not found in model");
return _sched; return _sched;
} }

View File

@@ -27,7 +27,7 @@
#include <kami/agent.h> #include <kami/agent.h>
#include <kami/domain.h> #include <kami/domain.h>
#include <kami/exception.h> #include <kami/error.h>
#include <kami/grid1d.h> #include <kami/grid1d.h>
#include <kami/multigrid1d.h> #include <kami/multigrid1d.h>
@@ -38,7 +38,7 @@ namespace kami {
AgentID MultiGrid1D::add_agent(const AgentID agent_id, const GridCoord1D &coord) { AgentID MultiGrid1D::add_agent(const AgentID agent_id, const GridCoord1D &coord) {
if (!is_location_valid(coord)) if (!is_location_valid(coord))
throw exception::LocationUnavailable(fmt::format("Coordinates {} are invalid", coord.to_string())); throw error::InvalidCoordinates(fmt::format("Coordinates {} are invalid", coord.to_string()));
_agent_index->insert(std::pair<AgentID, GridCoord1D>(agent_id, coord)); _agent_index->insert(std::pair<AgentID, GridCoord1D>(agent_id, coord));
_agent_grid->insert(std::pair<GridCoord1D, AgentID>(coord, agent_id)); _agent_grid->insert(std::pair<GridCoord1D, AgentID>(coord, agent_id));

View File

@@ -27,7 +27,7 @@
#include <kami/agent.h> #include <kami/agent.h>
#include <kami/domain.h> #include <kami/domain.h>
#include <kami/exception.h> #include <kami/error.h>
#include <kami/grid2d.h> #include <kami/grid2d.h>
#include <kami/multigrid2d.h> #include <kami/multigrid2d.h>
@@ -38,7 +38,7 @@ namespace kami {
AgentID MultiGrid2D::add_agent(const AgentID agent_id, const GridCoord2D &coord) { AgentID MultiGrid2D::add_agent(const AgentID agent_id, const GridCoord2D &coord) {
if (!is_location_valid(coord)) if (!is_location_valid(coord))
throw exception::LocationUnavailable(fmt::format("Coordinates {} are invalid", coord.to_string())); throw error::InvalidCoordinates(fmt::format("Coordinates {} are invalid", coord.to_string()));
_agent_index->insert(std::pair<AgentID, GridCoord2D>(agent_id, coord)); _agent_index->insert(std::pair<AgentID, GridCoord2D>(agent_id, coord));
_agent_grid->insert(std::pair<GridCoord2D, AgentID>(coord, agent_id)); _agent_grid->insert(std::pair<GridCoord2D, AgentID>(coord, agent_id));

View File

@@ -28,7 +28,7 @@
#include <vector> #include <vector>
#include <kami/agent.h> #include <kami/agent.h>
#include <kami/exception.h> #include <kami/error.h>
#include <kami/population.h> #include <kami/population.h>
namespace kami { namespace kami {
@@ -43,7 +43,7 @@ namespace kami {
auto agent_it = _agent_map.find(agent_id); auto agent_it = _agent_map.find(agent_id);
if (agent_it == _agent_map.end()) if (agent_it == _agent_map.end())
throw exception::ResourceNotAvailable("Agent not found in population"); throw error::ResourceNotAvailable("Agent not found in population");
auto agent = agent_it->second; auto agent = agent_it->second;
_agent_map.erase(agent_it); _agent_map.erase(agent_it);
@@ -54,7 +54,7 @@ namespace kami {
auto agent_it = _agent_map.find(agent_id); auto agent_it = _agent_map.find(agent_id);
if (agent_it == _agent_map.end()) if (agent_it == _agent_map.end())
throw exception::ResourceNotAvailable("Agent not found in population"); throw error::AgentNotFound("Agent not found in population");
return agent_it->second; return agent_it->second;
} }

View File

@@ -29,7 +29,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <kami/exception.h> #include <kami/error.h>
#include <kami/model.h> #include <kami/model.h>
#include <kami/random.h> #include <kami/random.h>
#include <kami/sequential.h> #include <kami/sequential.h>
@@ -43,7 +43,7 @@ namespace kami {
std::unique_ptr<std::vector<AgentID>> std::unique_ptr<std::vector<AgentID>>
RandomScheduler::step(std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list) { RandomScheduler::step(std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list) {
if (_rng == nullptr) if (_rng == nullptr)
throw exception::ResourceNotAvailable("No random number generator available"); throw error::ResourceNotAvailable("No random number generator available");
shuffle(agent_list->begin(), agent_list->end(), *_rng); shuffle(agent_list->begin(), agent_list->end(), *_rng);
return std::move(this->SequentialScheduler::step(model, std::move(agent_list))); return std::move(this->SequentialScheduler::step(model, std::move(agent_list)));
@@ -52,7 +52,7 @@ namespace kami {
std::unique_ptr<std::vector<AgentID>> std::unique_ptr<std::vector<AgentID>>
RandomScheduler::step(std::shared_ptr<ReporterModel> model, std::unique_ptr<std::vector<AgentID>> agent_list) { RandomScheduler::step(std::shared_ptr<ReporterModel> model, std::unique_ptr<std::vector<AgentID>> agent_list) {
if (_rng == nullptr) if (_rng == nullptr)
throw exception::ResourceNotAvailable("No random number generator available"); throw error::ResourceNotAvailable("No random number generator available");
shuffle(agent_list->begin(), agent_list->end(), *_rng); shuffle(agent_list->begin(), agent_list->end(), *_rng);
return std::move(this->SequentialScheduler::step(model, std::move(agent_list))); return std::move(this->SequentialScheduler::step(model, std::move(agent_list)));

View File

@@ -26,7 +26,7 @@
#include <fmt/format.h> #include <fmt/format.h>
#include <kami/agent.h> #include <kami/agent.h>
#include <kami/exception.h> #include <kami/error.h>
#include <kami/sologrid1d.h> #include <kami/sologrid1d.h>
namespace kami { namespace kami {
@@ -36,9 +36,9 @@ namespace kami {
AgentID SoloGrid1D::add_agent(const AgentID agent_id, const GridCoord1D &coord) { AgentID SoloGrid1D::add_agent(const AgentID agent_id, const GridCoord1D &coord) {
if (!is_location_valid(coord)) if (!is_location_valid(coord))
throw exception::LocationUnavailable(fmt::format("Coordinates {} are invalid", coord.to_string())); throw error::InvalidCoordinates(fmt::format("Coordinates {} are invalid", coord.to_string()));
if (!is_location_empty(coord)) if (!is_location_empty(coord))
throw exception::LocationUnavailable(fmt::format("Coordinates {} already occupied", coord.to_string())); throw error::LocationUnavailable(fmt::format("Coordinates {} already occupied", coord.to_string()));
_agent_index->insert(std::pair<AgentID, GridCoord1D>(agent_id, coord)); _agent_index->insert(std::pair<AgentID, GridCoord1D>(agent_id, coord));
_agent_grid->insert(std::pair<GridCoord1D, AgentID>(coord, agent_id)); _agent_grid->insert(std::pair<GridCoord1D, AgentID>(coord, agent_id));

View File

@@ -28,7 +28,7 @@
#include <fmt/format.h> #include <fmt/format.h>
#include <kami/agent.h> #include <kami/agent.h>
#include <kami/exception.h> #include <kami/error.h>
#include <kami/sologrid2d.h> #include <kami/sologrid2d.h>
namespace kami { namespace kami {
@@ -38,9 +38,9 @@ namespace kami {
AgentID SoloGrid2D::add_agent(const AgentID agent_id, const GridCoord2D &coord) { AgentID SoloGrid2D::add_agent(const AgentID agent_id, const GridCoord2D &coord) {
if (!is_location_valid(coord)) if (!is_location_valid(coord))
throw exception::LocationUnavailable(fmt::format("Coordinates {} are invalid", coord.to_string())); throw error::InvalidCoordinates(fmt::format("Coordinates {} are invalid", coord.to_string()));
if (!is_location_empty(coord)) if (!is_location_empty(coord))
throw exception::LocationUnavailable(fmt::format("Coordinates {} already occupied", coord.to_string())); throw error::LocationUnavailable(fmt::format("Coordinates {} already occupied", coord.to_string()));
_agent_index->insert(std::pair<AgentID, GridCoord2D>(agent_id, coord)); _agent_index->insert(std::pair<AgentID, GridCoord2D>(agent_id, coord));
_agent_grid->insert(std::pair<GridCoord2D, AgentID>(coord, agent_id)); _agent_grid->insert(std::pair<GridCoord2D, AgentID>(coord, agent_id));