From 2b1607114985e56a7847eb6081d5656f73c230d0 Mon Sep 17 00:00:00 2001 From: Tushar Maheshwari Date: Thu, 26 Sep 2019 08:36:24 +0530 Subject: CursesDialog: modernize CMake usage --- Source/CMakeLists.txt | 2 +- Source/CursesDialog/CMakeLists.txt | 36 ++++++++++++++++-------------------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 8ed7b2f..fe048cb 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -1142,7 +1142,7 @@ target_link_libraries(cpack CPackLib) # Curses GUI if(BUILD_CursesDialog) - include(${CMake_SOURCE_DIR}/Source/CursesDialog/CMakeLists.txt) + add_subdirectory(CursesDialog) endif() # Qt GUI diff --git a/Source/CursesDialog/CMakeLists.txt b/Source/CursesDialog/CMakeLists.txt index 270b07e..7009717 100644 --- a/Source/CursesDialog/CMakeLists.txt +++ b/Source/CursesDialog/CMakeLists.txt @@ -1,26 +1,22 @@ # Distributed under the OSI-approved BSD 3-Clause License. See accompanying # file Copyright.txt or https://cmake.org/licensing for details. -set( CURSES_SRCS - CursesDialog/cmCursesOptionsWidget.cxx - CursesDialog/cmCursesBoolWidget.cxx - CursesDialog/cmCursesCacheEntryComposite.cxx - CursesDialog/cmCursesDummyWidget.cxx - CursesDialog/cmCursesFilePathWidget.cxx - CursesDialog/cmCursesForm.cxx - CursesDialog/cmCursesLabelWidget.cxx - CursesDialog/cmCursesLongMessageForm.cxx - CursesDialog/cmCursesMainForm.cxx - CursesDialog/cmCursesPathWidget.cxx - CursesDialog/cmCursesStringWidget.cxx - CursesDialog/cmCursesWidget.cxx - CursesDialog/ccmake.cxx - ) - -include_directories(${CURSES_INCLUDE_PATH}) - - -add_executable(ccmake ${CURSES_SRCS} ) +add_executable(ccmake + ccmake.cxx + cmCursesBoolWidget.cxx + cmCursesCacheEntryComposite.cxx + cmCursesDummyWidget.cxx + cmCursesFilePathWidget.cxx + cmCursesForm.cxx + cmCursesLabelWidget.cxx + cmCursesLongMessageForm.cxx + cmCursesMainForm.cxx + cmCursesOptionsWidget.cxx + cmCursesPathWidget.cxx + cmCursesStringWidget.cxx + cmCursesWidget.cxx + ) +target_include_directories(ccmake PRIVATE ${CURSES_INCLUDE_PATH}) target_link_libraries(ccmake CMakeLib) if(CMAKE_USE_SYSTEM_FORM) find_path(CURSES_FORM_INCLUDE_DIR NAMES form.h HINTS ${CURSES_INCLUDE_PATH} ${CURSES_INCLUDE_PATH}/ncurses) -- cgit v0.12 From 36875ff419240a5a60b1b13ff190a5824a8363a5 Mon Sep 17 00:00:00 2001 From: Tushar Maheshwari Date: Sun, 8 Sep 2019 11:22:38 +0530 Subject: cmCursesMainForm: cleanup manual allocation --- .../CursesDialog/cmCursesCacheEntryComposite.cxx | 11 +- Source/CursesDialog/cmCursesCacheEntryComposite.h | 6 +- Source/CursesDialog/cmCursesMainForm.cxx | 148 ++++++++------------- Source/CursesDialog/cmCursesMainForm.h | 11 +- 4 files changed, 69 insertions(+), 107 deletions(-) diff --git a/Source/CursesDialog/cmCursesCacheEntryComposite.cxx b/Source/CursesDialog/cmCursesCacheEntryComposite.cxx index 47fe84c..8b160a4 100644 --- a/Source/CursesDialog/cmCursesCacheEntryComposite.cxx +++ b/Source/CursesDialog/cmCursesCacheEntryComposite.cxx @@ -13,7 +13,6 @@ #include "cmStateTypes.h" #include "cmStringAlgorithms.h" #include "cmSystemTools.h" -#include "cmake.h" #include #include @@ -26,12 +25,11 @@ cmCursesCacheEntryComposite::cmCursesCacheEntryComposite( { this->Label = new cmCursesLabelWidget(this->LabelWidth, 1, 1, 1, key); this->IsNewLabel = new cmCursesLabelWidget(1, 1, 1, 1, " "); - this->Entry = nullptr; this->Entry = new cmCursesStringWidget(this->EntryWidth, 1, 1, 1); } cmCursesCacheEntryComposite::cmCursesCacheEntryComposite( - const std::string& key, cmake* cm, bool isNew, int labelwidth, + const std::string& key, cmState* state, bool isNew, int labelwidth, int entrywidth) : Key(key) , LabelWidth(labelwidth) @@ -45,9 +43,9 @@ cmCursesCacheEntryComposite::cmCursesCacheEntryComposite( } this->Entry = nullptr; - const char* value = cm->GetState()->GetCacheEntryValue(key); + const char* value = state->GetCacheEntryValue(key); assert(value); - switch (cm->GetState()->GetCacheEntryType(key)) { + switch (state->GetCacheEntryType(key)) { case cmStateEnums::BOOL: this->Entry = new cmCursesBoolWidget(this->EntryWidth, 1, 1, 1); if (cmIsOn(value)) { @@ -65,8 +63,7 @@ cmCursesCacheEntryComposite::cmCursesCacheEntryComposite( static_cast(this->Entry)->SetString(value); break; case cmStateEnums::STRING: { - const char* stringsProp = - cm->GetState()->GetCacheEntryProperty(key, "STRINGS"); + const char* stringsProp = state->GetCacheEntryProperty(key, "STRINGS"); if (stringsProp) { cmCursesOptionsWidget* ow = new cmCursesOptionsWidget(this->EntryWidth, 1, 1, 1); diff --git a/Source/CursesDialog/cmCursesCacheEntryComposite.h b/Source/CursesDialog/cmCursesCacheEntryComposite.h index 0a69d3a..b18f076 100644 --- a/Source/CursesDialog/cmCursesCacheEntryComposite.h +++ b/Source/CursesDialog/cmCursesCacheEntryComposite.h @@ -9,15 +9,15 @@ class cmCursesLabelWidget; class cmCursesWidget; -class cmake; +class cmState; class cmCursesCacheEntryComposite { public: cmCursesCacheEntryComposite(const std::string& key, int labelwidth, int entrywidth); - cmCursesCacheEntryComposite(const std::string& key, cmake* cm, bool isNew, - int labelwidth, int entrywidth); + cmCursesCacheEntryComposite(const std::string& key, cmState* state, + bool isNew, int labelwidth, int entrywidth); ~cmCursesCacheEntryComposite(); cmCursesCacheEntryComposite(cmCursesCacheEntryComposite const&) = delete; diff --git a/Source/CursesDialog/cmCursesMainForm.cxx b/Source/CursesDialog/cmCursesMainForm.cxx index 5f8a19e..e754196 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 + #include #include #include @@ -34,8 +35,6 @@ cmCursesMainForm::cmCursesMainForm(std::vector 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 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::RoleProject, cmState::Project); this->CMakeInstance->SetCMakeEditCommand( cmSystemTools::GetCMakeCursesCommand()); @@ -52,8 +52,6 @@ cmCursesMainForm::cmCursesMainForm(std::vector 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,16 @@ 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](std::unique_ptr const& entry) { + return key == entry->Key; + }); } // Create new cmCursesCacheEntryComposite entries from the cache @@ -92,11 +79,10 @@ void cmCursesMainForm::InitializeUI() { // Create a vector of cmCursesCacheEntryComposite's // which contain labels, entries and new entry markers - std::vector* newEntries = - new std::vector; + std::vector> newEntries; std::vector 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 +98,13 @@ 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); + std::unique_ptr comp = + cm::make_unique("EMPTY CACHE", 30, 30); comp->Entry = new cmCursesDummyWidget(1, 1, 1, 1); - newEntries->push_back(comp); + newEntries.emplace_back(std::move(comp)); } else { // Create the composites. @@ -132,8 +118,8 @@ void cmCursesMainForm::InitializeUI() } if (!this->LookForCacheEntry(key)) { - newEntries->push_back(new cmCursesCacheEntryComposite( - key, this->CMakeInstance, true, 30, entrywidth)); + newEntries.emplace_back(cm::make_unique( + key, this->CMakeInstance->GetState(), true, 30, entrywidth)); this->OkToGenerate = false; } } @@ -148,18 +134,14 @@ void cmCursesMainForm::InitializeUI() } if (this->LookForCacheEntry(key)) { - newEntries->push_back(new cmCursesCacheEntryComposite( - key, this->CMakeInstance, false, 30, entrywidth)); + newEntries.emplace_back(cm::make_unique( + 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,13 +155,13 @@ 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 (std::unique_ptr& entry : this->Entries) { const char* existingValue = this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue()); bool advanced = @@ -197,15 +179,10 @@ 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 (std::unique_ptr& entry : this->Entries) { const char* existingValue = this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue()); bool advanced = @@ -214,21 +191,20 @@ void cmCursesMainForm::RePost() 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,11 +237,11 @@ 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 (std::unique_ptr& entry : this->Entries) { const char* existingValue = this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue()); bool advanced = @@ -283,7 +259,7 @@ 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 (std::unique_ptr& entry : this->Entries) { const char* existingValue = this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue()); bool advanced = @@ -308,7 +284,7 @@ void cmCursesMainForm::Render(int left, int top, int width, int height) } // Post the form - this->Form = new_form(this->Fields); + this->Form = new_form(this->Fields.data()); post_form(this->Form); // Update toolbar this->UpdateStatusBar(); @@ -654,23 +630,23 @@ void cmCursesMainForm::RemoveEntry(const char* value) return; } - auto removeIt = - std::find_if(this->Entries->begin(), this->Entries->end(), - [value](cmCursesCacheEntryComposite* entry) -> bool { - const char* val = entry->GetValue(); - return val != nullptr && !strcmp(value, val); - }); + auto removeIt = std::find_if( + this->Entries.begin(), this->Entries.end(), + [value](std::unique_ptr& 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) { + for (std::unique_ptr& entry : this->Entries) { const std::string& cacheKey = entry->Key; const char* existingValue = this->CMakeInstance->GetState()->GetCacheEntryValue(cacheKey); @@ -762,7 +738,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 +754,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 +843,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,13 +941,13 @@ 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; - }); + auto nextEntryIt = std::find_if( + this->Entries.begin(), this->Entries.end(), + [&nextVal](std::unique_ptr& entry) { + return nextVal == entry->Key; + }); - if (nextEntryIt != this->Entries->end()) { + if (nextEntryIt != this->Entries.end()) { set_current_field(this->Form, (*nextEntryIt)->Entry->Field); } } diff --git a/Source/CursesDialog/cmCursesMainForm.h b/Source/CursesDialog/cmCursesMainForm.h index f3194ab..9a83c30 100644 --- a/Source/CursesDialog/cmCursesMainForm.h +++ b/Source/CursesDialog/cmCursesMainForm.h @@ -10,6 +10,7 @@ #include "cmStateTypes.h" #include +#include #include #include @@ -122,7 +123,7 @@ protected: void JumpToCacheEntry(const char* str); // Copies of cache entries stored in the user interface - std::vector* Entries; + std::vector> Entries; // Errors produced during last run of cmake std::vector Errors; // Command line arguments to be passed to cmake each time @@ -136,11 +137,7 @@ protected: static const char* s_ConstHelpMessage; // Fields displayed. Includes labels, new entry markers, entries - FIELD** Fields; - // Where is source of current project - std::string WhereSource; - // Where is cmake executable - std::string WhereCMake; + std::vector Fields; // Number of entries shown (depends on mode -normal or advanced-) size_t NumberOfVisibleEntries; bool AdvancedMode; @@ -150,7 +147,7 @@ protected: int NumberOfPages; int InitialWidth; - cmake* CMakeInstance; + std::unique_ptr CMakeInstance; std::string SearchString; std::string OldSearchString; -- cgit v0.12 From bc71b253cb372cc6c0ae50406a11b81fe4259559 Mon Sep 17 00:00:00 2001 From: Tushar Maheshwari Date: Sat, 14 Sep 2019 17:27:00 +0530 Subject: cmCursesCacheEntryComposite: default destructor --- .../CursesDialog/cmCursesCacheEntryComposite.cxx | 70 ++++++++++++---------- Source/CursesDialog/cmCursesCacheEntryComposite.h | 7 ++- Source/CursesDialog/cmCursesMainForm.cxx | 2 +- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/Source/CursesDialog/cmCursesCacheEntryComposite.cxx b/Source/CursesDialog/cmCursesCacheEntryComposite.cxx index 8b160a4..561784c 100644 --- a/Source/CursesDialog/cmCursesCacheEntryComposite.cxx +++ b/Source/CursesDialog/cmCursesCacheEntryComposite.cxx @@ -14,7 +14,10 @@ #include "cmStringAlgorithms.h" #include "cmSystemTools.h" +#include + #include +#include #include cmCursesCacheEntryComposite::cmCursesCacheEntryComposite( @@ -23,9 +26,11 @@ cmCursesCacheEntryComposite::cmCursesCacheEntryComposite( , LabelWidth(labelwidth) , EntryWidth(entrywidth) { - this->Label = new cmCursesLabelWidget(this->LabelWidth, 1, 1, 1, key); - this->IsNewLabel = new cmCursesLabelWidget(1, 1, 1, 1, " "); - this->Entry = new cmCursesStringWidget(this->EntryWidth, 1, 1, 1); + this->Label = + cm::make_unique(this->LabelWidth, 1, 1, 1, key); + this->IsNewLabel = cm::make_unique(1, 1, 1, 1, " "); + this->Entry = + cm::make_unique(this->EntryWidth, 1, 1, 1); } cmCursesCacheEntryComposite::cmCursesCacheEntryComposite( @@ -35,47 +40,51 @@ cmCursesCacheEntryComposite::cmCursesCacheEntryComposite( , LabelWidth(labelwidth) , EntryWidth(entrywidth) { - this->Label = new cmCursesLabelWidget(this->LabelWidth, 1, 1, 1, key); + this->Label = + cm::make_unique(this->LabelWidth, 1, 1, 1, key); if (isNew) { - this->IsNewLabel = new cmCursesLabelWidget(1, 1, 1, 1, "*"); + this->IsNewLabel = cm::make_unique(1, 1, 1, 1, "*"); } else { - this->IsNewLabel = new cmCursesLabelWidget(1, 1, 1, 1, " "); + this->IsNewLabel = cm::make_unique(1, 1, 1, 1, " "); } - this->Entry = nullptr; const char* value = state->GetCacheEntryValue(key); assert(value); switch (state->GetCacheEntryType(key)) { - case cmStateEnums::BOOL: - this->Entry = new cmCursesBoolWidget(this->EntryWidth, 1, 1, 1); - if (cmIsOn(value)) { - static_cast(this->Entry)->SetValueAsBool(true); - } else { - static_cast(this->Entry)->SetValueAsBool(false); - } + case cmStateEnums::BOOL: { + auto bw = cm::make_unique(this->EntryWidth, 1, 1, 1); + bw->SetValueAsBool(cmIsOn(value)); + this->Entry = std::move(bw); break; - case cmStateEnums::PATH: - this->Entry = new cmCursesPathWidget(this->EntryWidth, 1, 1, 1); - static_cast(this->Entry)->SetString(value); + } + case cmStateEnums::PATH: { + auto pw = cm::make_unique(this->EntryWidth, 1, 1, 1); + pw->SetString(value); + this->Entry = std::move(pw); break; - case cmStateEnums::FILEPATH: - this->Entry = new cmCursesFilePathWidget(this->EntryWidth, 1, 1, 1); - static_cast(this->Entry)->SetString(value); + } + case cmStateEnums::FILEPATH: { + auto fpw = + cm::make_unique(this->EntryWidth, 1, 1, 1); + fpw->SetString(value); + this->Entry = std::move(fpw); break; + } case cmStateEnums::STRING: { const char* stringsProp = state->GetCacheEntryProperty(key, "STRINGS"); if (stringsProp) { - cmCursesOptionsWidget* ow = - new cmCursesOptionsWidget(this->EntryWidth, 1, 1, 1); - this->Entry = ow; - std::vector options = cmExpandedList(stringsProp); - for (auto const& opt : options) { + auto ow = + cm::make_unique(this->EntryWidth, 1, 1, 1); + for (std::string const& opt : cmExpandedList(stringsProp)) { ow->AddOption(opt); } ow->SetOption(value); + this->Entry = std::move(ow); } else { - this->Entry = new cmCursesStringWidget(this->EntryWidth, 1, 1, 1); - static_cast(this->Entry)->SetString(value); + auto sw = + cm::make_unique(this->EntryWidth, 1, 1, 1); + sw->SetString(value); + this->Entry = std::move(sw); } break; } @@ -88,12 +97,7 @@ cmCursesCacheEntryComposite::cmCursesCacheEntryComposite( } } -cmCursesCacheEntryComposite::~cmCursesCacheEntryComposite() -{ - delete this->Label; - delete this->IsNewLabel; - delete this->Entry; -} +cmCursesCacheEntryComposite::~cmCursesCacheEntryComposite() = default; const char* cmCursesCacheEntryComposite::GetValue() { diff --git a/Source/CursesDialog/cmCursesCacheEntryComposite.h b/Source/CursesDialog/cmCursesCacheEntryComposite.h index b18f076..1ed60c8 100644 --- a/Source/CursesDialog/cmCursesCacheEntryComposite.h +++ b/Source/CursesDialog/cmCursesCacheEntryComposite.h @@ -5,6 +5,7 @@ #include "cmConfigure.h" // IWYU pragma: keep +#include #include class cmCursesLabelWidget; @@ -29,9 +30,9 @@ public: friend class cmCursesMainForm; protected: - cmCursesLabelWidget* Label; - cmCursesLabelWidget* IsNewLabel; - cmCursesWidget* Entry; + std::unique_ptr Label; + std::unique_ptr IsNewLabel; + std::unique_ptr Entry; std::string Key; int LabelWidth; int EntryWidth; diff --git a/Source/CursesDialog/cmCursesMainForm.cxx b/Source/CursesDialog/cmCursesMainForm.cxx index e754196..15ccc91 100644 --- a/Source/CursesDialog/cmCursesMainForm.cxx +++ b/Source/CursesDialog/cmCursesMainForm.cxx @@ -103,7 +103,7 @@ void cmCursesMainForm::InitializeUI() // dummy entry widget (does not respond to input) std::unique_ptr comp = cm::make_unique("EMPTY CACHE", 30, 30); - comp->Entry = new cmCursesDummyWidget(1, 1, 1, 1); + comp->Entry = cm::make_unique(1, 1, 1, 1); newEntries.emplace_back(std::move(comp)); } else { // Create the composites. -- cgit v0.12 From 0833486d62e8ed3ae9f0219f9924c4e78d74f781 Mon Sep 17 00:00:00 2001 From: Tushar Maheshwari Date: Sat, 21 Sep 2019 19:47:23 +0530 Subject: cmCursesStringWidget: remove manual delete - Relpace `char*` operations with `std::string` --- Source/CursesDialog/cmCursesStringWidget.cxx | 32 +++++++++++----------------- Source/CursesDialog/cmCursesStringWidget.h | 5 +---- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/Source/CursesDialog/cmCursesStringWidget.cxx b/Source/CursesDialog/cmCursesStringWidget.cxx index 3fc1858..d3a05e8 100644 --- a/Source/CursesDialog/cmCursesStringWidget.cxx +++ b/Source/CursesDialog/cmCursesStringWidget.cxx @@ -9,7 +9,6 @@ #include "cmStateTypes.h" #include -#include inline int ctrl(int z) { @@ -35,13 +34,13 @@ void cmCursesStringWidget::OnTab(cmCursesMainForm* /*unused*/, void cmCursesStringWidget::OnReturn(cmCursesMainForm* fm, WINDOW* /*unused*/) { - FORM* form = fm->GetForm(); if (this->InEdit) { cmCursesForm::LogMessage("String widget leaving edit."); this->InEdit = false; fm->PrintKeys(); - delete[] this->OriginalString; + this->OriginalString.clear(); // trick to force forms to update the field buffer + FORM* form = fm->GetForm(); form_driver(form, REQ_NEXT_FIELD); form_driver(form, REQ_PREV_FIELD); this->Done = true; @@ -49,9 +48,7 @@ void cmCursesStringWidget::OnReturn(cmCursesMainForm* fm, WINDOW* /*unused*/) cmCursesForm::LogMessage("String widget entering edit."); this->InEdit = true; fm->PrintKeys(); - char* buf = field_buffer(this->Field, 0); - this->OriginalString = new char[strlen(buf) + 1]; - strcpy(this->OriginalString, buf); + this->OriginalString = field_buffer(this->Field, 0); } } @@ -75,7 +72,7 @@ bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm, return false; } - this->OriginalString = nullptr; + this->OriginalString.clear(); this->Done = false; char debugMessage[128]; @@ -113,7 +110,7 @@ bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm, key == ctrl('p') || key == KEY_NPAGE || key == ctrl('d') || key == KEY_PPAGE || key == ctrl('u')) { this->InEdit = false; - delete[] this->OriginalString; + this->OriginalString.clear(); // trick to force forms to update the field buffer form_driver(form, REQ_NEXT_FIELD); form_driver(form, REQ_PREV_FIELD); @@ -125,7 +122,7 @@ bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm, this->InEdit = false; fm->PrintKeys(); this->SetString(this->OriginalString); - delete[] this->OriginalString; + this->OriginalString.clear(); touchwin(w); wrefresh(w); return true; @@ -188,23 +185,18 @@ bool cmCursesStringWidget::PrintKeys() } if (this->InEdit) { char fmt_s[] = "%s"; - char firstLine[512]; // Clean the toolbar - memset(firstLine, ' ', sizeof(firstLine)); - firstLine[511] = '\0'; curses_move(y - 4, 0); - printw(fmt_s, firstLine); - curses_move(y - 3, 0); - printw(fmt_s, firstLine); - curses_move(y - 2, 0); - printw(fmt_s, firstLine); - curses_move(y - 1, 0); - printw(fmt_s, firstLine); - + clrtoeol(); curses_move(y - 3, 0); printw(fmt_s, "Editing option, press [enter] to confirm"); + clrtoeol(); curses_move(y - 2, 0); printw(fmt_s, " press [esc] to cancel"); + clrtoeol(); + curses_move(y - 1, 0); + clrtoeol(); + return true; } return false; diff --git a/Source/CursesDialog/cmCursesStringWidget.h b/Source/CursesDialog/cmCursesStringWidget.h index 021515b..a41f0e8 100644 --- a/Source/CursesDialog/cmCursesStringWidget.h +++ b/Source/CursesDialog/cmCursesStringWidget.h @@ -23,9 +23,6 @@ class cmCursesStringWidget : public cmCursesWidget public: cmCursesStringWidget(int width, int height, int left, int top); - cmCursesStringWidget(cmCursesStringWidget const&) = delete; - cmCursesStringWidget& operator=(cmCursesStringWidget const&) = delete; - /** * Handle user input. Called by the container of this widget * when this widget has focus. Returns true if the input was @@ -65,7 +62,7 @@ public: protected: // true if the widget is in edit mode bool InEdit; - char* OriginalString; + std::string OriginalString; bool Done; }; -- cgit v0.12 From 7d6e08b438a187e89735ae6aad749a14137f34be Mon Sep 17 00:00:00 2001 From: Tushar Maheshwari Date: Sun, 22 Sep 2019 07:24:57 +0530 Subject: cmCursesMainForm: change Entries to object vector - Add move constructor and move assignment operator for `cmCursesCacheEntryComposite`. - Transfer ownership of Entries objects to std::vector. --- Source/CursesDialog/cmCursesCacheEntryComposite.h | 4 ++ Source/CursesDialog/cmCursesMainForm.cxx | 88 +++++++++++------------ Source/CursesDialog/cmCursesMainForm.h | 4 +- 3 files changed, 49 insertions(+), 47 deletions(-) diff --git a/Source/CursesDialog/cmCursesCacheEntryComposite.h b/Source/CursesDialog/cmCursesCacheEntryComposite.h index 1ed60c8..a711363 100644 --- a/Source/CursesDialog/cmCursesCacheEntryComposite.h +++ b/Source/CursesDialog/cmCursesCacheEntryComposite.h @@ -25,6 +25,10 @@ public: cmCursesCacheEntryComposite& operator=(cmCursesCacheEntryComposite const&) = delete; + cmCursesCacheEntryComposite(cmCursesCacheEntryComposite&&) = default; + cmCursesCacheEntryComposite& operator=(cmCursesCacheEntryComposite&&) = + default; + const char* GetValue(); friend class cmCursesMainForm; diff --git a/Source/CursesDialog/cmCursesMainForm.cxx b/Source/CursesDialog/cmCursesMainForm.cxx index 15ccc91..219771b 100644 --- a/Source/CursesDialog/cmCursesMainForm.cxx +++ b/Source/CursesDialog/cmCursesMainForm.cxx @@ -67,11 +67,10 @@ cmCursesMainForm::~cmCursesMainForm() // See if a cache entry is in the list of entries in the ui. bool cmCursesMainForm::LookForCacheEntry(const std::string& key) { - return std::any_of( - this->Entries.begin(), this->Entries.end(), - [&key](std::unique_ptr const& 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 @@ -79,7 +78,7 @@ void cmCursesMainForm::InitializeUI() { // Create a vector of cmCursesCacheEntryComposite's // which contain labels, entries and new entry markers - std::vector> newEntries; + std::vector newEntries; std::vector cacheKeys = this->CMakeInstance->GetState()->GetCacheEntryKeys(); newEntries.reserve(cacheKeys.size()); @@ -101,9 +100,8 @@ void cmCursesMainForm::InitializeUI() if (count == 0) { // If cache is empty, display a label saying so and a // dummy entry widget (does not respond to input) - std::unique_ptr comp = - cm::make_unique("EMPTY CACHE", 30, 30); - comp->Entry = cm::make_unique(1, 1, 1, 1); + cmCursesCacheEntryComposite comp("EMPTY CACHE", 30, 30); + comp.Entry = cm::make_unique(1, 1, 1, 1); newEntries.emplace_back(std::move(comp)); } else { // Create the composites. @@ -118,8 +116,8 @@ void cmCursesMainForm::InitializeUI() } if (!this->LookForCacheEntry(key)) { - newEntries.emplace_back(cm::make_unique( - key, this->CMakeInstance->GetState(), true, 30, entrywidth)); + newEntries.emplace_back(key, this->CMakeInstance->GetState(), true, 30, + entrywidth); this->OkToGenerate = false; } } @@ -134,8 +132,8 @@ void cmCursesMainForm::InitializeUI() } if (this->LookForCacheEntry(key)) { - newEntries.emplace_back(cm::make_unique( - key, this->CMakeInstance->GetState(), false, 30, entrywidth)); + newEntries.emplace_back(key, this->CMakeInstance->GetState(), false, + 30, entrywidth); } } } @@ -161,12 +159,12 @@ void cmCursesMainForm::RePost() } else { // If normal mode, count only non-advanced entries this->NumberOfVisibleEntries = 0; - for (std::unique_ptr& 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; } @@ -182,22 +180,22 @@ void cmCursesMainForm::RePost() this->Fields.reserve(3 * this->NumberOfVisibleEntries + 1); // Assign fields - for (std::unique_ptr& 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.push_back(entry->Label->Field); - this->Fields.push_back(entry->IsNewLabel->Field); - this->Fields.push_back(entry->Entry->Field); + 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 (this->Fields.empty()) { - const auto& front = *this->Entries.front(); + 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); @@ -241,12 +239,12 @@ void cmCursesMainForm::Render(int left, int top, int width, int height) } else { // If normal, display only non-advanced entries this->NumberOfVisibleEntries = 0; - for (std::unique_ptr& 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; } @@ -259,12 +257,12 @@ void cmCursesMainForm::Render(int left, int top, int width, int height) if (height > 0) { bool isNewPage; int i = 0; - for (std::unique_ptr& 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; } @@ -275,10 +273,10 @@ 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++; } } @@ -630,12 +628,12 @@ void cmCursesMainForm::RemoveEntry(const char* value) return; } - auto removeIt = std::find_if( - this->Entries.begin(), this->Entries.end(), - [value](std::unique_ptr& entry) -> bool { - const char* val = entry->GetValue(); - return val != nullptr && !strcmp(value, val); - }); + auto removeIt = + 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()) { this->CMakeInstance->UnwatchUnusedCli(value); @@ -646,13 +644,13 @@ void cmCursesMainForm::RemoveEntry(const char* value) // copy from the list box to the cache manager void cmCursesMainForm::FillCacheManagerFromUI() { - for (std::unique_ptr& 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 = @@ -943,12 +941,12 @@ void cmCursesMainForm::HandleInput() // make the next or prev. current field after deletion auto nextEntryIt = std::find_if( this->Entries.begin(), this->Entries.end(), - [&nextVal](std::unique_ptr& entry) { - return nextVal == entry->Key; + [&nextVal](cmCursesCacheEntryComposite const& entry) { + return nextVal == entry.Key; }); if (nextEntryIt != this->Entries.end()) { - set_current_field(this->Form, (*nextEntryIt)->Entry->Field); + set_current_field(this->Form, nextEntryIt->Entry->Field); } } } diff --git a/Source/CursesDialog/cmCursesMainForm.h b/Source/CursesDialog/cmCursesMainForm.h index 9a83c30..48d1791 100644 --- a/Source/CursesDialog/cmCursesMainForm.h +++ b/Source/CursesDialog/cmCursesMainForm.h @@ -5,6 +5,7 @@ #include "cmConfigure.h" // IWYU pragma: keep +#include "cmCursesCacheEntryComposite.h" #include "cmCursesForm.h" #include "cmCursesStandardIncludes.h" #include "cmStateTypes.h" @@ -14,7 +15,6 @@ #include #include -class cmCursesCacheEntryComposite; class cmake; /** \class cmCursesMainForm @@ -123,7 +123,7 @@ protected: void JumpToCacheEntry(const char* str); // Copies of cache entries stored in the user interface - std::vector> Entries; + std::vector Entries; // Errors produced during last run of cmake std::vector Errors; // Command line arguments to be passed to cmake each time -- cgit v0.12