1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-19 19:43:23 +08:00

cmPropertyMap: Introduce cmProp as return type for GetProperty() functions

Currently properties are usually stored internally as `std::string`.
However, family of GetProperty() functions return them as `const char *` using `c_str()`.
The proposed `cmProp`, typedef'ed as `const std::string *` will expose properties
more naturally.
This commit is contained in:
Vitaly Stakhovsky
2020-03-13 13:30:00 -04:00
parent 3766633b8a
commit 60f57d0dcc
12 changed files with 35 additions and 28 deletions

View File

@@ -585,7 +585,8 @@ const char* CCONV cmSourceFileGetProperty(void* arg, const char* prop)
if (!strcmp(prop, "LOCATION")) { if (!strcmp(prop, "LOCATION")) {
return sf->FullPath.c_str(); return sf->FullPath.c_str();
} }
return sf->Properties.GetPropertyValue(prop); cmProp retVal = sf->Properties.GetPropertyValue(prop);
return retVal ? retVal->c_str() : nullptr;
} }
int CCONV cmSourceFileGetPropertyAsBool(void* arg, const char* prop) int CCONV cmSourceFileGetPropertyAsBool(void* arg, const char* prop)

View File

@@ -628,7 +628,8 @@ const char* cmCacheManager::CacheEntry::GetProperty(
if (prop == "VALUE") { if (prop == "VALUE") {
return this->Value.c_str(); return this->Value.c_str();
} }
return this->Properties.GetPropertyValue(prop); cmProp retVal = this->Properties.GetPropertyValue(prop);
return retVal ? retVal->c_str() : nullptr;
} }
void cmCacheManager::CacheEntry::SetProperty(const std::string& prop, void cmCacheManager::CacheEntry::SetProperty(const std::string& prop,

View File

@@ -1215,9 +1215,9 @@ bool cmExportFileGenerator::PopulateExportProperties(
std::string& errorMessage) std::string& errorMessage)
{ {
auto& targetProperties = gte->Target->GetProperties(); auto& targetProperties = gte->Target->GetProperties();
if (const char* exportProperties = if (cmProp exportProperties =
targetProperties.GetPropertyValue("EXPORT_PROPERTIES")) { targetProperties.GetPropertyValue("EXPORT_PROPERTIES")) {
for (auto& prop : cmExpandedList(exportProperties)) { for (auto& prop : cmExpandedList(*exportProperties)) {
/* Black list reserved properties */ /* Black list reserved properties */
if (cmHasLiteralPrefix(prop, "IMPORTED_") || if (cmHasLiteralPrefix(prop, "IMPORTED_") ||
cmHasLiteralPrefix(prop, "INTERFACE_")) { cmHasLiteralPrefix(prop, "INTERFACE_")) {
@@ -1228,15 +1228,15 @@ bool cmExportFileGenerator::PopulateExportProperties(
errorMessage = e.str(); errorMessage = e.str();
return false; return false;
} }
auto propertyValue = targetProperties.GetPropertyValue(prop); cmProp propertyValue = targetProperties.GetPropertyValue(prop);
if (propertyValue == nullptr) { if (propertyValue == nullptr) {
// Asked to export a property that isn't defined on the target. Do not // Asked to export a property that isn't defined on the target. Do not
// consider this an error, there's just nothing to export. // consider this an error, there's just nothing to export.
continue; continue;
} }
std::string evaluatedValue = cmGeneratorExpression::Preprocess( std::string evaluatedValue = cmGeneratorExpression::Preprocess(
propertyValue, cmGeneratorExpression::StripAllGeneratorExpressions); *propertyValue, cmGeneratorExpression::StripAllGeneratorExpressions);
if (evaluatedValue != propertyValue) { if (evaluatedValue != *propertyValue) {
std::ostringstream e; std::ostringstream e;
e << "Target \"" << gte->Target->GetName() << "\" contains property \"" e << "Target \"" << gte->Target->GetName() << "\" contains property \""
<< prop << "\" in EXPORT_PROPERTIES but this property contains a " << prop << "\" in EXPORT_PROPERTIES but this property contains a "
@@ -1244,7 +1244,7 @@ bool cmExportFileGenerator::PopulateExportProperties(
errorMessage = e.str(); errorMessage = e.str();
return false; return false;
} }
properties[prop] = propertyValue; properties[prop] = *propertyValue;
} }
} }
return true; return true;

View File

@@ -42,13 +42,11 @@ void cmPropertyMap::RemoveProperty(const std::string& name)
Map_.erase(name); Map_.erase(name);
} }
const char* cmPropertyMap::GetPropertyValue(const std::string& name) const cmProp cmPropertyMap::GetPropertyValue(const std::string& name) const
{ {
{ auto it = Map_.find(name);
auto it = Map_.find(name); if (it != Map_.end()) {
if (it != Map_.end()) { return &it->second;
return it->second.c_str();
}
} }
return nullptr; return nullptr;
} }

View File

@@ -10,6 +10,8 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
using cmProp = const std::string*;
/** \class cmPropertyMap /** \class cmPropertyMap
* \brief String property map. * \brief String property map.
*/ */
@@ -31,7 +33,7 @@ public:
bool asString = false); bool asString = false);
//! Get the property value //! Get the property value
const char* GetPropertyValue(const std::string& name) const; cmProp GetPropertyValue(const std::string& name) const;
//! Remove the property @a name from the map //! Remove the property @a name from the map
void RemoveProperty(const std::string& name); void RemoveProperty(const std::string& name);

View File

@@ -361,7 +361,7 @@ const char* cmSourceFile::GetProperty(const std::string& prop) const
return output.c_str(); return output.c_str();
} }
const char* retVal = this->Properties.GetPropertyValue(prop); cmProp retVal = this->Properties.GetPropertyValue(prop);
if (!retVal) { if (!retVal) {
cmMakefile const* mf = this->Location.GetMakefile(); cmMakefile const* mf = this->Location.GetMakefile();
const bool chain = const bool chain =
@@ -369,9 +369,10 @@ const char* cmSourceFile::GetProperty(const std::string& prop) const
if (chain) { if (chain) {
return mf->GetProperty(prop, chain); return mf->GetProperty(prop, chain);
} }
return nullptr;
} }
return retVal; return retVal->c_str();
} }
const char* cmSourceFile::GetSafeProperty(const std::string& prop) const const char* cmSourceFile::GetSafeProperty(const std::string& prop) const

