cmProp: cm::string_view cast operator must be explicit

To avoid ambiguity on std::string assigment between the following two
cmProp cast operators:
* operator const std::string&() const noexcept
* operator cm::string_view() const noexcept
This commit is contained in:
Marc Chevrier
2021-09-13 16:55:00 +02:00
parent 69c0a5daf9
commit 79362cf117
6 changed files with 39 additions and 9 deletions

View File

@@ -167,7 +167,7 @@ int cmCPackGenerator::PrepareNames()
} }
cmProp algoSignature = this->GetOption("CPACK_PACKAGE_CHECKSUM"); cmProp algoSignature = this->GetOption("CPACK_PACKAGE_CHECKSUM");
if (algoSignature) { if (algoSignature) {
if (!cmCryptoHash::New(algoSignature)) { if (!cmCryptoHash::New(*algoSignature)) {
cmCPackLogger(cmCPackLog::LOG_ERROR, cmCPackLogger(cmCPackLog::LOG_ERROR,
"Cannot recognize algorithm: " << algoSignature "Cannot recognize algorithm: " << algoSignature
<< std::endl); << std::endl);
@@ -1129,7 +1129,7 @@ int cmCPackGenerator::DoPackage()
/* Prepare checksum algorithm*/ /* Prepare checksum algorithm*/
cmProp algo = this->GetOption("CPACK_PACKAGE_CHECKSUM"); cmProp algo = this->GetOption("CPACK_PACKAGE_CHECKSUM");
std::unique_ptr<cmCryptoHash> crypto = cmCryptoHash::New(algo); std::unique_ptr<cmCryptoHash> crypto = cmCryptoHash::New(*algo);
/* /*
* Copy the generated packages to final destination * Copy the generated packages to final destination

View File

@@ -606,7 +606,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
std::string langFlags = "CMAKE_" + li + "_FLAGS"; std::string langFlags = "CMAKE_" + li + "_FLAGS";
cmProp flags = this->Makefile->GetDefinition(langFlags); cmProp flags = this->Makefile->GetDefinition(langFlags);
fprintf(fout, "set(CMAKE_%s_FLAGS %s)\n", li.c_str(), fprintf(fout, "set(CMAKE_%s_FLAGS %s)\n", li.c_str(),
cmOutputConverter::EscapeForCMake(flags).c_str()); cmOutputConverter::EscapeForCMake(*flags).c_str());
fprintf(fout, fprintf(fout,
"set(CMAKE_%s_FLAGS \"${CMAKE_%s_FLAGS}" "set(CMAKE_%s_FLAGS \"${CMAKE_%s_FLAGS}"
" ${COMPILE_DEFINITIONS}\")\n", " ${COMPILE_DEFINITIONS}\")\n",
@@ -645,7 +645,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
cmStrCat("CMAKE_", li, "_FLAGS_", cfg); cmStrCat("CMAKE_", li, "_FLAGS_", cfg);
cmProp flagsCfg = this->Makefile->GetDefinition(langFlagsCfg); cmProp flagsCfg = this->Makefile->GetDefinition(langFlagsCfg);
fprintf(fout, "set(%s %s)\n", langFlagsCfg.c_str(), fprintf(fout, "set(%s %s)\n", langFlagsCfg.c_str(),
cmOutputConverter::EscapeForCMake(flagsCfg).c_str()); cmOutputConverter::EscapeForCMake(*flagsCfg).c_str());
} }
} break; } break;
} }
@@ -678,7 +678,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
cmProp exeLinkFlags = cmProp exeLinkFlags =
this->Makefile->GetDefinition("CMAKE_EXE_LINKER_FLAGS"); this->Makefile->GetDefinition("CMAKE_EXE_LINKER_FLAGS");
fprintf(fout, "set(CMAKE_EXE_LINKER_FLAGS %s)\n", fprintf(fout, "set(CMAKE_EXE_LINKER_FLAGS %s)\n",
cmOutputConverter::EscapeForCMake(exeLinkFlags).c_str()); cmOutputConverter::EscapeForCMake(*exeLinkFlags).c_str());
} }
break; break;
} }

View File

@@ -286,6 +286,10 @@ public:
* can be used in CMake to refer to lists, directories, etc. * can be used in CMake to refer to lists, directories, etc.
*/ */
void AddDefinition(const std::string& name, cm::string_view value); void AddDefinition(const std::string& name, cm::string_view value);
void AddDefinition(const std::string& name, cmProp value)
{
this->AddDefinition(name, *value);
}
/** /**
* Add bool variable definition to the build. * Add bool variable definition to the build.
*/ */

View File

@@ -66,7 +66,10 @@ public:
explicit operator bool() const noexcept { return this->Value != nullptr; } explicit operator bool() const noexcept { return this->Value != nullptr; }
operator const std::string&() const noexcept { return this->operator*(); } operator const std::string&() const noexcept { return this->operator*(); }
operator cm::string_view() const noexcept { return this->operator*(); } explicit operator cm::string_view() const noexcept
{
return this->operator*();
}
/** /**
* Does the value indicate a true or ON value? * Does the value indicate a true or ON value?

View File

@@ -4,6 +4,7 @@
#include <algorithm> #include <algorithm>
#include <cm/string_view>
#include <cmext/string_view> #include <cmext/string_view>
#include "cmArgumentParser.h" #include "cmArgumentParser.h"
@@ -81,7 +82,7 @@ bool cmSeparateArgumentsCommand(std::vector<std::string> const& args,
} }
if (unparsedArguments.empty()) { if (unparsedArguments.empty()) {
status.GetMakefile().AddDefinition(var, {}); status.GetMakefile().AddDefinition(var, cm::string_view{});
return true; return true;
} }

View File

@@ -94,6 +94,13 @@ std::vector<std::string> cmTokenize(cm::string_view str, cm::string_view sep);
*/ */
void cmExpandList(cm::string_view arg, std::vector<std::string>& argsOut, void cmExpandList(cm::string_view arg, std::vector<std::string>& argsOut,
bool emptyArgs = false); bool emptyArgs = false);
inline void cmExpandList(cmProp arg, std::vector<std::string>& argsOut,
bool emptyArgs = false)
{
if (arg) {
cmExpandList(*arg, argsOut, emptyArgs);
}
}
/** /**
* Expand out any arguments in the string range [@a first, @a last) that have * Expand out any arguments in the string range [@a first, @a last) that have
@@ -115,6 +122,14 @@ void cmExpandLists(InputIt first, InputIt last,
*/ */
std::vector<std::string> cmExpandedList(cm::string_view arg, std::vector<std::string> cmExpandedList(cm::string_view arg,
bool emptyArgs = false); bool emptyArgs = false);
inline std::vector<std::string> cmExpandedList(cmProp arg,
bool emptyArgs = false)
{
if (!arg) {
return {};
}
return cmExpandedList(*arg, emptyArgs);
}
/** /**
* Same as cmExpandList but a new vector is created containing the expanded * Same as cmExpandList but a new vector is created containing the expanded
@@ -217,6 +232,13 @@ inline bool cmIsInternallyOn(const char* val)
} }
return cmIsInternallyOn(cm::string_view(val)); return cmIsInternallyOn(cm::string_view(val));
} }
inline bool cmIsInternallyOn(cmProp val)
{
if (!val) {
return false;
}
return cmIsInternallyOn(*val);
}
/** Check for non-empty Property/Variable value. */ /** Check for non-empty Property/Variable value. */
inline bool cmNonempty(cm::string_view val) inline bool cmNonempty(cm::string_view val)
@@ -297,7 +319,7 @@ inline bool cmHasPrefix(cm::string_view str, cmProp prefix)
return false; return false;
} }
return str.compare(0, prefix->size(), prefix) == 0; return str.compare(0, prefix->size(), *prefix) == 0;
} }
/** Returns true if string @a str starts with string @a prefix. */ /** Returns true if string @a str starts with string @a prefix. */
@@ -328,7 +350,7 @@ inline bool cmHasSuffix(cm::string_view str, cmProp suffix)
} }
return str.size() >= suffix->size() && return str.size() >= suffix->size() &&
str.compare(str.size() - suffix->size(), suffix->size(), suffix) == 0; str.compare(str.size() - suffix->size(), suffix->size(), *suffix) == 0;
} }
/** Returns true if string @a str ends with string @a suffix. */ /** Returns true if string @a str ends with string @a suffix. */