Commit f6291eee authored by Artur Ryt's avatar Artur Ryt

cmCursesMainForm: Modernize with STL and ranged-for loops

parent e7d31971
......@@ -79,18 +79,11 @@ cmCursesMainForm::~cmCursesMainForm()
// See if a cache entry is in the list of entries in the ui.
bool cmCursesMainForm::LookForCacheEntry(const std::string& key)
{
if (!this->Entries) {
return false;
}
std::vector<cmCursesCacheEntryComposite*>::iterator it;
for (it = this->Entries->begin(); it != this->Entries->end(); ++it) {
if (key == (*it)->Key) {
return true;
}
}
return false;
return this->Entries &&
std::any_of(this->Entries->begin(), this->Entries->end(),
[&key](cmCursesCacheEntryComposite* entry) {
return key == entry->Key;
});
}
// Create new cmCursesCacheEntryComposite entries from the cache
......@@ -190,13 +183,12 @@ void cmCursesMainForm::RePost()
} else {
// If normal mode, count only non-advanced entries
this->NumberOfVisibleEntries = 0;
std::vector<cmCursesCacheEntryComposite*>::iterator it;
for (it = this->Entries->begin(); it != this->Entries->end(); ++it) {
for (cmCursesCacheEntryComposite* entry : *this->Entries) {
const char* existingValue =
this->CMakeInstance->GetState()->GetCacheEntryValue((*it)->GetValue());
this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue());
bool advanced =
this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
(*it)->GetValue(), "ADVANCED");
entry->GetValue(), "ADVANCED");
if (!existingValue || (!this->AdvancedMode && advanced)) {
continue;
}
......@@ -217,27 +209,26 @@ void cmCursesMainForm::RePost()
// Assign fields
int j = 0;
std::vector<cmCursesCacheEntryComposite*>::iterator it;
for (it = this->Entries->begin(); it != this->Entries->end(); ++it) {
for (cmCursesCacheEntryComposite* entry : *this->Entries) {
const char* existingValue =
this->CMakeInstance->GetState()->GetCacheEntryValue((*it)->GetValue());
this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue());
bool advanced =
this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
(*it)->GetValue(), "ADVANCED");
entry->GetValue(), "ADVANCED");
if (!existingValue || (!this->AdvancedMode && advanced)) {
continue;
}
this->Fields[3 * j] = (*it)->Label->Field;
this->Fields[3 * j + 1] = (*it)->IsNewLabel->Field;
this->Fields[3 * j + 2] = (*it)->Entry->Field;
this->Fields[3 * j] = entry->Label->Field;
this->Fields[3 * j + 1] = entry->IsNewLabel->Field;
this->Fields[3 * j + 2] = entry->Entry->Field;
j++;
}
// if no cache entries there should still be one dummy field
if (j == 0) {
it = this->Entries->begin();
this->Fields[0] = (*it)->Label->Field;
this->Fields[1] = (*it)->IsNewLabel->Field;
this->Fields[2] = (*it)->Entry->Field;
const auto& front = *this->Entries->front();
this->Fields[0] = front.Label->Field;
this->Fields[1] = front.IsNewLabel->Field;
this->Fields[2] = front.Entry->Field;
this->NumberOfVisibleEntries = 1;
}
// Has to be null terminated.
......@@ -278,13 +269,12 @@ void cmCursesMainForm::Render(int left, int top, int width, int height)
} else {
// If normal, display only non-advanced entries
this->NumberOfVisibleEntries = 0;
std::vector<cmCursesCacheEntryComposite*>::iterator it;
for (it = this->Entries->begin(); it != this->Entries->end(); ++it) {
for (cmCursesCacheEntryComposite* entry : *this->Entries) {
const char* existingValue =
this->CMakeInstance->GetState()->GetCacheEntryValue((*it)->GetValue());
this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue());
bool advanced =
this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
(*it)->GetValue(), "ADVANCED");
entry->GetValue(), "ADVANCED");
if (!existingValue || (!this->AdvancedMode && advanced)) {
continue;
}
......@@ -297,13 +287,12 @@ void cmCursesMainForm::Render(int left, int top, int width, int height)
if (height > 0) {
bool isNewPage;
int i = 0;
std::vector<cmCursesCacheEntryComposite*>::iterator it;
for (it = this->Entries->begin(); it != this->Entries->end(); ++it) {
for (cmCursesCacheEntryComposite* entry : *this->Entries) {
const char* existingValue =
this->CMakeInstance->GetState()->GetCacheEntryValue((*it)->GetValue());
this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue());
bool advanced =
this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
(*it)->GetValue(), "ADVANCED");
entry->GetValue(), "ADVANCED");
if (!existingValue || (!this->AdvancedMode && advanced)) {
continue;
}
......@@ -314,10 +303,10 @@ void cmCursesMainForm::Render(int left, int top, int width, int height)
if (isNewPage) {
this->NumberOfPages++;
}
(*it)->Label->Move(left, top + row - 1, isNewPage);
(*it)->IsNewLabel->Move(left + 32, top + row - 1, false);
(*it)->Entry->Move(left + 33, top + row - 1, false);
(*it)->Entry->SetPage(this->NumberOfPages);
entry->Label->Move(left, top + row - 1, isNewPage);
entry->IsNewLabel->Move(left + 32, top + row - 1, false);
entry->Entry->Move(left + 33, top + row - 1, false);
entry->Entry->SetPage(this->NumberOfPages);
i++;
}
}
......@@ -659,28 +648,28 @@ void cmCursesMainForm::RemoveEntry(const char* value)
return;
}
std::vector<cmCursesCacheEntryComposite*>::iterator it;
for (it = this->Entries->begin(); it != this->Entries->end(); ++it) {
const char* val = (*it)->GetValue();
if (val && !strcmp(value, val)) {
this->CMakeInstance->UnwatchUnusedCli(value);
this->Entries->erase(it);
break;
}
auto removeIt = std::find_if(this->Entries->begin(), this->Entries->end(),
[value](cmCursesCacheEntryComposite* entry) {
const char* val = entry->GetValue();
return val && !strcmp(value, val);
});
if (removeIt != this->Entries->end()) {
this->CMakeInstance->UnwatchUnusedCli(value);
this->Entries->erase(removeIt);
}
}
// copy from the list box to the cache manager
void cmCursesMainForm::FillCacheManagerFromUI()
{
size_t size = this->Entries->size();
for (size_t i = 0; i < size; i++) {
std::string cacheKey = (*this->Entries)[i]->Key;
for (cmCursesCacheEntryComposite* entry : *this->Entries) {
const std::string& cacheKey = entry->Key;
const char* existingValue =
this->CMakeInstance->GetState()->GetCacheEntryValue(cacheKey);
if (existingValue) {
std::string oldValue = existingValue;
std::string newValue = (*this->Entries)[i]->Entry->GetValue();
std::string newValue = entry->GetValue();
std::string fixedOldValue;
std::string fixedNewValue;
cmStateEnums::CacheEntryType t =
......@@ -976,17 +965,14 @@ void cmCursesMainForm::HandleInput()
if (nextCur) {
// make the next or prev. current field after deletion
nextCur = nullptr;
std::vector<cmCursesCacheEntryComposite*>::iterator it;
for (it = this->Entries->begin(); it != this->Entries->end();
++it) {
if (nextVal == (*it)->Key) {
nextCur = (*it)->Entry->Field;
}
}
if (nextCur) {
set_current_field(this->Form, nextCur);
auto nextEntryIt =
std::find_if(this->Entries->begin(), this->Entries->end(),
[&nextVal](cmCursesCacheEntryComposite* entry) {
return nextVal == entry->Key;
});
if (nextEntryIt != this->Entries->end()) {
set_current_field(this->Form, (*nextEntryIt)->Entry->Field);
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment