Commit f6291eee authored by Artur Ryt's avatar Artur Ryt
Browse files

cmCursesMainForm: Modernize with STL and ranged-for loops

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