From 204a026335d6b655f340069f8c0dfdce7ed84980 Mon Sep 17 00:00:00 2001
From: Thibaut Cuvelier <cuvelier.thibaut@gmail.com>
Date: Sat, 2 May 2020 02:59:31 +0200
Subject: [PATCH 3/4] Rewriting of some code as modern C++

---
 src/BiblioInfo.cpp                | 12 ++----
 src/BranchList.cpp                | 29 +++----------
 src/Format.cpp                    | 69 ++++---------------------------
 src/IndicesList.cpp               | 52 ++++-------------------
 src/Lexer.cpp                     | 27 ++++++------
 src/TextClass.cpp                 | 26 +++---------
 src/insets/InsetCommandParams.cpp |  3 +-
 7 files changed, 42 insertions(+), 176 deletions(-)

diff --git a/src/BiblioInfo.cpp b/src/BiblioInfo.cpp
index e39768bea3..04c9077d38 100644
--- a/src/BiblioInfo.cpp
+++ b/src/BiblioInfo.cpp
@@ -1176,13 +1176,9 @@ docstring BibTeXInfo::getValueForKey(string const & oldkey, Buffer const & buf,
 namespace {
 
 // A functor for use with sort, leading to case insensitive sorting
-class compareNoCase
-{
-public:
-	bool operator()(docstring const & s1, docstring const & s2) const {
-		return compare_no_case(s1, s2) < 0;
-	}
-};
+bool compareNoCase(const docstring &a, const docstring &b) {
+	return compare_no_case(a, b) < 0;
+}
 
 } // namespace
 
@@ -1229,7 +1225,7 @@ vector<docstring> const BiblioInfo::getKeys() const
 	vector<docstring> bibkeys;
 	for (auto const & bi : *this)
 		bibkeys.push_back(bi.first);
-	sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
+	sort(bibkeys.begin(), bibkeys.end(), &compareNoCase);
 	return bibkeys;
 }
 
