mirror of
https://github.com/JHUAPL/kami.git
synced 2026-01-09 14:58:02 -05:00
Renamed kami::exception to kami::error to avoid conflicts with std::expection
This commit is contained in:
@@ -2,14 +2,15 @@
|
||||
// Created by James Howard on 9/9/22.
|
||||
//
|
||||
|
||||
#ifndef KAMI_EXCEPTION_H
|
||||
#ifndef KAMI_ERROR_H
|
||||
//! @cond SuppressGuard
|
||||
#define KAMI_EXCEPTION_H
|
||||
#define KAMI_ERROR_H
|
||||
//! @endcond
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace kami::exception {
|
||||
namespace kami::error {
|
||||
|
||||
class AgentNotFound : public std::logic_error {
|
||||
public:
|
||||
@@ -18,6 +19,13 @@ namespace kami::exception {
|
||||
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 {
|
||||
public:
|
||||
explicit InvalidOption(const char *s) : std::invalid_argument(s) {};
|
||||
@@ -28,17 +36,15 @@ namespace kami::exception {
|
||||
class LocationUnavailable : public std::domain_error {
|
||||
public:
|
||||
explicit LocationUnavailable(const char *s) : std::domain_error(s) {};
|
||||
|
||||
explicit LocationUnavailable(const std::string &s) : std::domain_error(s) {};
|
||||
};
|
||||
|
||||
class ResourceNotAvailable : public std::logic_error {
|
||||
public:
|
||||
explicit ResourceNotAvailable(const char *s) : std::logic_error(s) {};
|
||||
|
||||
explicit ResourceNotAvailable(const std::string &s) : std::logic_error(s) {};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //KAMI_EXCEPTION_H
|
||||
#endif //KAMI_ERROR_H
|
||||
@@ -38,7 +38,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include <kami/domain.h>
|
||||
#include <kami/exception.h>
|
||||
#include <kami/error.h>
|
||||
#include <kami/grid.h>
|
||||
#include <kami/kami.h>
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/domain.h>
|
||||
#include <kami/exception.h>
|
||||
#include <kami/error.h>
|
||||
#include <kami/grid1d.h>
|
||||
|
||||
namespace kami {
|
||||
@@ -103,7 +103,8 @@ namespace kami {
|
||||
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 {
|
||||
@@ -148,7 +149,7 @@ namespace kami {
|
||||
auto agent_ids = std::make_shared<std::set<AgentID>>();
|
||||
|
||||
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))
|
||||
return agent_ids;
|
||||
|
||||
@@ -168,7 +169,7 @@ namespace kami {
|
||||
GridCoord1D Grid1D::get_location_by_agent(const AgentID &agent_id) const {
|
||||
auto coord = _agent_index->find(agent_id);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/domain.h>
|
||||
#include <kami/exception.h>
|
||||
#include <kami/error.h>
|
||||
#include <kami/grid2d.h>
|
||||
|
||||
namespace kami {
|
||||
@@ -64,7 +64,7 @@ namespace kami {
|
||||
case GridDistanceType::Euclidean:
|
||||
return distance_euclidean(p);
|
||||
default:
|
||||
throw exception::InvalidOption("Unknown distance type given");
|
||||
throw error::InvalidOption("Unknown distance type given");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ namespace kami {
|
||||
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 {
|
||||
@@ -174,7 +174,7 @@ namespace kami {
|
||||
directions = directions_moore;
|
||||
break;
|
||||
default:
|
||||
throw exception::InvalidOption(
|
||||
throw error::InvalidOption(
|
||||
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>>();
|
||||
|
||||
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))
|
||||
return agent_ids;
|
||||
|
||||
@@ -219,7 +219,7 @@ namespace kami {
|
||||
GridCoord2D Grid2D::get_location_by_agent(const AgentID &agent_id) const {
|
||||
auto coord = _agent_index->find(agent_id);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include <kami/exception.h>
|
||||
#include <kami/error.h>
|
||||
#include <kami/model.h>
|
||||
#include <kami/scheduler.h>
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace kami {
|
||||
|
||||
std::shared_ptr<Domain> Model::get_domain() {
|
||||
if (_domain == nullptr)
|
||||
throw exception::ResourceNotAvailable("Domain not found in model");
|
||||
throw error::ResourceNotAvailable("Domain not found in model");
|
||||
return _domain;
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace kami {
|
||||
|
||||
std::shared_ptr<Population> Model::get_population() {
|
||||
if (_pop == nullptr)
|
||||
throw exception::ResourceNotAvailable("Population not found in model");
|
||||
throw error::ResourceNotAvailable("Population not found in model");
|
||||
return _pop;
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ namespace kami {
|
||||
|
||||
std::shared_ptr<Scheduler> Model::get_scheduler() {
|
||||
if (_sched == nullptr)
|
||||
throw exception::ResourceNotAvailable("Scheduler not found in model");
|
||||
throw error::ResourceNotAvailable("Scheduler not found in model");
|
||||
return _sched;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/domain.h>
|
||||
#include <kami/exception.h>
|
||||
#include <kami/error.h>
|
||||
#include <kami/grid1d.h>
|
||||
#include <kami/multigrid1d.h>
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace kami {
|
||||
|
||||
AgentID MultiGrid1D::add_agent(const AgentID agent_id, const GridCoord1D &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_grid->insert(std::pair<GridCoord1D, AgentID>(coord, agent_id));
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/domain.h>
|
||||
#include <kami/exception.h>
|
||||
#include <kami/error.h>
|
||||
#include <kami/grid2d.h>
|
||||
#include <kami/multigrid2d.h>
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace kami {
|
||||
|
||||
AgentID MultiGrid2D::add_agent(const AgentID agent_id, const GridCoord2D &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_grid->insert(std::pair<GridCoord2D, AgentID>(coord, agent_id));
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/exception.h>
|
||||
#include <kami/error.h>
|
||||
#include <kami/population.h>
|
||||
|
||||
namespace kami {
|
||||
@@ -43,7 +43,7 @@ namespace kami {
|
||||
auto agent_it = _agent_map.find(agent_id);
|
||||
|
||||
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;
|
||||
_agent_map.erase(agent_it);
|
||||
@@ -54,7 +54,7 @@ namespace kami {
|
||||
auto agent_it = _agent_map.find(agent_id);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <kami/exception.h>
|
||||
#include <kami/error.h>
|
||||
#include <kami/model.h>
|
||||
#include <kami/random.h>
|
||||
#include <kami/sequential.h>
|
||||
@@ -43,7 +43,7 @@ namespace kami {
|
||||
std::unique_ptr<std::vector<AgentID>>
|
||||
RandomScheduler::step(std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list) {
|
||||
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);
|
||||
return std::move(this->SequentialScheduler::step(model, std::move(agent_list)));
|
||||
@@ -52,7 +52,7 @@ namespace kami {
|
||||
std::unique_ptr<std::vector<AgentID>>
|
||||
RandomScheduler::step(std::shared_ptr<ReporterModel> model, std::unique_ptr<std::vector<AgentID>> agent_list) {
|
||||
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);
|
||||
return std::move(this->SequentialScheduler::step(model, std::move(agent_list)));
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/exception.h>
|
||||
#include <kami/error.h>
|
||||
#include <kami/sologrid1d.h>
|
||||
|
||||
namespace kami {
|
||||
@@ -36,9 +36,9 @@ namespace kami {
|
||||
|
||||
AgentID SoloGrid1D::add_agent(const AgentID agent_id, const GridCoord1D &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))
|
||||
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_grid->insert(std::pair<GridCoord1D, AgentID>(coord, agent_id));
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/exception.h>
|
||||
#include <kami/error.h>
|
||||
#include <kami/sologrid2d.h>
|
||||
|
||||
namespace kami {
|
||||
@@ -38,9 +38,9 @@ namespace kami {
|
||||
|
||||
AgentID SoloGrid2D::add_agent(const AgentID agent_id, const GridCoord2D &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))
|
||||
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_grid->insert(std::pair<GridCoord2D, AgentID>(coord, agent_id));
|
||||
|
||||
Reference in New Issue
Block a user