View File

@@ -625,7 +625,8 @@ const char* cmState::GetGlobalProperty(const std::string& prop)
} }
#undef STRING_LIST_ELEMENT #undef STRING_LIST_ELEMENT
return this->GlobalProperties.GetPropertyValue(prop); cmProp retVal = this->GlobalProperties.GetPropertyValue(prop);
return retVal ? retVal->c_str() : nullptr;
} }
bool cmState::GetGlobalPropertyAsBool(const std::string& prop) bool cmState::GetGlobalPropertyAsBool(const std::string& prop)

View File

@@ -634,7 +634,7 @@ const char* cmStateDirectory::GetProperty(const std::string& prop,
return output.c_str(); return output.c_str();
} }
const char* retVal = this->DirectoryState->Properties.GetPropertyValue(prop); cmProp retVal = this->DirectoryState->Properties.GetPropertyValue(prop);
if (!retVal && chain) { if (!retVal && chain) {
cmStateSnapshot parentSnapshot = cmStateSnapshot parentSnapshot =
this->Snapshot_.GetBuildsystemDirectoryParent(); this->Snapshot_.GetBuildsystemDirectoryParent();
@@ -644,7 +644,7 @@ const char* cmStateDirectory::GetProperty(const std::string& prop,
return this->Snapshot_.State->GetGlobalProperty(prop); return this->Snapshot_.State->GetGlobalProperty(prop);
} }
return retVal; return retVal ? retVal->c_str() : nullptr;
} }
bool cmStateDirectory::GetPropertyAsBool(const std::string& prop) const bool cmStateDirectory::GetPropertyAsBool(const std::string& prop) const

View File