diff --git a/src/BranchList.cpp b/src/BranchList.cpp
index 22e78fb9dc..a640fca371 100644
--- a/src/BranchList.cpp
+++ b/src/BranchList.cpp
@@ -25,25 +25,6 @@ using namespace std;
 
 namespace lyx {
 
-namespace {
-
-class BranchNamesEqual
-{
-public:
-	BranchNamesEqual(docstring const & name)
-		: name_(name)
-	{}
-
-	bool operator()(Branch const & branch) const
-	{
-		return branch.branch() == name_;
-	}
-private:
-	docstring name_;
-};
-
-} // namespace
-
 
 Branch::Branch()
 	: selected_(false), filenameSuffix_(false)
@@ -125,7 +106,7 @@ void Branch::setColor(string const & str)
 Branch * BranchList::find(docstring const & name)
 {
 	List::iterator it =
-		find_if(list_.begin(), list_.end(), BranchNamesEqual(name));
+		find_if(list_.begin(), list_.end(), [name](const Branch& b) { return b.branch() == name; });
 	return it == list_.end() ? 0 : &*it;
 }
 
@@ -133,7 +114,7 @@ Branch * BranchList::find(docstring const & name)
 Branch const * BranchList::find(docstring const & name) const
 {
 	List::const_iterator it =
-		find_if(list_.begin(), list_.end(), BranchNamesEqual(name));
+		find_if(list_.begin(), list_.end(), [name](const Branch& b) { return b.branch() == name; });
 	return it == list_.end() ? 0 : &*it;
 }
 
@@ -152,7 +133,7 @@ bool BranchList::add(docstring const & s)
 		// Is this name already in the list?
 		bool const already =
 			find_if(list_.begin(), list_.end(),
-					 BranchNamesEqual(name)) != list_.end();
+				[name](const Branch& b) { return b.branch() == name; }) != list_.end();
 		if (!already) {
 			added = true;
 			Branch br;
@@ -172,7 +153,7 @@ bool BranchList::add(docstring const & s)
 bool BranchList::remove(docstring const & s)
 {
 	size_t const size = list_.size();
-	list_.remove_if(BranchNamesEqual(s));
+	list_.remove_if([s](const Branch& b) { return b.branch() == s; });
 	return size != list_.size();
 }
 
@@ -183,7 +164,7 @@ bool BranchList::rename(docstring const & oldname,
 	if (newname.empty())
 		return false;
 	if (find_if(list_.begin(), list_.end(),
-			BranchNamesEqual(newname)) != list_.end()) {
+	            [newname](const Branch& b) { return b.branch() == newname; }) != list_.end()) {
 		// new name already taken
 		if (merge)
 		      return remove(oldname);
diff --git a/src/Format.cpp b/src/Format.cpp
index 3200c7b3ab..722eb7420c 100644
--- a/src/Format.cpp
+++ b/src/Format.cpp
@@ -55,50 +55,6 @@ string const token_path_format("$$p");
 string const token_socket_format("$$a");
 
 
-class FormatNamesEqual {
-public:
-	FormatNamesEqual(string const & name)
-		: name_(name)
-	{}
-	bool operator()(Format const & f) const
-	{
-		return f.name() == name_;
-	}
-private:
-	string name_;
-};
-
-
-class FormatExtensionsEqual {
-public:
-	FormatExtensionsEqual(string const & extension)
-		: extension_(extension)
-	{}
-	bool operator()(Format const & f) const
-	{
-		return f.hasExtension(extension_);
-	}
-private:
-	string extension_;
-};
-
-
-class FormatMimeEqual {
-public:
-	FormatMimeEqual(string const & mime)
-		: mime_(mime)
-	{}
-	bool operator()(Format const & f) const
-	{
-		// The test for empty mime strings is needed since we allow
-		// formats with empty mime types.
-		return f.mime() == mime_ && !mime_.empty();
-	}
-private:
-	string mime_;
-};
-
-
 } // namespace
 
 bool Format::formatSorter(Format const * lhs, Format const * rhs)
@@ -169,15 +125,13 @@ void Format::setExtensions(string const & e)
 Format const * Formats::getFormat(string const & name) const
 {
 	FormatList::const_iterator cit =
-		find_if(formatlist_.begin(), formatlist_.end(),
-			FormatNamesEqual(name));
+		find_if(formatlist_.begin(), formatlist_.end(), [name](const Format &f) { return f.name() == name; });
 	if (cit != formatlist_.end())
 		return &(*cit);
 	else
 		return nullptr;
 }
 
-
 namespace {
 
 /** Guess the file format name (as in Format::name()) from contents.
@@ -419,8 +373,7 @@ string Formats::getFormatFromFile(FileName const & filename) const
 		if (!mime.empty() && mime != "application/octet-stream" &&
 			mime != "text/plain") {
 			Formats::const_iterator cit =
-				find_if(formatlist_.begin(), formatlist_.end(),
-						FormatMimeEqual(mime));
+				find_if(formatlist_.begin(), formatlist_.end(), [mime](const Format &f) { return f.mime() == mime; });
 			if (cit != formatlist_.end()) {
 				LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
 					   << mime << " -> " << cit->name());
@@ -485,8 +438,7 @@ string Formats::getFormatFromExtension(string const & ext) const
 		// this is ambigous if two formats have the same extension,
 		// but better than nothing
 		Formats::const_iterator cit =
-			find_if(formatlist_.begin(), formatlist_.end(),
-				FormatExtensionsEqual(ext));
+			find_if(formatlist_.begin(), formatlist_.end(), [ext](const Format &f) { return f.hasExtension(ext); });
 		if (cit != formatlist_.end()) {
 			LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
 				<< ext << " -> " << cit->name());
@@ -574,8 +526,7 @@ void Formats::setAutoOpen()
 int Formats::getNumber(string const & name) const
 {
 	FormatList::const_iterator cit =
-		find_if(formatlist_.begin(), formatlist_.end(),
-			FormatNamesEqual(name));
+		find_if(formatlist_.begin(), formatlist_.end(), [name](const Format &f) { return f.name() == name; });
 	if (cit != formatlist_.end())
 		return distance(formatlist_.begin(), cit);
 	else
@@ -597,8 +548,7 @@ void Formats::add(string const & name, string const & extensions,
 		  string const & mime, int flags)
 {
 	FormatList::iterator it =
-		find_if(formatlist_.begin(), formatlist_.end(),
-			FormatNamesEqual(name));
+		find_if(formatlist_.begin(), formatlist_.end(), [name](const Format &f) { return f.name() == name; });
 	if (it == formatlist_.end())
 		formatlist_.push_back(Format(name, extensions, prettyname,
 					    shortcut, viewer, editor, mime, flags));
@@ -611,8 +561,7 @@ void Formats::add(string const & name, string const & extensions,
 void Formats::erase(string const & name)
 {
 	FormatList::iterator it =
-		find_if(formatlist_.begin(), formatlist_.end(),
-			FormatNamesEqual(name));
+		find_if(formatlist_.begin(), formatlist_.end(), [name](const Format &f) { return f.name() == name; });
 	if (it != formatlist_.end())
 		formatlist_.erase(it);
 }
@@ -628,8 +577,7 @@ void Formats::setViewer(string const & name, string const & command)
 {
 	add(name);
 	FormatList::iterator it =
-		find_if(formatlist_.begin(), formatlist_.end(),
-			FormatNamesEqual(name));
+		find_if(formatlist_.begin(), formatlist_.end(), [name](const Format &f) { return f.name() == name; });
 	if (it != formatlist_.end())
 		it->setViewer(command);
 }
@@ -639,8 +587,7 @@ void Formats::setEditor(string const & name, string const & command)
 {
 	add(name);
 	FormatList::iterator it =
-		find_if(formatlist_.begin(), formatlist_.end(),
-			FormatNamesEqual(name));
+		find_if(formatlist_.begin(), formatlist_.end(), [name](const Format &f) { return f.name() == name; });
 	if (it != formatlist_.end())
 		it->setEditor(command);
 }
diff --git a/src/IndicesList.cpp b/src/IndicesList.cpp
index e666c246ff..160c438342 100644
--- a/src/IndicesList.cpp
+++ b/src/IndicesList.cpp
@@ -25,42 +25,6 @@ using namespace lyx::support;
 
 namespace lyx {
 
-namespace {
-
-class IndexNamesEqual
-{
-public:
-	IndexNamesEqual(docstring const & name)
-		: name_(name)
-	{}
-
-	bool operator()(Index const & index) const
-	{
-		return index.index() == name_;
-	}
-private:
-	docstring name_;
-};
-
-
-class IndexHasShortcut
-{
-public:
-	IndexHasShortcut(docstring const & shortcut)
-		: shortc_(shortcut)
-	{}
-
-	bool operator()(Index const & index) const
-	{
-		return index.shortcut() == shortc_;
-	}
-private:
-	docstring shortc_;
-};
-
-} // namespace
-
-
 /////////////////////////////////////////////////////////////////////
 //
 // Index
@@ -139,7 +103,7 @@ void Index::setColor(string const & str)
 Index * IndicesList::find(docstring const & name)
 {
 	List::iterator it =
-		find_if(list.begin(), list.end(), IndexNamesEqual(name));
+		find_if(list.begin(), list.end(), [name](const Index &i) { return i.index() == name; });
 	return it == list.end() ? nullptr : &*it;
 }
 
@@ -147,7 +111,7 @@ Index * IndicesList::find(docstring const & name)
 Index const * IndicesList::find(docstring const & name) const
 {
 	List::const_iterator it =
-		find_if(list.begin(), list.end(), IndexNamesEqual(name));
+		find_if(list.begin(), list.end(), [name](const Index &i) { return i.index() == name; });
 	return it == list.end() ? nullptr : &*it;
 }
 
@@ -155,7 +119,7 @@ Index const * IndicesList::find(docstring const & name) const
 Index * IndicesList::findShortcut(docstring const & shortcut)
 {
 	List::iterator it =
-		find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
+		find_if(list.begin(), list.end(), [shortcut](const Index &i) { return i.shortcut() == shortcut; });
 	return it == list.end() ? nullptr : &*it;
 }
 
@@ -163,7 +127,7 @@ Index * IndicesList::findShortcut(docstring const & shortcut)
 Index const * IndicesList::findShortcut(docstring const & shortcut) const
 {
 	List::const_iterator it =
-		find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
+		find_if(list.begin(), list.end(), [shortcut](const Index &i) { return i.shortcut() == shortcut; });
 	return it == list.end() ? nullptr : &*it;
 }
 
@@ -181,8 +145,7 @@ bool IndicesList::add(docstring const & n, docstring const & s)
 			name = n.substr(i, j - i);
 		// Is this name already in the list?
 		bool const already =
-			find_if(list.begin(), list.end(),
-				     IndexNamesEqual(name)) != list.end();
+			find_if(list.begin(), list.end(), [name](const Index &i) { return i.index() == name; }) != list.end();
 		if (!already) {
 			added = true;
 			Index in;
@@ -220,7 +183,7 @@ bool IndicesList::addDefault(docstring const & n)
 bool IndicesList::remove(docstring const & s)
 {
 	size_t const size = list.size();
-	list.remove_if(IndexNamesEqual(s));
+	list.remove_if([s](const Index &i) { return i.index() == s; });
 	return size != list.size();
 }
 
@@ -230,8 +193,7 @@ bool IndicesList::rename(docstring const & oldname,
 {
 	if (newname.empty())
 		return false;
-	if (find_if(list.begin(), list.end(),
-		    IndexNamesEqual(newname)) != list.end())
+	if (find_if(list.begin(), list.end(), [newname](const Index &i) { return i.index() == newname; }) != list.end())
 		// new name already taken
 		return false;
 
diff --git a/src/Lexer.cpp b/src/Lexer.cpp
index b5d8c24bc7..47d57c3eba 100644
--- a/src/Lexer.cpp
+++ b/src/Lexer.cpp
@@ -132,24 +132,21 @@ private:
 };
 
 
-
 namespace {
 
-class CompareTags {
-public:
-	// used by lower_bound, sort and sorted
-	bool operator()(LexerKeyword const & a, LexerKeyword const & b) const
-	{
-		// we use the ascii version, because in turkish, 'i'
-		// is not the lowercase version of 'I', and thus
-		// turkish locale breaks parsing of tags.
-		return compare_ascii_no_case(a.tag, b.tag) < 0;
-	}
-};
+// used by lower_bound, sort and sorted
+bool compareTags(LexerKeyword const & a, LexerKeyword const & b)
+{
+	// we use the ascii version, because in turkish, 'i'
+	// is not the lowercase version of 'I', and thus
+	// turkish locale breaks parsing of tags.
+	return compare_ascii_no_case(a.tag, b.tag) < 0;
+}
 
 } // namespace
 
 
+
 Lexer::Pimpl::Pimpl(LexerKeyword * tab, int num)
 	: is(&fb_), table(tab), no_items(num),
 	  status(0), lineno(0), commentChar('#')
@@ -195,14 +192,14 @@ void Lexer::Pimpl::verifyTable()
 {
 	// Check if the table is sorted and if not, sort it.
 	if (table
-	    && !lyx::sorted(table, table + no_items, CompareTags())) {
+	    && !lyx::sorted(table, table + no_items, &compareTags)) {
 		lyxerr << "The table passed to Lexer is not sorted!\n"
 		       << "Tell the developers to fix it!" << endl;
 		// We sort it anyway to avoid problems.
 		lyxerr << "\nUnsorted:" << endl;
 		printTable(lyxerr);
 
-		sort(table, table + no_items, CompareTags());
+		sort(table, table + no_items, &compareTags);
 		lyxerr << "\nSorted:" << endl;
 		printTable(lyxerr);
 	}
@@ -439,7 +436,7 @@ int Lexer::Pimpl::searchKeyword(char const * const tag) const
 	LexerKeyword search_tag = { tag, 0 };
 	LexerKeyword * res =
 		lower_bound(table, table + no_items,
-			    search_tag, CompareTags());
+			    search_tag, &compareTags);
 	// use the compare_ascii_no_case instead of compare_no_case,
 	// because in turkish, 'i' is not the lowercase version of 'I',
 	// and thus turkish locale breaks parsing of tags.
diff --git a/src/TextClass.cpp b/src/TextClass.cpp
index 1f4661220c..de0272f9c4 100644
--- a/src/TextClass.cpp
+++ b/src/TextClass.cpp
@@ -72,20 +72,6 @@ int const LYXFILE_LAYOUT_FORMAT = LAYOUT_FORMAT;
 
 namespace {
 
-class LayoutNamesEqual {
-public:
-	LayoutNamesEqual(docstring const & name)
-		: name_(name)
-	{}
-	bool operator()(Layout const & c) const
-	{
-		return c.name() == name_;
-	}
-private:
-	docstring name_;
-};
-
-
 bool layout2layout(FileName const & filename, FileName const & tempfile,
                    int const format = LAYOUT_FORMAT)
 {
@@ -1614,8 +1600,7 @@ bool TextClass::hasLayout(docstring const & n) const
 {
 	docstring const name = n.empty() ? defaultLayoutName() : n;
 
-	return find_if(layoutlist_.begin(), layoutlist_.end(),
-		       LayoutNamesEqual(name))
+	return find_if(layoutlist_.begin(), layoutlist_.end(), [name](const Layout &c) { return c.name() == name; })
 		!= layoutlist_.end();
 }
 
@@ -1634,7 +1619,7 @@ Layout const & TextClass::operator[](docstring const & name) const
 	LATTEST(!name.empty());
 
 	const_iterator it =
-		find_if(begin(), end(), LayoutNamesEqual(name));
+		find_if(begin(), end(), [name](const Layout &c) { return c.name() == name; });
 
 	if (it == end()) {
 		LYXERR0("We failed to find the layout '" << name
@@ -1656,7 +1641,7 @@ Layout & TextClass::operator[](docstring const & name)
 	LATTEST(!name.empty());
 	// Safe to continue, given what we do below.
 
-	iterator it = find_if(begin(), end(), LayoutNamesEqual(name));
+	iterator it = find_if(begin(), end(), [name](const Layout &c) { return c.name() == name; });
 
 	if (it == end()) {
 		LYXERR0("We failed to find the layout '" << to_utf8(name)
@@ -1668,7 +1653,7 @@ Layout & TextClass::operator[](docstring const & name)
 		LATTEST(false);
 		// we are here only in release mode
 		layoutlist_.push_back(createBasicLayout(name, true));
-		it = find_if(begin(), end(), LayoutNamesEqual(name));
+		it = find_if(begin(), end(), [name](const Layout &c) { return c.name() == name; });
 	}
 
 	return *it;
@@ -1681,8 +1666,7 @@ bool TextClass::deleteLayout(docstring const & name)
 		return false;
 
 	LayoutList::iterator it =
-		remove_if(layoutlist_.begin(), layoutlist_.end(),
-			  LayoutNamesEqual(name));
+		remove_if(layoutlist_.begin(), layoutlist_.end(), [name](const Layout &c) { return c.name() == name; });
 
 	LayoutList::iterator end = layoutlist_.end();
 	bool const ret = (it != end);
diff --git a/src/insets/InsetCommandParams.cpp b/src/insets/InsetCommandParams.cpp
index 10bd7a8a53..8dcf99c1ac 100644
--- a/src/insets/InsetCommandParams.cpp
+++ b/src/insets/InsetCommandParams.cpp
@@ -599,8 +599,7 @@ docstring InsetCommandParams::getCommand(OutputParams const & runparams) const
 docstring InsetCommandParams::getFirstNonOptParam() const
 {
 	ParamInfo::const_iterator it =
-		find_if(info_.begin(), info_.end(),
-			not_fn(mem_fn(&ParamInfo::ParamData::isOptional)));
+		find_if(info_.begin(), info_.end(), [](const ParamInfo::ParamData &d) { return !d.isOptional(); });
 	LASSERT(it != info_.end(), return docstring());
 	return (*this)[it->name()];
 }
-- 
2.26.1.windows.1

