Code reduction by using of Range-based for loop

This commit is contained in:
Markus Gans 2018-12-15 22:10:31 +01:00
parent cef13611d1
commit fd924233c3
7 changed files with 61 additions and 115 deletions

View File

@ -1,5 +1,6 @@
2018-12-15 Markus Gans <guru.mail@muenster.de>
* Use of the C++11 auto specifier in the program code
* Code reduction by using of Range-based for loop
2018-12-09 Markus Gans <guru.mail@muenster.de>
* Better handling of the scrollbar maximum

View File

@ -334,18 +334,14 @@ void FDialog::setSize (std::size_t w, std::size_t h, bool adjust)
if ( window_list && ! window_list->empty() )
{
bool overlaid = false;
auto iter = window_list->begin();
auto last = window_list->end();
while ( iter != last )
for (auto&& win : *window_list)
{
if ( overlaid )
putArea ((*iter)->getTermPos(), (*iter)->getVWin());
putArea (win->getTermPos(), win->getVWin());
if ( vwin == (*iter)->getVWin() )
if ( vwin == win->getVWin() )
overlaid = true;
++iter;
}
}
@ -729,14 +725,8 @@ void FDialog::onWindowRaised (FEvent*)
// Handle always-on-top windows
if ( always_on_top_list && ! always_on_top_list->empty() )
{
auto iter = always_on_top_list->begin();
auto last = always_on_top_list->end();
while ( iter != last )
{
putArea ((*iter)->getTermPos(), (*iter)->getVWin());
++iter;
}
for (auto&& win : *always_on_top_list)
putArea (win->getTermPos(), win->getVWin());
}
}
@ -749,14 +739,8 @@ void FDialog::onWindowLowered (FEvent*)
if ( window_list->empty() )
return;
auto iter = window_list->begin();
auto last = window_list->end();
while ( iter != last )
{
putArea ((*iter)->getTermPos(), (*iter)->getVWin());
++iter;
}
for (auto&& win : *window_list)
putArea (win->getTermPos(), win->getVWin());
}
@ -1198,18 +1182,14 @@ void FDialog::restoreOverlaidWindows()
return;
bool overlaid = false;
auto iter = window_list->begin();
auto last = window_list->end();
while ( iter != last )
for (auto&& win : *window_list)
{
if ( overlaid )
putArea ((*iter)->getTermPos(), (*iter)->getVWin());
putArea (win->getTermPos(), win->getVWin());
if ( vwin == (*iter)->getVWin() )
if ( vwin == win->getVWin() )
overlaid = true;
++iter;
}
}

View File

@ -471,14 +471,8 @@ void FFileDialog::clear()
return;
// delete all directory entries;
auto iter = dir_entries.begin();
auto last = dir_entries.end();
while ( iter != last )
{
std::free (iter->name);
++iter;
}
for (auto&& entry : dir_entries)
std::free (entry.name);
dir_entries.clear();
}
@ -490,17 +484,11 @@ int FFileDialog::numOfDirs()
return 0;
int n = 0;
auto iter = dir_entries.begin();
auto last = dir_entries.end();
while ( iter != last )
{
if ( iter->directory && std::strcmp(iter->name, ".") != 0 )
for (auto&& entry : dir_entries)
if ( entry.directory && std::strcmp(entry.name, ".") != 0 )
n++;
++iter;
}
return n;
}
@ -668,17 +656,33 @@ void FFileDialog::dirEntriesToList()
if ( dir_entries.empty() )
return;
auto iter = dir_entries.begin();
auto last = dir_entries.end();
while ( iter != last )
for (auto&& entry : dir_entries)
{
if ( iter->directory )
filebrowser.insert(FString(iter->name), fc::SquareBrackets);
if ( entry.directory )
filebrowser.insert(FString(entry.name), fc::SquareBrackets);
else
filebrowser.insert(FString(iter->name));
filebrowser.insert(FString(entry.name));
}
}
++iter;
//----------------------------------------------------------------------
void FFileDialog::selectDirectoryEntry (const char* const name)
{
if ( dir_entries.empty() )
return;
std::size_t i = 1;
for (auto&& entry : dir_entries)
{
if ( std::strcmp(entry.name, name) == 0 )
{
filebrowser.setCurrentItem(i);
filename.setText(FString(name) + '/');
break;
}
i++;
}
}
@ -712,26 +716,10 @@ int FFileDialog::changeDir (const FString& dirname)
{
if ( lastdir == FString('/') )
filename.setText('/');
else if ( ! dir_entries.empty() )
else
{
std::size_t i = 1;
const char* const baseName = \
basename(C_STR(lastdir.c_str()));
auto iter = dir_entries.begin();
auto last = dir_entries.end();
while ( iter != last )
{
if ( std::strcmp(iter->name, baseName) == 0 )
{
filebrowser.setCurrentItem(i);
filename.setText(FString(baseName) + '/');
break;
}
i++;
++iter;
}
auto baseName = basename(C_STR(lastdir.c_str()));
selectDirectoryEntry (baseName);
}
}
else
@ -807,21 +795,17 @@ void FFileDialog::cb_processActivate (FWidget*, data_ptr)
if ( ! dir_entries.empty() )
{
const FString& input = filename.getText().trim();
auto iter = dir_entries.begin();
auto last = dir_entries.end();
while ( iter != last )
for (auto&& entry : dir_entries)
{
if ( iter->name && input && ! input.isNull()
&& std::strcmp(iter->name, input) == 0
&& iter->directory )
if ( entry.name && input && ! input.isNull()
&& std::strcmp(entry.name, input) == 0
&& entry.directory )
{
found = true;
changeDir(input);
break;
}
++iter;
}
}

