From 275ce060f0b370f2fc8f388d557f6dcc2d002fc4 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sat, 5 Jun 2021 12:29:59 +0200 Subject: [PATCH] Minor optimization --- Hazelnupp/Hazelnupp.cpp | 9 +++++---- Hazelnupp/Hazelnupp.h | 4 ++-- Hazelnupp/Placeholders.h | 11 +++++++++++ Hazelnupp/VoidValue.cpp | 5 +++-- Hazelnupp/VoidValue.h | 4 ++-- Test_Hazelnupp/Conversion.cpp | 17 ++++------------- 6 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 Hazelnupp/Placeholders.h diff --git a/Hazelnupp/Hazelnupp.cpp b/Hazelnupp/Hazelnupp.cpp index fbc7214..09e849f 100644 --- a/Hazelnupp/Hazelnupp.cpp +++ b/Hazelnupp/Hazelnupp.cpp @@ -5,6 +5,7 @@ #include "StringValue.h" #include "ListValue.h" #include "HazelnuppException.h" +#include "Placeholders.h" #include "StringTools.h" #include #include @@ -320,12 +321,12 @@ void Hazelnp::Hazelnupp::RegisterDescription(const std::string& parameter, const return; } -std::string Hazelnp::Hazelnupp::GetDescription(const std::string& parameter) const +const std::string& Hazelnp::Hazelnupp::GetDescription(const std::string& parameter) const { // Do we already have a description for this parameter? if (!HasDescription(parameter)) // No? Then return "" - return ""; + return Placeholders::g_emptyString; // We do? Then return it return parameterDescriptions.find(parameter)->second; @@ -523,10 +524,10 @@ void Hazelnupp::RegisterAbbreviation(const std::string& abbrev, const std::strin return; } -std::string Hazelnupp::GetAbbreviation(const std::string& abbrev) const +const std::string& Hazelnupp::GetAbbreviation(const std::string& abbrev) const { if (!HasAbbreviation(abbrev)) - return ""; + return Placeholders::g_emptyString; return parameterAbreviations.find(abbrev)->second; } diff --git a/Hazelnupp/Hazelnupp.h b/Hazelnupp/Hazelnupp.h index 3d4b073..a775219 100644 --- a/Hazelnupp/Hazelnupp.h +++ b/Hazelnupp/Hazelnupp.h @@ -34,7 +34,7 @@ namespace Hazelnp //! Will return the long form of an abbreviation (like --force for -f) //! Returns "" if no match is found - std::string GetAbbreviation(const std::string& abbrev) const; + const std::string& GetAbbreviation(const std::string& abbrev) const; //! Will check wether or not an abbreviation is registered bool HasAbbreviation(const std::string& abbrev) const; @@ -85,7 +85,7 @@ namespace Hazelnp //! Will return a short description for a parameter, if it exists. //! Empty string if it does not exist. - std::string GetDescription(const std::string& parameter) const; + const std::string& GetDescription(const std::string& parameter) const; //! Returns whether or not a given parameter has a registered description bool HasDescription(const std::string& parameter) const; diff --git a/Hazelnupp/Placeholders.h b/Hazelnupp/Placeholders.h new file mode 100644 index 0000000..fe1d343 --- /dev/null +++ b/Hazelnupp/Placeholders.h @@ -0,0 +1,11 @@ +#pragma once +#include + +namespace Hazelnp +{ + namespace Placeholders + { + //! The only purpose of this is to provide the ability to return an empty string as an error for std::string& methods. + static const std::string g_emptyString; + } +} diff --git a/Hazelnupp/VoidValue.cpp b/Hazelnupp/VoidValue.cpp index a3d4c86..3e52b79 100644 --- a/Hazelnupp/VoidValue.cpp +++ b/Hazelnupp/VoidValue.cpp @@ -44,10 +44,11 @@ double VoidValue::GetFloat32() const std::string VoidValue::GetString() const { - throw HazelnuppValueNotConvertibleException(); + return ""; } const std::vector& VoidValue::GetList() const { - throw HazelnuppValueNotConvertibleException(); + static const std::vector empty; + return empty; } diff --git a/Hazelnupp/VoidValue.h b/Hazelnupp/VoidValue.h index 96a81ae..a67a503 100644 --- a/Hazelnupp/VoidValue.h +++ b/Hazelnupp/VoidValue.h @@ -27,10 +27,10 @@ namespace Hazelnp //! Throws HazelnuppValueNotConvertibleException double GetFloat32() const override; - //! Throws HazelnuppValueNotConvertibleException + //! Returns an empty string std::string GetString() const override; - //! Throws HazelnuppValueNotConvertibleException + //! Returns an empty list const std::vector& GetList() const; }; } diff --git a/Test_Hazelnupp/Conversion.cpp b/Test_Hazelnupp/Conversion.cpp index b7b7657..8c92c1d 100644 --- a/Test_Hazelnupp/Conversion.cpp +++ b/Test_Hazelnupp/Conversion.cpp @@ -112,20 +112,11 @@ namespace TestHazelnupp } , L"Float32"); - Assert::ExpectException( - [ptnupp] - { - (*ptnupp)["--pud"].GetString(); - } - , L"String"); - - Assert::ExpectException( - [ptnupp] - { - (*ptnupp)["--pud"].GetList(); - } - , L"List"); + // Expect empty string + Assert::AreEqual(std::string(), nupp["--pud"].GetString(), L"String"); + // Expect empty list + Assert::AreEqual(std::size_t(0), nupp["--pud"].GetList().size(), L"List"); return; }