From 888a8bbc7e30f33b730e79619201e56b13ead6f5 Mon Sep 17 00:00:00 2001 From: Oliver Woolland Date: Mon, 23 Nov 2026 10:09:02 +0000 Subject: [PATCH 1/2] WIP: make getentity safe --- include/ro-crate.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/ro-crate.hpp b/include/ro-crate.hpp index edf7942..996d13b 100644 --- a/include/ro-crate.hpp +++ b/include/ro-crate.hpp @@ -187,8 +187,7 @@ namespace rocrate { * Retrieves an entity from the RO-Crate's entity register by its id. * * @param id The identifier of the entity to retrieve. - * @return A reference to the entity with the specified id. - * @throw std::runtime_error if no entity with the given id is found in the RO-Crate. + * @return A reference to the entity with the specified id, if found, nullptr otherwise. */ Entity& getEntity(const std::string& id); @@ -283,7 +282,7 @@ namespace rocrate { inline Entity& ROCrate::getEntity(const std::string& id) { auto it = entities_.find(id); if (it == entities_.end()) { - throw std::runtime_error("Entity with id '" + id + "' not found in the RO-Crate."); + return nullptr; // Return nullptr if the entity is not found } return it->second; } From 4ee5b60af421d7c73f52016dfdd0e897d3e779c1 Mon Sep 17 00:00:00 2001 From: Oliver Woolland Date: Mon, 23 Nov 2026 12:48:20 +0000 Subject: [PATCH 2/2] Add hasEntity method --- include/ro-crate.hpp | 25 +++++++++++++++++++------ tests/unit.cpp | 6 ++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/include/ro-crate.hpp b/include/ro-crate.hpp index d838e5e..4b648b9 100644 --- a/include/ro-crate.hpp +++ b/include/ro-crate.hpp @@ -185,11 +185,20 @@ namespace rocrate { Entity& addEntity(const std::string& id, std::vector types); /** - * Retrieves an entity from the RO-Crate's entity register by its id. - * - * @param id The identifier of the entity to retrieve. - * @return A reference to the entity with the specified id, if found, nullptr otherwise. - */ + * Checks if an entity with the specified id exists in the RO-Crate's entity register. + * + * @param id The identifier of the entity to check. + * @return true if an entity with the given id exists, false otherwise. + */ + bool hasEntity(const std::string& id); + + /** + * Retrieves an entity from the RO-Crate's entity register by its id. + * + * @param id The identifier of the entity to retrieve. + * @return A reference to the entity with the specified id. + * @throw std::runtime_error if no entity with the given id exists in the RO-Crate. + */ Entity& getEntity(const std::string& id); /** @@ -290,10 +299,14 @@ namespace rocrate { return entities_.at(id); } + inline bool ROCrate::hasEntity(const std::string& id) { + return entities_.find(id) != entities_.end(); + } + inline Entity& ROCrate::getEntity(const std::string& id) { auto it = entities_.find(id); if (it == entities_.end()) { - return nullptr; // Return nullptr if the entity is not found + throw std::runtime_error("Entity with id '" + id + "' not found in the RO-Crate."); } return it->second; } diff --git a/tests/unit.cpp b/tests/unit.cpp index d9fde6e..da496d3 100644 --- a/tests/unit.cpp +++ b/tests/unit.cpp @@ -86,6 +86,12 @@ TEST_CASE("Ignore duplicate key-value pairs", "[unit]") { // Unit tests for RO-Crate class // ------------------------------------------------------------------------------- +TEST_CASE("hasEntity returns true/false correctly", "[unit]") { + rocrate::ROCrate crate; + REQUIRE(crate.hasEntity("./")); + REQUIRE_FALSE(crate.hasEntity("#nonexistent")); +} + TEST_CASE("Get entity throws if entity not found", "[unit]") { // Create a crate rocrate::ROCrate crate;