cmCacheManager: Cleanup CacheIterator interface

- Expose required functionality from CacheEntry.
- Modify usage in cmState member functions.
- Remove cmState access to CacheEntry members.
This commit is contained in:
Tushar Maheshwari
2020-04-17 17:01:53 +05:30
parent df2d39bc51
commit 80d8b20351
3 changed files with 157 additions and 291 deletions

View File

@@ -19,12 +19,6 @@
#include "cmSystemTools.h"
#include "cmVersion.h"
cmCacheManager::cmCacheManager()
{
this->CacheMajorVersion = 0;
this->CacheMinorVersion = 0;
}
void cmCacheManager::CleanCMakeFiles(const std::string& path)
{
std::string glob = cmStrCat(path, "/CMakeFiles/*.cmake");
@@ -172,10 +166,10 @@ bool cmCacheManager::LoadCache(const std::string& path, bool internal,
}
const char* cmCacheManager::PersistentProperties[] = { "ADVANCED", "MODIFIED",
"STRINGS", nullptr };
"STRINGS" };
bool cmCacheManager::ReadPropertyEntry(std::string const& entryKey,
CacheEntry& e)
bool cmCacheManager::ReadPropertyEntry(const std::string& entryKey,
const CacheEntry& e)
{
// All property entries are internal.
if (e.Type != cmStateEnums::INTERNAL) {
@@ -183,20 +177,18 @@ bool cmCacheManager::ReadPropertyEntry(std::string const& entryKey,
}
const char* end = entryKey.c_str() + entryKey.size();
for (const char** p = cmCacheManager::PersistentProperties; *p; ++p) {
std::string::size_type plen = strlen(*p) + 1;
for (const char* p : cmCacheManager::PersistentProperties) {
std::string::size_type plen = strlen(p) + 1;
if (entryKey.size() > plen && *(end - plen) == '-' &&
strcmp(end - plen + 1, *p) == 0) {
strcmp(end - plen + 1, p) == 0) {
std::string key = entryKey.substr(0, entryKey.size() - plen);
cmCacheManager::CacheIterator it = this->GetCacheIterator(key);
if (it.IsAtEnd()) {
if (auto entry = this->GetCacheEntry(key)) {
// Store this property on its entry.
entry->SetProperty(p, e.Value.c_str());
} else {
// Create an entry and store the property.
CacheEntry& ne = this->Cache[key];
ne.Type = cmStateEnums::UNINITIALIZED;
ne.SetProperty(*p, e.Value.c_str());
} else {
// Store this property on its entry.
it.SetProperty(*p, e.Value.c_str());
ne.SetProperty(p, e.Value.c_str());
}
return true;
}
@@ -204,16 +196,18 @@ bool cmCacheManager::ReadPropertyEntry(std::string const& entryKey,
return false;
}
void cmCacheManager::WritePropertyEntries(std::ostream& os, CacheIterator i,
cmMessenger* messenger)
void cmCacheManager::WritePropertyEntries(std::ostream& os,
const std::string& entryKey,
const CacheEntry& e,
cmMessenger* messenger) const
{
for (const char** p = cmCacheManager::PersistentProperties; *p; ++p) {
if (cmProp value = i.GetProperty(*p)) {
for (const char* p : cmCacheManager::PersistentProperties) {
if (cmProp value = e.GetProperty(p)) {
std::string helpstring =
cmStrCat(*p, " property for variable: ", i.GetName());
cmStrCat(p, " property for variable: ", entryKey);
cmCacheManager::OutputHelpString(os, helpstring);
std::string key = cmStrCat(i.GetName(), '-', *p);
std::string key = cmStrCat(entryKey, '-', p);
cmCacheManager::OutputKey(os, key);
os << ":INTERNAL=";
cmCacheManager::OutputValue(os, *value);
@@ -322,25 +316,24 @@ bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger)
"########################\n"
"\n";
for (cmCacheManager::CacheIterator i = this->NewIterator(); !i.IsAtEnd();
i.Next()) {
if (!i.Initialized()) {
for (auto const& i : this->Cache) {
if (!i.second.Initialized) {
continue;
}
cmStateEnums::CacheEntryType t = i.GetType();
this->WritePropertyEntries(fout, i, messenger);
cmStateEnums::CacheEntryType t = i.second.GetType();
this->WritePropertyEntries(fout, i.first, i.second, messenger);
if (t == cmStateEnums::INTERNAL) {
// Format is key:type=value
if (cmProp help = i.GetProperty("HELPSTRING")) {
if (cmProp help = i.second.GetProperty("HELPSTRING")) {
cmCacheManager::OutputHelpString(fout, *help);
}
cmCacheManager::OutputKey(fout, i.GetName());
cmCacheManager::OutputKey(fout, i.first);
fout << ':' << cmState::CacheEntryTypeToString(t) << '=';
cmCacheManager::OutputValue(fout, i.GetValue());
cmCacheManager::OutputValue(fout, i.second.GetValue());
fout << '\n';
cmCacheManager::OutputNewlineTruncationWarning(fout, i.GetName(),
i.GetValue(), messenger);
cmCacheManager::OutputNewlineTruncationWarning(
fout, i.first, i.second.GetValue(), messenger);
}
}
fout << '\n';
@@ -479,10 +472,7 @@ void cmCacheManager::OutputNewlineTruncationWarning(std::ostream& fout,
void cmCacheManager::RemoveCacheEntry(const std::string& key)
{
auto i = this->Cache.find(key);
if (i != this->Cache.end()) {
this->Cache.erase(i);
}
this->Cache.erase(key);
}
cmCacheManager::CacheEntry* cmCacheManager::GetCacheEntry(
@@ -495,22 +485,22 @@ cmCacheManager::CacheEntry* cmCacheManager::GetCacheEntry(
return nullptr;
}
cmCacheManager::CacheIterator cmCacheManager::GetCacheIterator(
const std::string& key)
const cmCacheManager::CacheEntry* cmCacheManager::GetCacheEntry(
const std::string& key) const
{
return { *this, key.c_str() };
}
cmCacheManager::CacheIterator cmCacheManager::GetCacheIterator()
{
return { *this, nullptr };
auto i = this->Cache.find(key);
if (i != this->Cache.end()) {
return &i->second;
}
return nullptr;
}
cmProp cmCacheManager::GetInitializedCacheValue(const std::string& key) const
{
auto i = this->Cache.find(key);
if (i != this->Cache.end() && i->second.Initialized) {
return &i->second.Value;
if (auto entry = this->GetCacheEntry(key)) {
if (entry->Initialized) {
return &entry->GetValue();
}
}
return nullptr;
}
@@ -535,12 +525,7 @@ void cmCacheManager::AddCacheEntry(const std::string& key, const char* value,
cmStateEnums::CacheEntryType type)
{
CacheEntry& e = this->Cache[key];
if (value) {
e.Value = value;
e.Initialized = true;
} else {
e.Value.clear();
}
e.SetValue(value);
e.Type = type;
// make sure we only use unix style paths
if (type == cmStateEnums::FILEPATH || type == cmStateEnums::PATH) {
@@ -564,53 +549,16 @@ void cmCacheManager::AddCacheEntry(const std::string& key, const char* value,
: "(This variable does not exist and should not be used)");
}
bool cmCacheManager::CacheIterator::IsAtEnd() const
void cmCacheManager::CacheEntry::SetValue(const char* value)
{
return this->Position == this->Container.Cache.end();
}
void cmCacheManager::CacheIterator::Begin()
{
this->Position = this->Container.Cache.begin();
}
bool cmCacheManager::CacheIterator::Find(const std::string& key)
{
this->Position = this->Container.Cache.find(key);
return !this->IsAtEnd();
}
void cmCacheManager::CacheIterator::Next()
{
if (!this->IsAtEnd()) {
++this->Position;
}
}
std::vector<std::string> cmCacheManager::CacheIterator::GetPropertyList() const
{
return this->GetEntry().GetPropertyList();
}
void cmCacheManager::CacheIterator::SetValue(const char* value)
{
if (this->IsAtEnd()) {
return;
}
CacheEntry* entry = &this->GetEntry();
if (value) {
entry->Value = value;
entry->Initialized = true;
this->Value = value;
this->Initialized = true;
} else {
entry->Value.clear();
this->Value.clear();
}
}
bool cmCacheManager::CacheIterator::GetValueAsBool() const
{
return cmIsOn(this->GetEntry().Value);
}
std::vector<std::string> cmCacheManager::CacheEntry::GetPropertyList() const
{
return this->Properties.GetKeys();
@@ -627,6 +575,15 @@ cmProp cmCacheManager::CacheEntry::GetProperty(const std::string& prop) const
return this->Properties.GetPropertyValue(prop);
}
bool cmCacheManager::CacheEntry::GetPropertyAsBool(
const std::string& prop) const
{
if (cmProp value = this->GetProperty(prop)) {
return cmIsOn(*value);
}
return false;
}
void cmCacheManager::CacheEntry::SetProperty(const std::string& prop,
const char* value)
{
@@ -639,6 +596,11 @@ void cmCacheManager::CacheEntry::SetProperty(const std::string& prop,
}
}
void cmCacheManager::CacheEntry::SetProperty(const std::string& p, bool v)
{
this->SetProperty(p, v ? "ON" : "OFF");
}
void cmCacheManager::CacheEntry::AppendProperty(const std::string& prop,
const std::string& value,
bool asString)
@@ -657,49 +619,3 @@ void cmCacheManager::CacheEntry::AppendProperty(const std::string& prop,
this->Properties.AppendProperty(prop, value, asString);
}
}
cmProp cmCacheManager::CacheIterator::GetProperty(
const std::string& prop) const
{
if (!this->IsAtEnd()) {
return this->GetEntry().GetProperty(prop);
}
return nullptr;
}
void cmCacheManager::CacheIterator::SetProperty(const std::string& p,
const char* v)
{
if (!this->IsAtEnd()) {
this->GetEntry().SetProperty(p, v);
}
}
void cmCacheManager::CacheIterator::AppendProperty(const std::string& p,
const std::string& v,
bool asString)
{
if (!this->IsAtEnd()) {
this->GetEntry().AppendProperty(p, v, asString);
}
}
bool cmCacheManager::CacheIterator::GetPropertyAsBool(
const std::string& prop) const
{
if (cmProp value = this->GetProperty(prop)) {
return cmIsOn(*value);
}
return false;
}
void cmCacheManager::CacheIterator::SetProperty(const std::string& p, bool v)
{
this->SetProperty(p, v ? "ON" : "OFF");
}
bool cmCacheManager::CacheIterator::PropertyExists(
const std::string& prop) const
{
return this->GetProperty(prop) != nullptr;
}

View File

@@ -25,77 +25,33 @@ class cmMessenger;
*/
class cmCacheManager
{
public:
cmCacheManager();
class CacheIterator;
friend class cmCacheManager::CacheIterator;
private:
struct CacheEntry
class CacheEntry
{
friend class cmCacheManager;
public:
const std::string& GetValue() const { return this->Value; }
void SetValue(const char*);
cmStateEnums::CacheEntryType GetType() const { return this->Type; }
void SetType(cmStateEnums::CacheEntryType ty) { this->Type = ty; }
std::vector<std::string> GetPropertyList() const;
cmProp GetProperty(const std::string& property) const;
bool GetPropertyAsBool(const std::string& property) const;
void SetProperty(const std::string& property, const char* value);
void SetProperty(const std::string& property, bool value);
void AppendProperty(const std::string& property, const std::string& value,
bool asString = false);
private:
std::string Value;
cmStateEnums::CacheEntryType Type = cmStateEnums::UNINITIALIZED;
cmPropertyMap Properties;
std::vector<std::string> GetPropertyList() const;
cmProp GetProperty(const std::string&) const;
void SetProperty(const std::string& property, const char* value);
void AppendProperty(const std::string& property, const std::string& value,
bool asString = false);
bool Initialized = false;
};
public:
class CacheIterator
{
public:
void Begin();
bool Find(const std::string&);
bool IsAtEnd() const;
void Next();
std::string GetName() const { return this->Position->first; }
std::vector<std::string> GetPropertyList() const;
cmProp GetProperty(const std::string&) const;
bool GetPropertyAsBool(const std::string&) const;
bool PropertyExists(const std::string&) const;
void SetProperty(const std::string& property, const char* value);
void AppendProperty(const std::string& property, const std::string& value,
bool asString = false);
void SetProperty(const std::string& property, bool value);
const std::string& GetValue() const { return this->GetEntry().Value; }
bool GetValueAsBool() const;
void SetValue(const char*);
cmStateEnums::CacheEntryType GetType() const
{
return this->GetEntry().Type;
}
void SetType(cmStateEnums::CacheEntryType ty)
{
this->GetEntry().Type = ty;
}
bool Initialized() { return this->GetEntry().Initialized; }
cmCacheManager& Container;
std::map<std::string, CacheEntry>::iterator Position;
CacheIterator(cmCacheManager& cm)
: Container(cm)
{
this->Begin();
}
CacheIterator(cmCacheManager& cm, const char* key)
: Container(cm)
{
if (key) {
this->Find(key);
}
}
private:
CacheEntry const& GetEntry() const { return this->Position->second; }
CacheEntry& GetEntry() { return this->Position->second; }
};
//! return an iterator to iterate through the cache map
cmCacheManager::CacheIterator NewIterator() { return { *this }; }
//! Load a cache for given makefile. Loads from path/CMakeCache.txt.
bool LoadCache(const std::string& path, bool internal,
std::set<std::string>& excludes,
@@ -110,67 +66,82 @@ public:
//! Print the cache to a stream
void PrintCache(std::ostream&) const;
//! Get the iterator for an entry with a given key.
cmCacheManager::CacheIterator GetCacheIterator(const std::string& key);
cmCacheManager::CacheIterator GetCacheIterator();
//! Remove an entry from the cache
void RemoveCacheEntry(const std::string& key);
//! Get the number of entries in the cache
int GetSize() { return static_cast<int>(this->Cache.size()); }
//! Get a value from the cache given a key
cmProp GetInitializedCacheValue(const std::string& key) const;
cmProp GetCacheEntryValue(const std::string& key)
cmProp GetCacheEntryValue(const std::string& key) const
{
cmCacheManager::CacheIterator it = this->GetCacheIterator(key);
if (it.IsAtEnd()) {
return nullptr;
if (auto entry = this->GetCacheEntry(key)) {
return &entry->GetValue();
}
return &it.GetValue();
return nullptr;
}
void SetCacheEntryValue(std::string const& key, std::string const& value)
{
if (auto entry = this->GetCacheEntry(key)) {
entry->SetValue(value.c_str());
}
}
cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key) const
{
if (auto entry = this->GetCacheEntry(key)) {
return entry->GetType();
}
return cmStateEnums::UNINITIALIZED;
}
std::vector<std::string> GetCacheEntryPropertyList(
std::string const& key) const
{
if (auto entry = this->GetCacheEntry(key)) {
return entry->GetPropertyList();
}
return {};
}
cmProp GetCacheEntryProperty(std::string const& key,
std::string const& propName)
std::string const& propName) const
{
return this->GetCacheIterator(key).GetProperty(propName);
}
cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key)
{
return this->GetCacheIterator(key).GetType();
if (auto entry = this->GetCacheEntry(key)) {
return entry->GetProperty(propName);
}
return nullptr;
}
bool GetCacheEntryPropertyAsBool(std::string const& key,
std::string const& propName)
std::string const& propName) const
{
return this->GetCacheIterator(key).GetPropertyAsBool(propName);
if (auto entry = this->GetCacheEntry(key)) {
return entry->GetPropertyAsBool(propName);
}
return false;
}
void SetCacheEntryProperty(std::string const& key,
std::string const& propName,
std::string const& value)
{
this->GetCacheIterator(key).SetProperty(propName, value.c_str());
if (auto entry = this->GetCacheEntry(key)) {
entry->SetProperty(propName, value.c_str());
}
}
void SetCacheEntryBoolProperty(std::string const& key,
std::string const& propName, bool value)
{
this->GetCacheIterator(key).SetProperty(propName, value);
}
void SetCacheEntryValue(std::string const& key, std::string const& value)
{
this->GetCacheIterator(key).SetValue(value.c_str());
if (auto entry = this->GetCacheEntry(key)) {
entry->SetProperty(propName, value);
}
}
void RemoveCacheEntryProperty(std::string const& key,
std::string const& propName)
{
this->GetCacheIterator(key).SetProperty(propName, nullptr);
if (auto entry = this->GetCacheEntry(key)) {
entry->SetProperty(propName, nullptr);
}
}
void AppendCacheEntryProperty(std::string const& key,
@@ -178,16 +149,17 @@ public:
std::string const& value,
bool asString = false)
{
this->GetCacheIterator(key).AppendProperty(propName, value, asString);
if (auto entry = this->GetCacheEntry(key)) {
entry->AppendProperty(propName, value, asString);
}
}
std::vector<std::string> GetCacheEntryKeys()
std::vector<std::string> GetCacheEntryKeys() const
{
std::vector<std::string> definitions;
definitions.reserve(this->GetSize());
cmCacheManager::CacheIterator cit = this->GetCacheIterator();
for (cit.Begin(); !cit.IsAtEnd(); cit.Next()) {
definitions.push_back(cit.GetName());
definitions.reserve(this->Cache.size());
for (auto const& i : this->Cache) {
definitions.push_back(i.first);
}
return definitions;
}
@@ -196,23 +168,22 @@ public:
unsigned int GetCacheMajorVersion() const { return this->CacheMajorVersion; }
unsigned int GetCacheMinorVersion() const { return this->CacheMinorVersion; }
protected:
//! Add an entry into the cache
void AddCacheEntry(const std::string& key, const char* value,
const char* helpString,
cmStateEnums::CacheEntryType type);
//! Remove an entry from the cache
void RemoveCacheEntry(const std::string& key);
private:
//! Get a cache entry object for a key
CacheEntry* GetCacheEntry(const std::string& key);
const CacheEntry* GetCacheEntry(const std::string& key) const;
//! Clean out the CMakeFiles directory if no CMakeCache.txt
void CleanCMakeFiles(const std::string& path);
// Cache version info
unsigned int CacheMajorVersion;
unsigned int CacheMinorVersion;
private:
using CacheEntryMap = std::map<std::string, CacheEntry>;
static void OutputHelpString(std::ostream& fout,
const std::string& helpString);
static void OutputWarningComment(std::ostream& fout,
@@ -228,15 +199,15 @@ private:
std::string const& value);
static const char* PersistentProperties[];
bool ReadPropertyEntry(std::string const& key, CacheEntry& e);
void WritePropertyEntries(std::ostream& os, CacheIterator i,
cmMessenger* messenger);
bool ReadPropertyEntry(const std::string& key, const CacheEntry& e);
void WritePropertyEntries(std::ostream& os, const std::string& entryKey,
const CacheEntry& e, cmMessenger* messenger) const;
CacheEntryMap Cache;
// Only cmake and cmState should be able to add cache values
// the commands should never use the cmCacheManager directly
friend class cmState; // allow access to add cache values
friend class cmake; // allow access to add cache values
std::map<std::string, CacheEntry> Cache;
// Cache version info
unsigned int CacheMajorVersion = 0;
unsigned int CacheMinorVersion = 0;
};
#endif

View File

@@ -139,28 +139,17 @@ bool cmState::DeleteCache(const std::string& path)
std::vector<std::string> cmState::GetCacheEntryKeys() const
{
std::vector<std::string> definitions;
definitions.reserve(this->CacheManager->GetSize());
cmCacheManager::CacheIterator cit = this->CacheManager->GetCacheIterator();
for (cit.Begin(); !cit.IsAtEnd(); cit.Next()) {
definitions.push_back(cit.GetName());
}
return definitions;
return this->CacheManager->GetCacheEntryKeys();
}
cmProp cmState::GetCacheEntryValue(std::string const& key) const
{
cmCacheManager::CacheEntry* e = this->CacheManager->GetCacheEntry(key);
if (!e) {
return nullptr;
}
return &e->Value;
return this->CacheManager->GetCacheEntryValue(key);
}
std::string cmState::GetSafeCacheEntryValue(std::string const& key) const
{
cmProp val = this->GetCacheEntryValue(key);
if (val) {
if (cmProp val = this->GetCacheEntryValue(key)) {
return *val;
}
return std::string();
@@ -175,8 +164,7 @@ const std::string* cmState::GetInitializedCacheValue(
cmStateEnums::CacheEntryType cmState::GetCacheEntryType(
std::string const& key) const
{
cmCacheManager::CacheIterator it = this->CacheManager->GetCacheIterator(key);
return it.GetType();
return this->CacheManager->GetCacheEntryType(key);
}
void cmState::SetCacheEntryValue(std::string const& key,
@@ -189,40 +177,32 @@ void cmState::SetCacheEntryProperty(std::string const& key,
std::string const& propertyName,
std::string const& value)
{
cmCacheManager::CacheIterator it = this->CacheManager->GetCacheIterator(key);
it.SetProperty(propertyName, value.c_str());
this->CacheManager->SetCacheEntryProperty(key, propertyName, value);
}
void cmState::SetCacheEntryBoolProperty(std::string const& key,
std::string const& propertyName,
bool value)
{
cmCacheManager::CacheIterator it = this->CacheManager->GetCacheIterator(key);
it.SetProperty(propertyName, value);
this->CacheManager->SetCacheEntryBoolProperty(key, propertyName, value);
}
std::vector<std::string> cmState::GetCacheEntryPropertyList(
const std::string& key)
{
cmCacheManager::CacheIterator it = this->CacheManager->GetCacheIterator(key);
return it.GetPropertyList();
return this->CacheManager->GetCacheEntryPropertyList(key);
}
cmProp cmState::GetCacheEntryProperty(std::string const& key,
std::string const& propertyName)
{
cmCacheManager::CacheIterator it = this->CacheManager->GetCacheIterator(key);
if (!it.PropertyExists(propertyName)) {
return nullptr;
}
return it.GetProperty(propertyName);
return this->CacheManager->GetCacheEntryProperty(key, propertyName);
}
bool cmState::GetCacheEntryPropertyAsBool(std::string const& key,
std::string const& propertyName)
{
return this->CacheManager->GetCacheIterator(key).GetPropertyAsBool(
propertyName);
return this->CacheManager->GetCacheEntryPropertyAsBool(key, propertyName);
}
void cmState::AddCacheEntry(const std::string& key, const char* value,
@@ -274,14 +254,13 @@ void cmState::AppendCacheEntryProperty(const std::string& key,
const std::string& property,
const std::string& value, bool asString)
{
this->CacheManager->GetCacheIterator(key).AppendProperty(property, value,
asString);
this->CacheManager->AppendCacheEntryProperty(key, property, value, asString);
}
void cmState::RemoveCacheEntryProperty(std::string const& key,
std::string const& propertyName)
{
this->CacheManager->GetCacheIterator(key).SetProperty(propertyName, nullptr);
this->CacheManager->RemoveCacheEntryProperty(key, propertyName);
}
cmStateSnapshot cmState::Reset()