cmCacheManager::GetProperty: return cmProp

This commit is contained in:
Vitaly Stakhovsky 2020-03-19 08:00:00 -04:00
parent 60db3af147
commit 0bf0e35e59
3 changed files with 28 additions and 32 deletions

View File

@ -124,13 +124,13 @@ bool cmCacheManager::LoadCache(const std::string& path, bool internal,
} }
this->CacheMajorVersion = 0; this->CacheMajorVersion = 0;
this->CacheMinorVersion = 0; this->CacheMinorVersion = 0;
if (const std::string* cmajor = if (cmProp cmajor =
this->GetInitializedCacheValue("CMAKE_CACHE_MAJOR_VERSION")) { this->GetInitializedCacheValue("CMAKE_CACHE_MAJOR_VERSION")) {
unsigned int v = 0; unsigned int v = 0;
if (sscanf(cmajor->c_str(), "%u", &v) == 1) { if (sscanf(cmajor->c_str(), "%u", &v) == 1) {
this->CacheMajorVersion = v; this->CacheMajorVersion = v;
} }
if (const std::string* cminor = if (cmProp cminor =
this->GetInitializedCacheValue("CMAKE_CACHE_MINOR_VERSION")) { this->GetInitializedCacheValue("CMAKE_CACHE_MINOR_VERSION")) {
if (sscanf(cminor->c_str(), "%u", &v) == 1) { if (sscanf(cminor->c_str(), "%u", &v) == 1) {
this->CacheMinorVersion = v; this->CacheMinorVersion = v;
@ -150,8 +150,7 @@ bool cmCacheManager::LoadCache(const std::string& path, bool internal,
} }
// check to make sure the cache directory has not // check to make sure the cache directory has not
// been moved // been moved
const std::string* oldDir = cmProp oldDir = this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
if (internal && oldDir) { if (internal && oldDir) {
std::string currentcwd = path; std::string currentcwd = path;
std::string oldcwd = *oldDir; std::string oldcwd = *oldDir;
@ -159,8 +158,7 @@ bool cmCacheManager::LoadCache(const std::string& path, bool internal,
currentcwd += "/CMakeCache.txt"; currentcwd += "/CMakeCache.txt";
oldcwd += "/CMakeCache.txt"; oldcwd += "/CMakeCache.txt";
if (!cmSystemTools::SameFile(oldcwd, currentcwd)) { if (!cmSystemTools::SameFile(oldcwd, currentcwd)) {
const std::string* dir = cmProp dir = this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
std::ostringstream message; std::ostringstream message;
message << "The current CMakeCache.txt directory " << currentcwd message << "The current CMakeCache.txt directory " << currentcwd
<< " is different than the directory " << (dir ? *dir : "") << " is different than the directory " << (dir ? *dir : "")
@ -210,7 +208,7 @@ void cmCacheManager::WritePropertyEntries(std::ostream& os, CacheIterator i,
cmMessenger* messenger) cmMessenger* messenger)
{ {
for (const char** p = cmCacheManager::PersistentProperties; *p; ++p) { for (const char** p = cmCacheManager::PersistentProperties; *p; ++p) {
if (const char* value = i.GetProperty(*p)) { if (cmProp value = i.GetProperty(*p)) {
std::string helpstring = std::string helpstring =
cmStrCat(*p, " property for variable: ", i.GetName()); cmStrCat(*p, " property for variable: ", i.GetName());
cmCacheManager::OutputHelpString(os, helpstring); cmCacheManager::OutputHelpString(os, helpstring);
@ -218,9 +216,9 @@ void cmCacheManager::WritePropertyEntries(std::ostream& os, CacheIterator i,
std::string key = cmStrCat(i.GetName(), '-', *p); std::string key = cmStrCat(i.GetName(), '-', *p);
cmCacheManager::OutputKey(os, key); cmCacheManager::OutputKey(os, key);
os << ":INTERNAL="; os << ":INTERNAL=";
cmCacheManager::OutputValue(os, value); cmCacheManager::OutputValue(os, *value);
os << "\n"; os << "\n";
cmCacheManager::OutputNewlineTruncationWarning(os, key, value, cmCacheManager::OutputNewlineTruncationWarning(os, key, *value,
messenger); messenger);
} }
} }
@ -305,8 +303,8 @@ bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger)
*/ */
} else if (t != cmStateEnums::INTERNAL) { } else if (t != cmStateEnums::INTERNAL) {
// Format is key:type=value // Format is key:type=value
if (const char* help = ce.GetProperty("HELPSTRING")) { if (cmProp help = ce.GetProperty("HELPSTRING")) {
cmCacheManager::OutputHelpString(fout, help); cmCacheManager::OutputHelpString(fout, *help);
} else { } else {
cmCacheManager::OutputHelpString(fout, "Missing description"); cmCacheManager::OutputHelpString(fout, "Missing description");
} }
@ -336,8 +334,8 @@ bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger)
this->WritePropertyEntries(fout, i, messenger); this->WritePropertyEntries(fout, i, messenger);
if (t == cmStateEnums::INTERNAL) { if (t == cmStateEnums::INTERNAL) {
// Format is key:type=value // Format is key:type=value
if (const char* help = i.GetProperty("HELPSTRING")) { if (cmProp help = i.GetProperty("HELPSTRING")) {
cmCacheManager::OutputHelpString(fout, help); cmCacheManager::OutputHelpString(fout, *help);
} }
cmCacheManager::OutputKey(fout, i.GetName()); cmCacheManager::OutputKey(fout, i.GetName());
fout << ":" << cmState::CacheEntryTypeToString(t) << "="; fout << ":" << cmState::CacheEntryTypeToString(t) << "=";
@ -508,8 +506,7 @@ cmCacheManager::CacheIterator cmCacheManager::GetCacheIterator()
return { *this, nullptr }; return { *this, nullptr };
} }
const std::string* cmCacheManager::GetInitializedCacheValue( cmProp cmCacheManager::GetInitializedCacheValue(const std::string& key) const
const std::string& key) const
{ {
auto i = this->Cache.find(key); auto i = this->Cache.find(key);
if (i != this->Cache.end() && i->second.Initialized) { if (i != this->Cache.end() && i->second.Initialized) {
@ -619,17 +616,15 @@ std::vector<std::string> cmCacheManager::CacheEntry::GetPropertyList() const
return this->Properties.GetKeys(); return this->Properties.GetKeys();
} }
const char* cmCacheManager::CacheEntry::GetProperty( cmProp cmCacheManager::CacheEntry::GetProperty(const std::string& prop) const
const std::string& prop) const
{ {
if (prop == "TYPE") { if (prop == "TYPE") {
return cmState::CacheEntryTypeToString(this->Type).c_str(); return &cmState::CacheEntryTypeToString(this->Type);
} }
if (prop == "VALUE") { if (prop == "VALUE") {
return this->Value.c_str(); return &this->Value;
} }
cmProp retVal = this->Properties.GetPropertyValue(prop); return 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,
@ -663,7 +658,7 @@ void cmCacheManager::CacheEntry::AppendProperty(const std::string& prop,
} }
} }
const char* cmCacheManager::CacheIterator::GetProperty( cmProp cmCacheManager::CacheIterator::GetProperty(
const std::string& prop) const const std::string& prop) const
{ {
if (!this->IsAtEnd()) { if (!this->IsAtEnd()) {
@ -692,8 +687,8 @@ void cmCacheManager::CacheIterator::AppendProperty(const std::string& p,
bool cmCacheManager::CacheIterator::GetPropertyAsBool( bool cmCacheManager::CacheIterator::GetPropertyAsBool(
const std::string& prop) const const std::string& prop) const
{ {
if (const char* value = this->GetProperty(prop)) { if (cmProp value = this->GetProperty(prop)) {
return cmIsOn(value); return cmIsOn(*value);
} }
return false; return false;
} }

View File

@ -37,7 +37,7 @@ private:
cmStateEnums::CacheEntryType Type = cmStateEnums::UNINITIALIZED; cmStateEnums::CacheEntryType Type = cmStateEnums::UNINITIALIZED;
cmPropertyMap Properties; cmPropertyMap Properties;
std::vector<std::string> GetPropertyList() const; std::vector<std::string> GetPropertyList() const;
const char* GetProperty(const std::string&) const; cmProp GetProperty(const std::string&) const;
void SetProperty(const std::string& property, const char* value); void SetProperty(const std::string& property, const char* value);
void AppendProperty(const std::string& property, const std::string& value, void AppendProperty(const std::string& property, const std::string& value,
bool asString = false); bool asString = false);
@ -54,7 +54,7 @@ public:
void Next(); void Next();
std::string GetName() const { return this->Position->first; } std::string GetName() const { return this->Position->first; }
std::vector<std::string> GetPropertyList() const; std::vector<std::string> GetPropertyList() const;
const char* GetProperty(const std::string&) const; cmProp GetProperty(const std::string&) const;
bool GetPropertyAsBool(const std::string&) const; bool GetPropertyAsBool(const std::string&) const;
bool PropertyExists(const std::string&) const; bool PropertyExists(const std::string&) const;
void SetProperty(const std::string& property, const char* value); void SetProperty(const std::string& property, const char* value);
@ -121,19 +121,19 @@ public:
int GetSize() { return static_cast<int>(this->Cache.size()); } int GetSize() { return static_cast<int>(this->Cache.size()); }
//! Get a value from the cache given a key //! Get a value from the cache given a key
const std::string* GetInitializedCacheValue(const std::string& key) const; cmProp GetInitializedCacheValue(const std::string& key) const;
const char* GetCacheEntryValue(const std::string& key) cmProp GetCacheEntryValue(const std::string& key)
{ {
cmCacheManager::CacheIterator it = this->GetCacheIterator(key); cmCacheManager::CacheIterator it = this->GetCacheIterator(key);
if (it.IsAtEnd()) { if (it.IsAtEnd()) {
return nullptr; return nullptr;
} }
return it.GetValue().c_str(); return &it.GetValue();
} }
const char* GetCacheEntryProperty(std::string const& key, cmProp GetCacheEntryProperty(std::string const& key,
std::string const& propName) std::string const& propName)
{ {
return this->GetCacheIterator(key).GetProperty(propName); return this->GetCacheIterator(key).GetProperty(propName);
} }

View File

@ -199,7 +199,8 @@ const char* cmState::GetCacheEntryProperty(std::string const& key,
if (!it.PropertyExists(propertyName)) { if (!it.PropertyExists(propertyName)) {
return nullptr; return nullptr;
} }
return it.GetProperty(propertyName); cmProp retVal = it.GetProperty(propertyName);
return retVal ? retVal->c_str() : nullptr;
} }
bool cmState::GetCacheEntryPropertyAsBool(std::string const& key, bool cmState::GetCacheEntryPropertyAsBool(std::string const& key,