diff options
Diffstat (limited to 'Source/CursesDialog/cmCursesMainForm.cxx')
-rw-r--r-- | Source/CursesDialog/cmCursesMainForm.cxx | 174 |
1 files changed, 70 insertions, 104 deletions
diff --git a/Source/CursesDialog/cmCursesMainForm.cxx b/Source/CursesDialog/cmCursesMainForm.cxx index 5f8a19e..219771b 100644 --- a/Source/CursesDialog/cmCursesMainForm.cxx +++ b/Source/CursesDialog/cmCursesMainForm.cxx @@ -2,7 +2,6 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmCursesMainForm.h" -#include "cmAlgorithms.h" #include "cmCursesCacheEntryComposite.h" #include "cmCursesDummyWidget.h" #include "cmCursesForm.h" @@ -18,6 +17,8 @@ #include "cmVersion.h" #include "cmake.h" +#include <cm/memory> + #include <algorithm> #include <cstdio> #include <cstring> @@ -34,8 +35,6 @@ cmCursesMainForm::cmCursesMainForm(std::vector<std::string> args, , InitialWidth(initWidth) { this->NumberOfPages = 0; - this->Fields = nullptr; - this->Entries = nullptr; this->AdvancedMode = false; this->NumberOfVisibleEntries = 0; this->OkToGenerate = false; @@ -43,7 +42,8 @@ cmCursesMainForm::cmCursesMainForm(std::vector<std::string> args, "Welcome to ccmake, curses based user interface for CMake."); this->HelpMessage.emplace_back(); this->HelpMessage.emplace_back(s_ConstHelpMessage); - this->CMakeInstance = new cmake(cmake::RoleProject, cmState::Project); + this->CMakeInstance = + cm::make_unique<cmake>(cmake::RoleProject, cmState::Project); this->CMakeInstance->SetCMakeEditCommand( cmSystemTools::GetCMakeCursesCommand()); @@ -52,8 +52,6 @@ cmCursesMainForm::cmCursesMainForm(std::vector<std::string> args, cmStrCat(cmSystemTools::GetProgramPath(this->Args[0]), "/cmake"); this->Args[0] = whereCMake; this->CMakeInstance->SetArgs(this->Args); - this->SearchString = ""; - this->OldSearchString = ""; this->SearchMode = false; } @@ -64,27 +62,15 @@ cmCursesMainForm::~cmCursesMainForm() free_form(this->Form); this->Form = nullptr; } - delete[] this->Fields; - - // Clean-up composites - if (this->Entries) { - cmDeleteAll(*this->Entries); - } - delete this->Entries; - if (this->CMakeInstance) { - delete this->CMakeInstance; - this->CMakeInstance = nullptr; - } } // See if a cache entry is in the list of entries in the ui. bool cmCursesMainForm::LookForCacheEntry(const std::string& key) { - return this->Entries && - std::any_of(this->Entries->begin(), this->Entries->end(), - [&key](cmCursesCacheEntryComposite* entry) { - return key == entry->Key; - }); + return std::any_of(this->Entries.begin(), this->Entries.end(), + [&key](cmCursesCacheEntryComposite const& entry) { + return key == entry.Key; + }); } // Create new cmCursesCacheEntryComposite entries from the cache @@ -92,11 +78,10 @@ void cmCursesMainForm::InitializeUI() { // Create a vector of cmCursesCacheEntryComposite's // which contain labels, entries and new entry markers - std::vector<cmCursesCacheEntryComposite*>* newEntries = - new std::vector<cmCursesCacheEntryComposite*>; + std::vector<cmCursesCacheEntryComposite> newEntries; std::vector<std::string> cacheKeys = this->CMakeInstance->GetState()->GetCacheEntryKeys(); - newEntries->reserve(cacheKeys.size()); + newEntries.reserve(cacheKeys.size()); // Count non-internal and non-static entries int count = 0; @@ -112,13 +97,12 @@ void cmCursesMainForm::InitializeUI() int entrywidth = this->InitialWidth - 35; - cmCursesCacheEntryComposite* comp; if (count == 0) { // If cache is empty, display a label saying so and a // dummy entry widget (does not respond to input) - comp = new cmCursesCacheEntryComposite("EMPTY CACHE", 30, 30); - comp->Entry = new cmCursesDummyWidget(1, 1, 1, 1); - newEntries->push_back(comp); + cmCursesCacheEntryComposite comp("EMPTY CACHE", 30, 30); + comp.Entry = cm::make_unique<cmCursesDummyWidget>(1, 1, 1, 1); + newEntries.emplace_back(std::move(comp)); } else { // Create the composites. @@ -132,8 +116,8 @@ void cmCursesMainForm::InitializeUI() } if (!this->LookForCacheEntry(key)) { - newEntries->push_back(new cmCursesCacheEntryComposite( - key, this->CMakeInstance, true, 30, entrywidth)); + newEntries.emplace_back(key, this->CMakeInstance->GetState(), true, 30, + entrywidth); this->OkToGenerate = false; } } @@ -148,18 +132,14 @@ void cmCursesMainForm::InitializeUI() } if (this->LookForCacheEntry(key)) { - newEntries->push_back(new cmCursesCacheEntryComposite( - key, this->CMakeInstance, false, 30, entrywidth)); + newEntries.emplace_back(key, this->CMakeInstance->GetState(), false, + 30, entrywidth); } } } - // Clean old entries - if (this->Entries) { - cmDeleteAll(*this->Entries); - } - delete this->Entries; - this->Entries = newEntries; + // Replace old entries + this->Entries = std::move(newEntries); // Compute fields from composites this->RePost(); @@ -173,18 +153,18 @@ void cmCursesMainForm::RePost() free_form(this->Form); this->Form = nullptr; } - delete[] this->Fields; + this->Fields.clear(); if (this->AdvancedMode) { - this->NumberOfVisibleEntries = this->Entries->size(); + this->NumberOfVisibleEntries = this->Entries.size(); } else { // If normal mode, count only non-advanced entries this->NumberOfVisibleEntries = 0; - for (cmCursesCacheEntryComposite* entry : *this->Entries) { + for (cmCursesCacheEntryComposite& entry : this->Entries) { const char* existingValue = - this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue()); + this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue()); bool advanced = this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool( - entry->GetValue(), "ADVANCED"); + entry.GetValue(), "ADVANCED"); if (!existingValue || (!this->AdvancedMode && advanced)) { continue; } @@ -197,38 +177,32 @@ void cmCursesMainForm::RePost() } // Assign the fields: 3 for each entry: label, new entry marker // ('*' or ' ') and entry widget - this->Fields = new FIELD*[3 * this->NumberOfVisibleEntries + 1]; - size_t cc; - for (cc = 0; cc < 3 * this->NumberOfVisibleEntries + 1; cc++) { - this->Fields[cc] = nullptr; - } + this->Fields.reserve(3 * this->NumberOfVisibleEntries + 1); // Assign fields - int j = 0; - for (cmCursesCacheEntryComposite* entry : *this->Entries) { + for (cmCursesCacheEntryComposite& entry : this->Entries) { const char* existingValue = - this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue()); + this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue()); bool advanced = this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool( - entry->GetValue(), "ADVANCED"); + entry.GetValue(), "ADVANCED"); if (!existingValue || (!this->AdvancedMode && advanced)) { continue; } - this->Fields[3 * j] = entry->Label->Field; - this->Fields[3 * j + 1] = entry->IsNewLabel->Field; - this->Fields[3 * j + 2] = entry->Entry->Field; - j++; + this->Fields.push_back(entry.Label->Field); + this->Fields.push_back(entry.IsNewLabel->Field); + this->Fields.push_back(entry.Entry->Field); } // if no cache entries there should still be one dummy field - if (j == 0) { - const auto& front = *this->Entries->front(); - this->Fields[0] = front.Label->Field; - this->Fields[1] = front.IsNewLabel->Field; - this->Fields[2] = front.Entry->Field; + if (this->Fields.empty()) { + const auto& front = this->Entries.front(); + this->Fields.push_back(front.Label->Field); + this->Fields.push_back(front.IsNewLabel->Field); + this->Fields.push_back(front.Entry->Field); this->NumberOfVisibleEntries = 1; } // Has to be null terminated. - this->Fields[3 * this->NumberOfVisibleEntries] = nullptr; + this->Fields.push_back(nullptr); } void cmCursesMainForm::Render(int left, int top, int width, int height) @@ -261,16 +235,16 @@ void cmCursesMainForm::Render(int left, int top, int width, int height) height -= 7; if (this->AdvancedMode) { - this->NumberOfVisibleEntries = this->Entries->size(); + this->NumberOfVisibleEntries = this->Entries.size(); } else { // If normal, display only non-advanced entries this->NumberOfVisibleEntries = 0; - for (cmCursesCacheEntryComposite* entry : *this->Entries) { + for (cmCursesCacheEntryComposite& entry : this->Entries) { const char* existingValue = - this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue()); + this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue()); bool advanced = this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool( - entry->GetValue(), "ADVANCED"); + entry.GetValue(), "ADVANCED"); if (!existingValue || (!this->AdvancedMode && advanced)) { continue; } @@ -283,12 +257,12 @@ void cmCursesMainForm::Render(int left, int top, int width, int height) if (height > 0) { bool isNewPage; int i = 0; - for (cmCursesCacheEntryComposite* entry : *this->Entries) { + for (cmCursesCacheEntryComposite& entry : this->Entries) { const char* existingValue = - this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue()); + this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue()); bool advanced = this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool( - entry->GetValue(), "ADVANCED"); + entry.GetValue(), "ADVANCED"); if (!existingValue || (!this->AdvancedMode && advanced)) { continue; } @@ -299,16 +273,16 @@ void cmCursesMainForm::Render(int left, int top, int width, int height) if (isNewPage) { 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); + 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++; } } // Post the form - this->Form = new_form(this->Fields); + this->Form = new_form(this->Fields.data()); post_form(this->Form); // Update toolbar this->UpdateStatusBar(); @@ -655,28 +629,28 @@ void cmCursesMainForm::RemoveEntry(const char* value) } auto removeIt = - std::find_if(this->Entries->begin(), this->Entries->end(), - [value](cmCursesCacheEntryComposite* entry) -> bool { - const char* val = entry->GetValue(); + std::find_if(this->Entries.begin(), this->Entries.end(), + [value](cmCursesCacheEntryComposite& entry) -> bool { + const char* val = entry.GetValue(); return val != nullptr && !strcmp(value, val); }); - if (removeIt != this->Entries->end()) { + if (removeIt != this->Entries.end()) { this->CMakeInstance->UnwatchUnusedCli(value); - this->Entries->erase(removeIt); + this->Entries.erase(removeIt); } } // copy from the list box to the cache manager void cmCursesMainForm::FillCacheManagerFromUI() { - for (cmCursesCacheEntryComposite* entry : *this->Entries) { - const std::string& cacheKey = entry->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 = entry->Entry->GetValue(); + std::string newValue = entry.Entry->GetValue(); std::string fixedOldValue; std::string fixedNewValue; cmStateEnums::CacheEntryType t = @@ -762,7 +736,7 @@ void cmCursesMainForm::HandleInput() this->JumpToCacheEntry(this->SearchString.c_str()); this->OldSearchString = this->SearchString; } - this->SearchString = ""; + this->SearchString.clear(); } /* else if ( key == KEY_ESCAPE ) @@ -778,7 +752,7 @@ void cmCursesMainForm::HandleInput() } } else if (key == ctrl('h') || key == KEY_BACKSPACE || key == KEY_DC) { if (!this->SearchString.empty()) { - this->SearchString.resize(this->SearchString.size() - 1); + this->SearchString.pop_back(); } } } else if (currentWidget && !this->SearchMode) { @@ -867,17 +841,9 @@ void cmCursesMainForm::HandleInput() curField, "HELPSTRING"); } if (helpString) { - char* message = new char - [strlen(curField) + strlen(helpString) + - strlen( - "Current option is: \n Help string for this option is: \n") + - 10]; - sprintf( - message, - "Current option is: %s\nHelp string for this option is: %s\n", - curField, helpString); - this->HelpMessage[1] = message; - delete[] message; + this->HelpMessage[1] = + cmStrCat("Current option is: ", curField, '\n', + "Help string for this option is: ", helpString, '\n'); } else { this->HelpMessage[1] = ""; } @@ -973,14 +939,14 @@ void cmCursesMainForm::HandleInput() if (nextCur) { // make the next or prev. current field after deletion - 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); + auto nextEntryIt = std::find_if( + this->Entries.begin(), this->Entries.end(), + [&nextVal](cmCursesCacheEntryComposite const& entry) { + return nextVal == entry.Key; + }); + + if (nextEntryIt != this->Entries.end()) { + set_current_field(this->Form, nextEntryIt->Entry->Field); } } } |