From 6a6d9547cfe490343b459aad25b0de775ee5000e Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Tue, 4 Jun 2024 13:22:13 +0200 Subject: [PATCH] Improve generated interface function name to obtain the actual type (#595) * Improve interface function name getValue mixes a bit too much implementation detail into the interface. Additionally, as is very short and reads well in this context. * Fix occurences of getValue in tests * Update documentation --- doc/datamodel_syntax.md | 4 ++-- python/templates/Interface.h.jinja2 | 10 ++++++++-- tests/unittests/interface_types.cpp | 12 ++++++------ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/doc/datamodel_syntax.md b/doc/datamodel_syntax.md index 8181543fd..d16be965d 100644 --- a/doc/datamodel_syntax.md +++ b/doc/datamodel_syntax.md @@ -177,10 +177,10 @@ bool same = (energyType == cluster); // <-- true (comparisons work as expected) bool isCluster = energyType.isA(); // <-- true bool isHit = energyType.isA(); // <-- false -auto newCluster = energyType.getValue(); // <-- "cast back" to original type +auto newCluster = energyType.as(); // <-- "cast back" to original type // "Casting" only works if the types match. Otherwise there will be an exception -auto newHit = energyType.getValue(); // <-- exception +auto newHit = energyType.as(); // <-- exception ``` ## Global options diff --git a/python/templates/Interface.h.jinja2 b/python/templates/Interface.h.jinja2 index 1adab38de..ef96f601d 100644 --- a/python/templates/Interface.h.jinja2 +++ b/python/templates/Interface.h.jinja2 @@ -131,9 +131,9 @@ public: /// Get the contained value as the concrete type it was put in. This will /// throw a std::runtime_error if T is not the type of the currently held - /// value. Use holds to check beforehand if necessary template + /// value. Use isA to check beforehand if necessary template - T getValue() const { + T as() const { if (!isA()) { throw std::runtime_error("Cannot get value as object currently holds another type"); } @@ -141,6 +141,12 @@ public: return static_cast*>(m_self.get())->m_value; } + template + [[deprecated("Use 'as' instead.")]] + T getValue() const { + return as(); + } + friend bool operator==(const {{ class.bare_type }}& lhs, const {{ class.bare_type }}& rhs) { return lhs.m_self->equal(rhs.m_self.get()); } diff --git a/tests/unittests/interface_types.cpp b/tests/unittests/interface_types.cpp index 8808d9fe7..400015843 100644 --- a/tests/unittests/interface_types.cpp +++ b/tests/unittests/interface_types.cpp @@ -73,14 +73,14 @@ TEST_CASE("InterfaceType from immutable", "[interface-types][basics]") { WrapperT wrapper{hit}; REQUIRE(wrapper.isA()); REQUIRE_FALSE(wrapper.isA()); - REQUIRE(wrapper.getValue() == hit); + REQUIRE(wrapper.as() == hit); REQUIRE(wrapper == hit); ExampleCluster cluster{}; wrapper = cluster; REQUIRE(wrapper.isA()); - REQUIRE(wrapper.getValue() == cluster); - REQUIRE_THROWS_AS(wrapper.getValue(), std::runtime_error); + REQUIRE(wrapper.as() == cluster); + REQUIRE_THROWS_AS(wrapper.as(), std::runtime_error); REQUIRE(wrapper != hit); } @@ -91,7 +91,7 @@ TEST_CASE("InterfaceType from mutable", "[interface-types][basics]") { WrapperT wrapper{hit}; REQUIRE(wrapper.isA()); REQUIRE_FALSE(wrapper.isA()); - REQUIRE(wrapper.getValue() == hit); + REQUIRE(wrapper.as() == hit); REQUIRE(wrapper == hit); // Comparison also work against the immutable classes ExampleHit immutableHit = hit; @@ -100,8 +100,8 @@ TEST_CASE("InterfaceType from mutable", "[interface-types][basics]") { MutableExampleCluster cluster{}; wrapper = cluster; REQUIRE(wrapper.isA()); - REQUIRE(wrapper.getValue() == cluster); - REQUIRE_THROWS_AS(wrapper.getValue(), std::runtime_error); + REQUIRE(wrapper.as() == cluster); + REQUIRE_THROWS_AS(wrapper.as(), std::runtime_error); REQUIRE(wrapper != hit); }