Minor optimization

This commit is contained in:
Leonetienne 2021-06-05 12:29:59 +02:00
parent 5efc4d5533
commit 275ce060f0
6 changed files with 27 additions and 23 deletions

View File

@ -5,6 +5,7 @@
#include "StringValue.h" #include "StringValue.h"
#include "ListValue.h" #include "ListValue.h"
#include "HazelnuppException.h" #include "HazelnuppException.h"
#include "Placeholders.h"
#include "StringTools.h" #include "StringTools.h"
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
@ -320,12 +321,12 @@ void Hazelnp::Hazelnupp::RegisterDescription(const std::string& parameter, const
return; 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? // Do we already have a description for this parameter?
if (!HasDescription(parameter)) if (!HasDescription(parameter))
// No? Then return "" // No? Then return ""
return ""; return Placeholders::g_emptyString;
// We do? Then return it // We do? Then return it
return parameterDescriptions.find(parameter)->second; return parameterDescriptions.find(parameter)->second;
@ -523,10 +524,10 @@ void Hazelnupp::RegisterAbbreviation(const std::string& abbrev, const std::strin
return; return;
} }
std::string Hazelnupp::GetAbbreviation(const std::string& abbrev) const const std::string& Hazelnupp::GetAbbreviation(const std::string& abbrev) const
{ {
if (!HasAbbreviation(abbrev)) if (!HasAbbreviation(abbrev))
return ""; return Placeholders::g_emptyString;
return parameterAbreviations.find(abbrev)->second; return parameterAbreviations.find(abbrev)->second;
} }

View File

@ -34,7 +34,7 @@ namespace Hazelnp
//! Will return the long form of an abbreviation (like --force for -f) //! Will return the long form of an abbreviation (like --force for -f)
//! Returns "" if no match is found //! 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 //! Will check wether or not an abbreviation is registered
bool HasAbbreviation(const std::string& abbrev) const; bool HasAbbreviation(const std::string& abbrev) const;
@ -85,7 +85,7 @@ namespace Hazelnp
//! Will return a short description for a parameter, if it exists. //! Will return a short description for a parameter, if it exists.
//! Empty string if it does not exist. //! 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 //! Returns whether or not a given parameter has a registered description
bool HasDescription(const std::string& parameter) const; bool HasDescription(const std::string& parameter) const;

11
Hazelnupp/Placeholders.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <string>
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;
}
}

View File

@ -44,10 +44,11 @@ double VoidValue::GetFloat32() const
std::string VoidValue::GetString() const std::string VoidValue::GetString() const
{ {
throw HazelnuppValueNotConvertibleException(); return "";
} }
const std::vector<Value*>& VoidValue::GetList() const const std::vector<Value*>& VoidValue::GetList() const
{ {
throw HazelnuppValueNotConvertibleException(); static const std::vector<Value*> empty;
return empty;
} }

View File

@ -27,10 +27,10 @@ namespace Hazelnp
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
double GetFloat32() const override; double GetFloat32() const override;
//! Throws HazelnuppValueNotConvertibleException //! Returns an empty string
std::string GetString() const override; std::string GetString() const override;
//! Throws HazelnuppValueNotConvertibleException //! Returns an empty list
const std::vector<Value*>& GetList() const; const std::vector<Value*>& GetList() const;
}; };
} }

View File

@ -112,20 +112,11 @@ namespace TestHazelnupp
} }
, L"Float32"); , L"Float32");
Assert::ExpectException<HazelnuppValueNotConvertibleException>( // Expect empty string
[ptnupp] Assert::AreEqual(std::string(), nupp["--pud"].GetString(), L"String");
{
(*ptnupp)["--pud"].GetString();
}
, L"String");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptnupp]
{
(*ptnupp)["--pud"].GetList();
}
, L"List");
// Expect empty list
Assert::AreEqual(std::size_t(0), nupp["--pud"].GetList().size(), L"List");
return; return;
} }