From f1a2e3472e61b8d6c3b3d0159a859f1c41654028 Mon Sep 17 00:00:00 2001 From: Jupp Tscheak <jupp.tscheak@daimler.com> Date: Thu, 15 Jul 2021 15:47:27 +0200 Subject: [PATCH] Interface function IEntityRepository::Get is returning an optional reference to query an IEntity. In order to check whether an IEntity with a certain name/id exists, function IEntityRepository::Contains could be used to query this information. If IEntityRepository::Get is called on a non-existing name/id, the function would throw, so for defensive programming reasons a IEntityRepository::Contains should always be done before actually calling IEntityRepository::Get. The good thing about plain old C-pointers is that assignment and existence check can be done in one call. Since C++17 the same functionality is managed by std::optional. Another disadvantage of using a plain reference to IEntity is when it is used as a class member, where it needs to be initialized when an object of that class is defined. It might be that during construction time this IEntity is not known/existing yet. Of cource, a plain old C-pointer could be used in this case, but it wouldn't be a consistant usage. The proposal is to use std::optional<std::reference_wrapper<IEntity>> as return value for IEntityRepository::Get. Signed-off-by: Jupp Tscheak <jupp.tscheak@daimler.com> --- MantleAPI/include/MantleAPI/Traffic/i_entity_repository.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/MantleAPI/include/MantleAPI/Traffic/i_entity_repository.h b/MantleAPI/include/MantleAPI/Traffic/i_entity_repository.h index 38e865e3..bcc1bb1c 100644 --- a/MantleAPI/include/MantleAPI/Traffic/i_entity_repository.h +++ b/MantleAPI/include/MantleAPI/Traffic/i_entity_repository.h @@ -18,6 +18,8 @@ #include <MantleAPI/Traffic/entity_properties.h> #include <MantleAPI/Traffic/i_entity.h> +#include <functional> +#include <optional> #include <string> #include <vector> @@ -35,8 +37,8 @@ class IEntityRepository virtual IStaticObject& Create(UniqueId id, const std::string& name, const StaticObjectProperties& properties) = 0; virtual IVehicle& GetHost() = 0; - virtual IEntity& Get(const std::string& name) = 0; - virtual IEntity& Get(UniqueId id) = 0; + virtual std::optional<std::reference_wrapper<IEntity>> Get(const std::string& name) = 0; + virtual std::optional<std::reference_wrapper<IEntity>> Get(UniqueId id) = 0; virtual bool Contains(UniqueId id) const = 0; virtual void Delete(const std::string& name) = 0; -- GitLab