View File

@ -295,16 +295,12 @@ void FListBox::remove (std::size_t item)
element_count = getCount();
max_line_width = 0;
auto iter = itemlist.begin();
while ( iter != itemlist.end() )
for (auto&& item : itemlist)
{
std::size_t len = iter->getText().getLength();
std::size_t len = item.getText().getLength();
if ( len > max_line_width )
max_line_width = len;
++iter;
}
int hmax = ( max_line_width > getWidth() - nf_offset - 4 )

View File

@ -161,16 +161,9 @@ bool FStatusBar::hasActivatedKey()
{
if ( ! key_list.empty() )
{
auto iter = key_list.begin();
auto last = key_list.end();
while ( iter != last )
{
if ( (*iter)->isActivated() )
for (auto&& k : key_list)
if ( k->isActivated() )
return true;
++iter;
}
}
return false;

View File

@ -871,19 +871,15 @@ FVTerm::covered_state FVTerm::isCovered ( int x, int y
if ( ! area )
return non_covered;
auto is_covered = non_covered;
bool found = bool(area == vdesktop);
auto is_covered = non_covered;
auto w = static_cast<FWidget*>(area->widget);
if ( w->window_list && ! w->window_list->empty() )
{
auto iter = w->window_list->begin();
auto end = w->window_list->end();
for (; iter != end; ++iter)
for (auto&& win_obj : *w->window_list)
{
auto win = (*iter)->getVWin();
auto win = win_obj->getVWin();
if ( ! win )
continue;
@ -1706,12 +1702,9 @@ FVTerm::charData FVTerm::generateCharacter (int x, int y)
if ( ! widget->window_list || widget->window_list->empty() )
return *sc;
auto iter = widget->window_list->begin();
auto end = widget->window_list->end();
for (; iter != end; ++iter)
for (auto&& win_obj : *widget->window_list)
{
auto win = (*iter)->getVWin();
auto win = win_obj->getVWin();
if ( ! win || ! win->visible )
continue;
@ -1808,23 +1801,21 @@ FVTerm::charData FVTerm::getCharacter ( character_type char_type
// Get the window layer of this object
int layer = FWindow::getWindowLayer(w);
auto iter = w->window_list->begin();
auto end = w->window_list->end();
for (; iter != end; ++iter)
for (auto&& win_obj : *w->window_list)
{
bool significant_char;
// char_type can be "overlapped_character"
// or "covered_character"
if ( char_type == covered_character )
significant_char = bool(layer >= FWindow::getWindowLayer(*iter));
significant_char = bool(layer >= FWindow::getWindowLayer(win_obj));
else
significant_char = bool(layer < FWindow::getWindowLayer(*iter));
significant_char = bool(layer < FWindow::getWindowLayer(win_obj));
if ( obj && *iter != obj && significant_char )
if ( obj && win_obj != obj && significant_char )
{
auto win = (*iter)->getVWin();
auto win = win_obj->getVWin();
if ( ! win || ! win->visible )
continue;

View File

@ -173,6 +173,7 @@ class FFileDialog : public FDialog
void getEntry (const char* const, struct dirent*);
void followSymLink (const char* const, dir_entry&);
void dirEntriesToList();
void selectDirectoryEntry (const char* const);
int changeDir (const FString&);
void printPath (const FString&);
static const FString getHomeDir();