@@ -411,11 +411,12 @@ void cmStateSnapshot::InitializeFromParent()
this->Position->BuildSystemDirectory->LinkDirectoriesBacktraces, this->Position->BuildSystemDirectory->LinkDirectoriesBacktraces,
this->Position->LinkDirectoriesPosition); this->Position->LinkDirectoriesPosition);
const char* include_regex = cmProp include_regex =
parent->BuildSystemDirectory->Properties.GetPropertyValue( parent->BuildSystemDirectory->Properties.GetPropertyValue(
"INCLUDE_REGULAR_EXPRESSION"); "INCLUDE_REGULAR_EXPRESSION");
this->Position->BuildSystemDirectory->Properties.SetProperty( this->Position->BuildSystemDirectory->Properties.SetProperty(
"INCLUDE_REGULAR_EXPRESSION", include_regex); "INCLUDE_REGULAR_EXPRESSION",
include_regex ? include_regex->c_str() : nullptr);
} }
cmState* cmStateSnapshot::GetState() const cmState* cmStateSnapshot::GetState() const

View File

@@ -1771,7 +1771,7 @@ const char* cmTarget::GetProperty(const std::string& prop) const
} }
} }
const char* retVal = impl->Properties.GetPropertyValue(prop); cmProp retVal = impl->Properties.GetPropertyValue(prop);
if (!retVal) { if (!retVal) {
const bool chain = const bool chain =
impl->Makefile->GetState()->IsPropertyChained(prop, cmProperty::TARGET); impl->Makefile->GetState()->IsPropertyChained(prop, cmProperty::TARGET);
@@ -1779,8 +1779,9 @@ const char* cmTarget::GetProperty(const std::string& prop) const
return impl->Makefile->GetStateSnapshot().GetDirectory().GetProperty( return impl->Makefile->GetStateSnapshot().GetDirectory().GetProperty(
prop, chain); prop, chain);
} }
return nullptr;
} }
return retVal; return retVal->c_str();
} }
const char* cmTarget::GetSafeProperty(const std::string& prop) const const char* cmTarget::GetSafeProperty(const std::string& prop) const

View File

@@ -34,15 +34,16 @@ void cmTest::SetCommand(std::vector<std::string> const& command)
const char* cmTest::GetProperty(const std::string& prop) const const char* cmTest::GetProperty(const std::string& prop) const
{ {
const char* retVal = this->Properties.GetPropertyValue(prop); cmProp retVal = this->Properties.GetPropertyValue(prop);
if (!retVal) { if (!retVal) {
const bool chain = const bool chain =
this->Makefile->GetState()->IsPropertyChained(prop, cmProperty::TEST); this->Makefile->GetState()->IsPropertyChained(prop, cmProperty::TEST);
if (chain) { if (chain) {
return this->Makefile->GetProperty(prop, chain); return this->Makefile->GetProperty(prop, chain);
} }
return nullptr;
} }
return retVal; return retVal->c_str();
} }
bool cmTest::GetPropertyAsBool(const std::string& prop) const bool cmTest::GetPropertyAsBool(const std::string& prop) const

View File

@@ -1017,7 +1017,7 @@ void cmVisualStudio10TargetGenerator::WriteEmbeddedResourceGroup(Elem& e0)
if (p.find(propNamePrefix) == 0) { if (p.find(propNamePrefix) == 0) {
std::string tagName = p.substr(propNamePrefix.length()); std::string tagName = p.substr(propNamePrefix.length());
if (!tagName.empty()) { if (!tagName.empty()) {
std::string value = props.GetPropertyValue(p); const std::string& value = *props.GetPropertyValue(p);
if (!value.empty()) { if (!value.empty()) {
e2.Element(tagName, value); e2.Element(tagName, value);
} }
@@ -4802,7 +4802,7 @@ void cmVisualStudio10TargetGenerator::GetCSharpSourceProperties(
if (p.find(propNamePrefix) == 0) { if (p.find(propNamePrefix) == 0) {
std::string tagName = p.substr(propNamePrefix.length()); std::string tagName = p.substr(propNamePrefix.length());
if (!tagName.empty()) { if (!tagName.empty()) {
const std::string val = props.GetPropertyValue(p); const std::string& val = *props.GetPropertyValue(p);
if (!val.empty()) { if (!val.empty()) {
tags[tagName] = val; tags[tagName] = val;
} else { } else {