Dear list,

Here is a patch that removes all uses of std::bind and boost::bind in
src/. I think this is something that we want in the long term, because
it makes the changed code much more readable and maintainable.

The thing is, at some point it made the call to
generateSyntheticMouseEvent() (file GuiWorkArea.cpp) cause a segfault
with gcc 4.6 (this is called when selecting text with the mouse and
moving the pointer above of below the screen).

I proofread the code and found nothing to change, nevertheless I was
unable to reproduce the problem today. I do not know whether this was
caused by a bad interaction between Boost::signals and lambdas, or a bad
implementation of lambdas in gcc 4.6 (or me being unable to read a manual).

I am conflicted about it, be cause I would like to be eventually able to
read and maintain things that have to do with concurrency, but I would
not like to introduce hard-to-debug crashes. It no longer crashes for
me, but I did not understand the cause of the previous crash.

Any advice? Also, since this touches a lot of sensitive code it would
not hurt if someone offered to proof-read it.


Guillaume
>From a6b8fb65eb88662f441e2a3dba77e783e0bb40df Mon Sep 17 00:00:00 2001
From: Guillaume Munch <g...@lyx.org>
Date: Sat, 4 Jun 2016 11:29:35 +0100
Subject: [PATCH] Remove support/functional.h and support/bind.h

Replace std::bind() with lambdas. This provides a much more readable code.
---
 src/Buffer.cpp                       |  12 ++--
 src/Buffer.h                         |   2 +-
 src/BufferList.cpp                   |  51 +++++------------
 src/Cursor.cpp                       |   7 +--
 src/LayoutFile.cpp                   |   1 -
 src/LyX.cpp                          |  11 ++--
 src/ParagraphMetrics.cpp             |   1 -
 src/Server.cpp                       |   5 +-
 src/ServerSocket.cpp                 |  17 +++---
 src/frontends/Application.h          |   6 +-
 src/frontends/qt4/GuiAlert.cpp       |  53 +++++++----------
 src/frontends/qt4/GuiApplication.cpp |   1 -
 src/frontends/qt4/GuiView.cpp        |  22 ++++---
 src/frontends/qt4/GuiWorkArea.cpp    |   8 +--
 src/frontends/qt4/InGuiThread.h      | 108 ++++++++---------------------------
 src/frontends/qt4/Toolbars.cpp       |   2 -
 src/graphics/GraphicsCacheItem.cpp   |   9 +--
 src/graphics/GraphicsConverter.cpp   |   3 +-
 src/graphics/GraphicsLoader.cpp      |   6 +-
 src/graphics/PreviewImage.cpp        |   4 +-
 src/graphics/PreviewLoader.cpp       |   5 +-
 src/insets/ExternalTransforms.cpp    |   2 +-
 src/insets/ExternalTransforms.h      |  17 +++---
 src/insets/InsetExternal.cpp         |   3 +-
 src/insets/InsetInclude.cpp          |   6 +-
 src/insets/InsetText.cpp             |   8 +--
 src/insets/RenderGraphic.cpp         |   6 +-
 src/insets/RenderPreview.cpp         |   8 +--
 src/support/FileMonitor.cpp          |   3 +-
 src/support/ForkedCalls.cpp          |  16 ++----
 src/support/Makefile.am              |   2 -
 src/support/bind.h                   |  26 ---------
 src/support/functional.h             |  23 --------
 33 files changed, 140 insertions(+), 314 deletions(-)
 delete mode 100644 src/support/bind.h
 delete mode 100644 src/support/functional.h

diff --git a/src/Buffer.cpp b/src/Buffer.cpp
index 12e3400..997cd6b 100644
--- a/src/Buffer.cpp
+++ b/src/Buffer.cpp
@@ -106,8 +106,6 @@
 #include "support/textutils.h"
 #include "support/types.h"
 
-#include "support/bind.h"
-
 #include <algorithm>
 #include <fstream>
 #include <iomanip>
@@ -2218,8 +2216,8 @@ void Buffer::validate(LaTeXFeatures & features) const
 	if (!features.runparams().is_child)
 		params().validate(features);
 
-	for_each(paragraphs().begin(), paragraphs().end(),
-		 bind(&Paragraph::validate, _1, ref(features)));
+	for (Paragraph const & p : paragraphs())
+		p.validate(features);
 
 	if (lyxerr.debugging(Debug::LATEX)) {
 		features.showStruct();
@@ -2829,10 +2827,8 @@ void Buffer::changeLanguage(Language const * from, Language const * to)
 {
 	LASSERT(from, return);
 	LASSERT(to, return);
-
-	for_each(par_iterator_begin(),
-		 par_iterator_end(),
-		 bind(&Paragraph::changeLanguage, _1, params(), from, to));
+	for_each(par_iterator_begin(), par_iterator_end(),
+	         [=](Paragraph & p){ p.changeLanguage(params(), from, to); });
 }
 
 
diff --git a/src/Buffer.h b/src/Buffer.h
index f488c4a..7acf1f3 100644
--- a/src/Buffer.h
+++ b/src/Buffer.h
@@ -687,7 +687,7 @@ private:
 	ExportStatus doExport(std::string const & target, bool put_in_tempdir,
 		bool includeall, std::string & result_file) const;
 	///
-	ExportStatus preview(std::string const & format, bool includeall = false) const;
+	ExportStatus preview(std::string const & format, bool includeall) const;
 	///
 	void setMathFlavor(OutputParams & op) const;
 
diff --git a/src/BufferList.cpp b/src/BufferList.cpp
index 9a5c4e1..0b76f13 100644
--- a/src/BufferList.cpp
+++ b/src/BufferList.cpp
@@ -32,10 +32,8 @@
 #include "support/Package.h"
 
 #include "support/lassert.h"
-#include "support/bind.h"
 
 #include <algorithm>
-#include <functional>
 #include <iterator>
 #include <memory>
 
@@ -277,55 +275,34 @@ bool BufferList::isOthersChild(Buffer * parent, Buffer * child)
 	Buffer const * parent_ = child->parent();
 	if (parent_ && parent_ != parent)
 		return true;
-	
-	BufferStorage::iterator it = bstore.begin();
-	BufferStorage::iterator end = bstore.end();
-	for (; it != end; ++it) {
-		Buffer * buf = *it;
+
+	for(Buffer * buf : bstore)
 		if (buf != parent && buf->isChild(child))
 			return true;
-	}
 	return false;
 }
 
 
-namespace {
-
-struct equivalent_to : public binary_function<FileName, FileName, bool>
-{
-	bool operator()(FileName const & x, FileName const & y) const
-	{ return equivalent(x, y); }
-};
-
-}
-
-
 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
 {
 	// 1) cheap test, using string comparison of file names
-	BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
-		lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
-	if (it != bstore.end())
-		return *it;
+	for (Buffer * b : bstore)
+		if (b->fileName() == fname)
+			return b;
 	// 2) possibly expensive test, using equivalence test of file names
-	it = find_if(bstore.begin(), bstore.end(),
-		lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
-	if (it != bstore.end())
-		return *it;
-
+	for (Buffer * b : bstore)
+		if (equivalent(b->fileName(), fname))
+			return b;
 	if (internal) {
 		// 1) cheap test, using string comparison of file names
-		BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
-			lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
-		if (it != binternal.end())
-			return *it;
+		for (Buffer * b : binternal)
+			if (b->fileName() == fname)
+				return b;
 		// 2) possibly expensive test, using equivalence test of file names
-		it = find_if(binternal.begin(), binternal.end(),
-			     lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
-		if (it != binternal.end())
-			return *it;
+		for (Buffer * b : binternal)
+			if (equivalent(b->fileName(), fname))
+				return b;
 	}
-
 	return 0;
 }
 
diff --git a/src/Cursor.cpp b/src/Cursor.cpp
index baf529b..2894f9d 100644
--- a/src/Cursor.cpp
+++ b/src/Cursor.cpp
@@ -51,8 +51,6 @@
 #include "mathed/MathData.h"
 #include "mathed/MathMacro.h"
 
-#include "support/bind.h"
-
 #include <sstream>
 #include <limits>
 #include <map>
@@ -1184,9 +1182,8 @@ void Cursor::plainInsert(MathAtom const & t)
 
 void Cursor::insert(docstring const & str)
 {
-	for_each(str.begin(), str.end(),
-		 bind(static_cast<void(Cursor::*)(char_type)>
-			     (&Cursor::insert), this, _1));
+	for (char_type c : str)
+		insert(c);
 }
 
 
diff --git a/src/LayoutFile.cpp b/src/LayoutFile.cpp
index 8cca663..771a9c4 100644
--- a/src/LayoutFile.cpp
+++ b/src/LayoutFile.cpp
@@ -27,7 +27,6 @@
 #include "support/lassert.h"
 #include "support/lstrings.h"
 
-#include "support/bind.h"
 #include "support/regex.h"
 #include "support/TempFile.h"
 
diff --git a/src/LyX.cpp b/src/LyX.cpp
index 66ff6f3..a732c7e 100644
--- a/src/LyX.cpp
+++ b/src/LyX.cpp
@@ -50,7 +50,6 @@
 #include "frontends/alert.h"
 #include "frontends/Application.h"
 
-#include "support/bind.h"
 #include "support/ConsoleApplication.h"
 #include "support/lassert.h"
 #include "support/debug.h"
@@ -65,8 +64,9 @@
 #include "support/unique_ptr.h"
 
 #include <algorithm>
-#include <iostream>
 #include <csignal>
+#include <iostream>
+#include <functional>
 #include <map>
 #include <stdlib.h>
 #include <string>
@@ -492,9 +492,8 @@ int LyX::execWithoutGui(int & argc, char * argv[])
 		LYXERR(Debug::FILES, "Loading " << fname);
 		if (buf && buf->loadLyXFile() == Buffer::ReadSuccess) {
 			ErrorList const & el = buf->errorList("Parse");
-			if (!el.empty())
-					for_each(el.begin(), el.end(),
-									 bind(&LyX::printError, this, _1));
+			for(ErrorItem const & e : el)
+				printError(e);
 			command_line_buffers.push_back(buf);
 		} else {
 			if (buf)
@@ -1109,7 +1108,7 @@ bool LyX::readEncodingsFile(string const & enc_name,
 namespace {
 
 /// return the the number of arguments consumed
-typedef boost::function<int(string const &, string const &, string &)> cmd_helper;
+typedef function<int(string const &, string const &, string &)> cmd_helper;
 
 int parse_dbg(string const & arg, string const &, string &)
 {
diff --git a/src/ParagraphMetrics.cpp b/src/ParagraphMetrics.cpp
index 1fd248f..01db6cb 100644
--- a/src/ParagraphMetrics.cpp
+++ b/src/ParagraphMetrics.cpp
@@ -47,7 +47,6 @@
 #include "support/lstrings.h"
 #include "support/textutils.h"
 
-#include "support/bind.h"
 #include <boost/crc.hpp>
 
 #include <algorithm>
diff --git a/src/Server.cpp b/src/Server.cpp
index c88ae82..556431d 100644
--- a/src/Server.cpp
+++ b/src/Server.cpp
@@ -56,8 +56,6 @@
 #include "support/lstrings.h"
 #include "support/os.h"
 
-#include "support/bind.h"
-
 #include <iostream>
 
 #ifdef _WIN32
@@ -859,8 +857,7 @@ int LyXComm::startPipe(string const & file, bool write)
 	}
 
 	if (!write) {
-		theApp()->registerSocketCallback(fd,
-			bind(&LyXComm::read_ready, this));
+		theApp()->registerSocketCallback(fd, [this]() { read_ready(); });
 	}
 
 	return fd;
diff --git a/src/ServerSocket.cpp b/src/ServerSocket.cpp
index 72a70d4..4baef01 100644
--- a/src/ServerSocket.cpp
+++ b/src/ServerSocket.cpp
@@ -26,11 +26,13 @@
 #include "support/debug.h"
 #include "support/environment.h"
 #include "support/FileName.h"
+#include "support/lassert.h"
 #include "support/socktools.h"
 
-#include "support/bind.h"
+#include <boost/assert.hpp>
 
 #include <cerrno>
+#include <cstring>
 #include <ostream>
 
 #if defined (_WIN32)
@@ -61,10 +63,7 @@ ServerSocket::ServerSocket(FileName const & addr)
 	// Needed by lyxclient
 	setEnv("LYXSOCKET", address_.absFileName());
 
-	theApp()->registerSocketCallback(
-		fd_,
-		bind(&ServerSocket::serverCallback, this)
-		);
+	theApp()->registerSocketCallback(fd_, [this]() { serverCallback(); });
 
 	LYXERR(Debug::LYXSERVER, "lyx: New server socket "
 				 << fd_ << ' ' << address_.absFileName());
@@ -111,11 +110,9 @@ void ServerSocket::serverCallback()
 
 	// Register the new client.
 	clients[client_fd] = make_shared<LyXDataSocket>(client_fd);
-	theApp()->registerSocketCallback(
-		client_fd,
-		bind(&ServerSocket::dataCallback,
-			    this, client_fd)
-		);
+	theApp()->registerSocketCallback(client_fd, [=]() {
+			dataCallback(client_fd);
+		});
 }
 
 
diff --git a/src/frontends/Application.h b/src/frontends/Application.h
index df0be95..b321052 100644
--- a/src/frontends/Application.h
+++ b/src/frontends/Application.h
@@ -18,7 +18,7 @@
 
 #include "support/strfwd.h"
 
-#include <boost/function.hpp>
+#include <functional>
 
 #include <vector>
 
@@ -216,12 +216,12 @@ public:
 	*      passing Color_white returns "ffffff".
 	*/
 	virtual std::string const hexName(ColorCode col) = 0;
-	
+
 	/**
 	* add a callback for socket read notification
 	* @param fd socket descriptor (file/socket/etc)
 	*/
-	typedef boost::function<void()> SocketCallback;
+	typedef std::function<void()> SocketCallback;
 	virtual void registerSocketCallback(int fd, SocketCallback func) = 0;
 
 	/**
diff --git a/src/frontends/qt4/GuiAlert.cpp b/src/frontends/qt4/GuiAlert.cpp
index 4c9b162..9710f38 100644
--- a/src/frontends/qt4/GuiAlert.cpp
+++ b/src/frontends/qt4/GuiAlert.cpp
@@ -50,6 +50,20 @@ namespace lyx {
 namespace frontend {
 
 
+namespace {
+
+template<typename F>
+typename std::result_of<F()>::type call(F f) {
+#if EXPORT_in_THREAD
+	return call_in_gui_thread(f);
+#else
+	return f();
+#endif
+}
+
+} // anon namespace
+
+
 void noAppDialog(QString const & title, QString const & msg, QMessageBox::Icon mode)
 {
 	int argc = 1;
@@ -136,13 +150,10 @@ int prompt(docstring const & title0, docstring const & question,
 		  docstring const & b1, docstring const & b2,
 		  docstring const & b3, docstring const & b4)
 {
-#ifdef EXPORT_in_THREAD
-	return InGuiThread<int>().call(&doPrompt,
-#else
-	return doPrompt(
-#endif
-				title0, question, default_button,
-				cancel_button, b1, b2, b3, b4);
+	return call([&, default_button, cancel_button] () {
+			return doPrompt(title0, question, default_button,
+			                cancel_button, b1, b2, b3, b4);
+		});
 }
 
 void doWarning(docstring const & title0, docstring const & message,
@@ -190,12 +201,7 @@ void doWarning(docstring const & title0, docstring const & message,
 void warning(docstring const & title0, docstring const & message,
 	     bool const & askshowagain)
 {
-#ifdef EXPORT_in_THREAD	
-	InGuiThread<void>().call(&doWarning,
-#else
-	doWarning(
-#endif
-				title0, message, askshowagain);
+	call([&] () { doWarning(title0, message, askshowagain); });
 }
 
 void doError(docstring const & title0, docstring const & message, bool backtrace)
@@ -240,12 +246,7 @@ void doError(docstring const & title0, docstring const & message, bool backtrace
 
 void error(docstring const & title0, docstring const & message, bool backtrace)
 {
-#ifdef EXPORT_in_THREAD
-	InGuiThread<void>().call(&doError, 
-#else
-	doError(
-#endif
-				title0, message, backtrace);
+	call([&, backtrace] () { doError(title0, message, backtrace); });
 }
 
 void doInformation(docstring const & title0, docstring const & message)
@@ -285,12 +286,7 @@ void doInformation(docstring const & title0, docstring const & message)
 
 void information(docstring const & title0, docstring const & message)
 {
-#ifdef EXPORT_in_THREAD
-	InGuiThread<void>().call(&doInformation,
-#else
-	doInformation(
-#endif
-				title0, message);
+	call ([&] () { doInformation(title0, message); });
 }
 
 bool doAskForText(docstring & response, docstring const & msg,
@@ -335,12 +331,7 @@ bool doAskForText(docstring & response, docstring const & msg,
 bool askForText(docstring & response, docstring const & msg,
 	docstring const & dflt)
 {
-#ifdef EXPORT_in_THREAD
-	return InGuiThread<bool>().call(&doAskForText,
-#else
-	return doAskForText(
-#endif
-				response, msg, dflt);
+	return call([&] () { return doAskForText(response, msg, dflt); });
 }
 
 } // namespace Alert
diff --git a/src/frontends/qt4/GuiApplication.cpp b/src/frontends/qt4/GuiApplication.cpp
index eb8c305..074492b 100644
--- a/src/frontends/qt4/GuiApplication.cpp
+++ b/src/frontends/qt4/GuiApplication.cpp
@@ -146,7 +146,6 @@
 #include <QMacPasteboardMime>
 #endif // Q_OS_MAC
 
-#include "support/bind.h"
 #include <boost/crc.hpp>
 
 #include <exception>
diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp
index 429def5..b2edb03 100644
--- a/src/frontends/qt4/GuiView.cpp
+++ b/src/frontends/qt4/GuiView.cpp
@@ -125,8 +125,6 @@
 #define EXPORT_in_THREAD 1
 
 
-#include "support/bind.h"
-
 #include <sstream>
 
 #ifdef HAVE_SYS_TIME_H
@@ -531,7 +529,7 @@ GuiView::GuiView(int id)
 
 	// Start autosave timer
 	if (lyxrc.autosave) {
-		d.autosave_timeout_.timeout.connect(bind(&GuiView::autoSave, this));
+		d.autosave_timeout_.timeout.connect([this]() { autoSave(); });
 		d.autosave_timeout_.setTimeout(lyxrc.autosave * 1000);
 		d.autosave_timeout_.start();
 	}
@@ -3385,22 +3383,28 @@ Buffer::ExportStatus GuiView::GuiViewPrivate::runAndDestroy(const T& func, Buffe
 
 Buffer::ExportStatus GuiView::GuiViewPrivate::compileAndDestroy(Buffer const * orig, Buffer * clone, string const & format)
 {
-	Buffer::ExportStatus (Buffer::* mem_func)(std::string const &, bool) const = &Buffer::doExport;
-	return runAndDestroy(lyx::bind(mem_func, clone, _1, true), orig, clone, format);
+	auto compile = [&](std::string const & s) {
+		return clone->doExport(s, true);
+	};
+	return runAndDestroy(compile, orig, clone, format);
 }
 
 
 Buffer::ExportStatus GuiView::GuiViewPrivate::exportAndDestroy(Buffer const * orig, Buffer * clone, string const & format)
 {
-	Buffer::ExportStatus (Buffer::* mem_func)(std::string const &, bool) const = &Buffer::doExport;
-	return runAndDestroy(lyx::bind(mem_func, clone, _1, false), orig, clone, format);
+	auto exporte = [&](std::string const & s) {
+		return clone->doExport(s, false);
+	};
+	return runAndDestroy(exporte, orig, clone, format);
 }
 
 
 Buffer::ExportStatus GuiView::GuiViewPrivate::previewAndDestroy(Buffer const * orig, Buffer * clone, string const & format)
 {
-	Buffer::ExportStatus (Buffer::* mem_func)(std::string const &) const = &Buffer::preview;
-	return runAndDestroy(lyx::bind(mem_func, clone, _1), orig, clone, format);
+	auto preview = [&](std::string const & s) {
+		return clone->preview(s);
+	};
+	return runAndDestroy(preview, orig, clone, format);
 }
 
 
diff --git a/src/frontends/qt4/GuiWorkArea.cpp b/src/frontends/qt4/GuiWorkArea.cpp
index b8886eb..f618702 100644
--- a/src/frontends/qt4/GuiWorkArea.cpp
+++ b/src/frontends/qt4/GuiWorkArea.cpp
@@ -78,8 +78,6 @@
 #include <QToolTip>
 #include <QMenuBar>
 
-#include "support/bind.h"
-
 #include <cmath>
 
 int const TabIndicatorWidth = 3;
@@ -319,9 +317,9 @@ void GuiWorkArea::init()
 
 	d->setCursorShape(Qt::IBeamCursor);
 
-	d->synthetic_mouse_event_.timeout.timeout.connect(
-		bind(&GuiWorkArea::generateSyntheticMouseEvent,
-					this));
+	d->synthetic_mouse_event_.timeout.timeout.connect([this]() {
+			generateSyntheticMouseEvent();
+		});
 
 	// Initialize the vertical Scroll Bar
 	QObject::connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
diff --git a/src/frontends/qt4/InGuiThread.h b/src/frontends/qt4/InGuiThread.h
index 86c4afe..71595f8 100644
--- a/src/frontends/qt4/InGuiThread.h
+++ b/src/frontends/qt4/InGuiThread.h
@@ -16,13 +16,22 @@
 #include <QObject>
 #include <QWaitCondition>
 
-#include "support/bind.h"
-#include "support/functional.h"
+#include <functional>
 
 namespace lyx {
 namespace frontend {
 
 
+// calls a function f of type R() in the GUI thread, and returns the result of
+// type R.
+template<typename F>
+typename std::result_of<F()>::type call_in_gui_thread(F f);
+
+
+//
+// Template implementation
+//
+
 class IntoGuiThreadMover : public QObject
 {
 	Q_OBJECT
@@ -51,10 +60,9 @@ template<class R>
 class InGuiThread : private IntoGuiThreadMover
 {
 public:
-
 	// please coverity by explicitly initalizing this variable.
 	InGuiThread() : return_value_(R()) {}
-
+	///
 	template<class F>
 	R call(F f)
 	{
@@ -63,51 +71,15 @@ public:
 		return return_value_;
 	}
 
-	template<class F, class P1>
-	R call(F f, P1& p1)
-	{
-		return call(lyx::bind(f, lyx::ref(p1)));
-	}
-
-	template<class F, class P1, class P2>
-	R call(F f, P1& p1, P2& p2)
-	{
-		return call(lyx::bind(f, lyx::ref(p1), lyx::ref(p2)));
-	}
-
-	template<class F, class P1, class P2, class P3>
-	R call(F f, P1& p1, P2& p2, P3& p3)
-	{
-		return call(lyx::bind(f, lyx::ref(p1), lyx::ref(p2), lyx::ref(p3)));
-	}
-
-	template<class F, class P1, class P2, class P3, class P4>
-	R call(F f, P1& p1, P2& p2, P3& p3, P4& p4)
-	{
-		return call(lyx::bind(f, lyx::ref(p1), lyx::ref(p2), lyx::ref(p3), lyx::ref(p4)));
-	}
-
-	/*
-	  ...
-	*/
-
-	template<class F, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>
-	R call(F f, P1& p1, P2& p2, P3& p3, P4& p4, P5& p5, P6& p6, P7& p7, P8& p8)
-	{
-		return call(lyx::bind(f, lyx::ref(p1), lyx::ref(p2), lyx::ref(p3), lyx::ref(p4), 
-			lyx::ref(p5), lyx::ref(p6), lyx::ref(p7), lyx::ref(p8)));
-	}
-
 private:
-
 	void synchronousFunctionCall()
 	{
 		return_value_ = func_();
 	}
-
-private:
+	///
 	R return_value_;
-	function<R()> func_;
+	///
+	std::function<R()> func_;
 };
 
 
@@ -116,9 +88,8 @@ template<>
 class InGuiThread<void> : private IntoGuiThreadMover
 {
 public:
-
 	InGuiThread() {}
-
+	///
 	template<class F>
 	void call(F f)
 	{
@@ -126,53 +97,22 @@ public:
 		callInGuiThread();
 	}
 
-	template<class F, class P1>
-	void call(F f, P1& p1)
-	{
-		call(lyx::bind(f, lyx::ref(p1)));
-	}
-
-	template<class F, class P1, class P2>
-	void call(F f, P1& p1, P2& p2)
-	{
-		call(lyx::bind(f, lyx::ref(p1), lyx::ref(p2)));
-	}
-
-	template<class F, class P1, class P2, class P3>
-	void call(F f, P1& p1, P2& p2, P3& p3)
-	{
-		call(lyx::bind(f, lyx::ref(p1), lyx::ref(p2), lyx::ref(p3)));
-	}
-
-	template<class F, class P1, class P2, class P3, class P4>
-	void call(F f, P1& p1, P2& p2, P3& p3, P4& p4)
-	{
-		call(lyx::bind(f, lyx::ref(p1), lyx::ref(p2), lyx::ref(p3), lyx::ref(p4)));
-	}
-
-	/*
-	  ...
-	*/
-
-	template<class F, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>
-	void call(F f, P1& p1, P2& p2, P3& p3, P4& p4, P5& p5, P6& p6, P7& p7, P8& p8)
-	{
-		call(bind(f, lyx::ref(p1), lyx::ref(p2), lyx::ref(p3), lyx::ref(p4), lyx::ref(p5),
-			lyx::ref(p6), lyx::ref(p7), lyx::ref(p8)));
-	}
-
 private:
-
 	void synchronousFunctionCall()
 	{
 		func_();
 	}
-
-private:
-	function<void()> func_;
+	///
+	std::function<void()> func_;
 };
 
 
+template<typename F>
+typename std::result_of<F()>::type call_in_gui_thread(F f) {
+	return InGuiThread<typename std::result_of<F()>::type>().call(f);
+}
+
+
 } // namespace frontend
 } // namespace lyx
 
diff --git a/src/frontends/qt4/Toolbars.cpp b/src/frontends/qt4/Toolbars.cpp
index 20eee93..19b9560 100644
--- a/src/frontends/qt4/Toolbars.cpp
+++ b/src/frontends/qt4/Toolbars.cpp
@@ -23,8 +23,6 @@
 #include "support/gettext.h"
 #include "support/lstrings.h"
 
-#include "support/bind.h"
-
 #include <algorithm>
 
 using namespace std;
diff --git a/src/graphics/GraphicsCacheItem.cpp b/src/graphics/GraphicsCacheItem.cpp
index 1a7279c..2709c45 100644
--- a/src/graphics/GraphicsCacheItem.cpp
+++ b/src/graphics/GraphicsCacheItem.cpp
@@ -26,8 +26,8 @@
 #include "support/filetools.h"
 #include "support/FileMonitor.h"
 #include "support/lassert.h"
+#include "support/unique_ptr.h"
 
-#include "support/bind.h"
 #include "support/TempFile.h"
 
 using namespace std;
@@ -210,7 +210,7 @@ CacheItem::Impl::Impl(FileName const & file)
 	  remove_loaded_file_(false),
 	  status_(WaitingToLoad)
 {
-	monitor_.connect(bind(&Impl::startLoading, this));
+	monitor_.connect([this]() { startLoading(); });
 }
 
 
@@ -437,8 +437,9 @@ void CacheItem::Impl::convertToDisplayFormat()
 	// Connect a signal to this->imageConverted and pass this signal to
 	// the graphics converter so that we can load the modified file
 	// on completion of the conversion process.
-	converter_.reset(new Converter(filename, to_file_base.absFileName(), from, to_));
-	converter_->connect(bind(&Impl::imageConverted, this, _1));
+	converter_ = make_unique<Converter>(filename, to_file_base.absFileName(),
+	                                    from, to_);
+	converter_->connect([this](bool b) { imageConverted(b); });
 	converter_->startConversion();
 }
 
diff --git a/src/graphics/GraphicsConverter.cpp b/src/graphics/GraphicsConverter.cpp
index ebb074c..511bf73 100644
--- a/src/graphics/GraphicsConverter.cpp
+++ b/src/graphics/GraphicsConverter.cpp
@@ -24,7 +24,6 @@
 #include "support/lstrings.h"
 #include "support/os.h"
 
-#include "support/bind.h"
 #include "support/TempFile.h"
 
 #include <sstream>
@@ -181,7 +180,7 @@ void Converter::Impl::startConversion()
 
 	ForkedCall::SignalTypePtr ptr =
 		ForkedCallQueue::add(script_command_);
-	ptr->connect(bind(&Impl::converted, this, _1, _2));
+	ptr->connect([this](pid_t p, int retval) { converted(p, retval); });
 }
 
 
diff --git a/src/graphics/GraphicsLoader.cpp b/src/graphics/GraphicsLoader.cpp
index 1fa5c65..8aeb972 100644
--- a/src/graphics/GraphicsLoader.cpp
+++ b/src/graphics/GraphicsLoader.cpp
@@ -20,8 +20,6 @@
 #include "support/debug.h"
 #include "support/Timeout.h"
 
-#include "support/bind.h"
-
 #include <queue>
 #include <memory>
 #include <set>
@@ -108,7 +106,7 @@ void LoaderQueue::loadNext()
 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
 			     running_(false)
 {
-	timer.timeout.connect(bind(&LoaderQueue::loadNext, this));
+	timer.timeout.connect([this]() { loadNext(); });
 }
 
 
@@ -414,7 +412,7 @@ void Loader::Impl::resetFile(FileName const & file)
 	if (continue_monitoring && !cached_item_->monitoring())
 		cached_item_->startMonitoring();
 
-	sc_ = cached_item_->connect(bind(&Impl::statusChanged, this));
+	sc_ = cached_item_->connect([this]() { statusChanged(); });
 }
 
 
diff --git a/src/graphics/PreviewImage.cpp b/src/graphics/PreviewImage.cpp
index 80e8e20..ad09f72 100644
--- a/src/graphics/PreviewImage.cpp
+++ b/src/graphics/PreviewImage.cpp
@@ -20,8 +20,6 @@
 
 #include "support/FileName.h"
 
-#include "support/bind.h"
-
 using namespace std;
 using namespace lyx::support;
 
@@ -107,7 +105,7 @@ PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
 	  snippet_(s), ascent_frac_(af)
 {
 	iloader_.setDisplayPixelRatio(l.displayPixelRatio());
-	iloader_.connect(bind(&Impl::statusChanged, this));
+	iloader_.connect([this]() { statusChanged(); });
 }
 
 
diff --git a/src/graphics/PreviewLoader.cpp b/src/graphics/PreviewLoader.cpp
index bc174da..600a3fe 100644
--- a/src/graphics/PreviewLoader.cpp
+++ b/src/graphics/PreviewLoader.cpp
@@ -37,7 +37,6 @@
 #include "support/ForkedCalls.h"
 #include "support/lstrings.h"
 
-#include "support/bind.h"
 #include "support/TempFile.h"
 
 #include <fstream>
@@ -741,7 +740,9 @@ void PreviewLoader::Impl::startLoading(bool wait)
 	// Initiate the conversion from LaTeX to bitmap images files.
 	ForkedCall::SignalTypePtr
 		convert_ptr(new ForkedCall::SignalType);
-	convert_ptr->connect(bind(&Impl::finishedGenerating, this, _1, _2));
+	convert_ptr->connect([this](pid_t p, int retval) {
+			finishedGenerating(p, retval);
+		});
 
 	ForkedCall call(buffer_.filePath());
 	int ret = call.startScript(command, convert_ptr);
diff --git a/src/insets/ExternalTransforms.cpp b/src/insets/ExternalTransforms.cpp
index ee31e19..7ac41ab 100644
--- a/src/insets/ExternalTransforms.cpp
+++ b/src/insets/ExternalTransforms.cpp
@@ -338,7 +338,7 @@ void extractIt(boost::any const & any_factory,
 		return;
 
 	Factory factory = boost::any_cast<Factory>(any_factory);
-	if (!factory.empty())
+	if (!factory)
 		transformer = factory(data);
 }
 
diff --git a/src/insets/ExternalTransforms.h b/src/insets/ExternalTransforms.h
index 1d81900..cd3e5e2 100644
--- a/src/insets/ExternalTransforms.h
+++ b/src/insets/ExternalTransforms.h
@@ -19,11 +19,12 @@
 #include "support/unique_ptr.h"
 
 #include <boost/any.hpp>
-#include <boost/function.hpp>
 
-#include <string>
+#include <functional>
 #include <map>
 #include <memory>
+#include <string>
+
 
 namespace lyx {
 
@@ -317,17 +318,17 @@ enum TransformID {
 };
 
 
-typedef boost::function<TransformOption::ptr_type(ClipData)>
+typedef std::function<TransformOption::ptr_type(ClipData)>
 	ClipOptionFactory;
-typedef boost::function<TransformOption::ptr_type(std::string)>
+typedef std::function<TransformOption::ptr_type(std::string)>
 	ExtraOptionFactory;
-typedef boost::function<TransformOption::ptr_type(ResizeData)>
+typedef std::function<TransformOption::ptr_type(ResizeData)>
 	ResizeOptionFactory;
-typedef boost::function<TransformOption::ptr_type(RotationData)>
+typedef std::function<TransformOption::ptr_type(RotationData)>
 	RotationOptionFactory;
-typedef boost::function<TransformCommand::ptr_type(ResizeData)>
+typedef std::function<TransformCommand::ptr_type(ResizeData)>
 	ResizeCommandFactory;
-typedef boost::function<TransformCommand::ptr_type(RotationData)>
+typedef std::function<TransformCommand::ptr_type(RotationData)>
 	RotationCommandFactory;
 
 
diff --git a/src/insets/InsetExternal.cpp b/src/insets/InsetExternal.cpp
index 5a7a07a..b8f3f98 100644
--- a/src/insets/InsetExternal.cpp
+++ b/src/insets/InsetExternal.cpp
@@ -37,7 +37,6 @@
 
 #include "graphics/PreviewLoader.h"
 
-#include "support/bind.h"
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/ExceptionMessage.h"
@@ -630,7 +629,7 @@ void InsetExternal::setParams(InsetExternalParams const & p)
 	case PREVIEW_INSTANT: {
 		renderer_.reset(new RenderMonitoredPreview(this));
 		RenderMonitoredPreview * preview_ptr = renderer_->asMonitoredPreview();
-		preview_ptr->fileChanged(bind(&InsetExternal::fileChanged, this));
+		preview_ptr->fileChanged([this]() { fileChanged(); });
 		if (preview_ptr->monitoring())
 			preview_ptr->stopMonitoring();
 		add_preview_and_start_loading(*preview_ptr, *this, buffer());
diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp
index 1031f3a..3b9afaa 100644
--- a/src/insets/InsetInclude.cpp
+++ b/src/insets/InsetInclude.cpp
@@ -62,8 +62,6 @@
 #include "support/lyxalgo.h"
 #include "support/mutex.h"
 
-#include "support/bind.h"
-
 using namespace std;
 using namespace lyx::support;
 
@@ -171,7 +169,7 @@ InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p)
 	  preview_(new RenderMonitoredPreview(this)), failedtoload_(false),
 	  set_label_(false), label_(0), child_buffer_(0)
 {
-	preview_->fileChanged(bind(&InsetInclude::fileChanged, this));
+	preview_->fileChanged([this]() { fileChanged(); });
 
 	if (isListings(params())) {
 		InsetListingsParams listing_params(to_utf8(p["lstparams"]));
@@ -186,7 +184,7 @@ InsetInclude::InsetInclude(InsetInclude const & other)
 	  preview_(new RenderMonitoredPreview(this)), failedtoload_(false),
 	  set_label_(false), label_(0), child_buffer_(0)
 {
-	preview_->fileChanged(bind(&InsetInclude::fileChanged, this));
+	preview_->fileChanged([this]() { fileChanged(); });
 
 	if (other.label_)
 		label_ = new InsetLabel(*other.label_);
diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp
index 5311527..2ca0341 100644
--- a/src/insets/InsetText.cpp
+++ b/src/insets/InsetText.cpp
@@ -61,7 +61,6 @@
 #include "support/gettext.h"
 #include "support/lstrings.h"
 
-#include "support/bind.h"
 #include "support/lassert.h"
 
 #include <algorithm>
@@ -423,8 +422,8 @@ void InsetText::rejectChanges()
 void InsetText::validate(LaTeXFeatures & features) const
 {
 	features.useInsetLayout(getLayout());
-	for_each(paragraphs().begin(), paragraphs().end(),
-		 bind(&Paragraph::validate, _1, ref(features)));
+	for (Paragraph const & p : paragraphs())
+		p.validate(features);
 }
 
 
@@ -698,8 +697,7 @@ void InsetText::appendParagraphs(ParagraphList & plist)
 	mergeParagraph(buffer().params(), pl,
 		       distance(pl.begin(), ins) - 1);
 
-	for_each(pit, plist.end(),
-		 bind(&ParagraphList::push_back, ref(pl), _1));
+	for_each(pit, plist.end(), [&](Paragraph const & p) { pl.push_back(p); });
 }
 
 
diff --git a/src/insets/RenderGraphic.cpp b/src/insets/RenderGraphic.cpp
index 48b41e2..dc91d86 100644
--- a/src/insets/RenderGraphic.cpp
+++ b/src/insets/RenderGraphic.cpp
@@ -27,8 +27,6 @@
 #include "support/filetools.h"
 #include "support/gettext.h"
 
-#include "support/bind.h"
-
 using namespace std;
 
 namespace lyx {
@@ -36,14 +34,14 @@ namespace lyx {
 
 RenderGraphic::RenderGraphic(Inset const * inset)
 {
-	loader_.connect(bind(&Inset::updateFrontend, inset));
+	loader_.connect([=]() { inset->updateFrontend(); });
 }
 
 
 RenderGraphic::RenderGraphic(RenderGraphic const & other, Inset const * inset)
 	: RenderBase(other), loader_(other.loader_), params_(other.params_)
 {
-	loader_.connect(bind(&Inset::updateFrontend, inset));
+	loader_.connect([=]() { inset->updateFrontend(); });
 }
 
 
diff --git a/src/insets/RenderPreview.cpp b/src/insets/RenderPreview.cpp
index 59a870c..0ad4712 100644
--- a/src/insets/RenderPreview.cpp
+++ b/src/insets/RenderPreview.cpp
@@ -31,8 +31,6 @@
 #include "support/lassert.h"
 #include "support/lstrings.h"
 
-#include "support/bind.h"
-
 using namespace std;
 using namespace lyx::support;
 
@@ -240,8 +238,10 @@ void RenderPreview::addPreview(docstring const & latex_snippet,
 	// PreviewLoader signal that'll inform us when the preview image
 	// is ready for loading.
 	if (!ploader_connection_.connected()) {
-		ploader_connection_ = ploader.connect(
-			bind(&RenderPreview::imageReady, this, _1));
+		ploader_connection_ =
+			ploader.connect([this](graphics::PreviewImage const & pimage) {
+				imageReady(pimage);
+			});
 	}
 
 	ploader.add(snippet_);
diff --git a/src/support/FileMonitor.cpp b/src/support/FileMonitor.cpp
index bdd6444..0930be3 100644
--- a/src/support/FileMonitor.cpp
+++ b/src/support/FileMonitor.cpp
@@ -15,7 +15,6 @@
 #include "support/FileName.h"
 #include "support/Timeout.h"
 
-#include "support/bind.h"
 #include <boost/signals2/trackable.hpp>
 
 using namespace std;
@@ -145,7 +144,7 @@ FileMonitor::Impl::Impl(FileName const & file_with_path, int interval)
 	  timestamp_(0),
 	  checksum_(0)
 {
-	timer_.timeout.connect(bind(&Impl::monitorFile, this));
+	timer_.timeout.connect([this]() { monitorFile(); });
 }
 
 
diff --git a/src/support/ForkedCalls.cpp b/src/support/ForkedCalls.cpp
index fc2b599..a26c19d 100644
--- a/src/support/ForkedCalls.cpp
+++ b/src/support/ForkedCalls.cpp
@@ -21,9 +21,8 @@
 #include "support/os.h"
 #include "support/Timeout.h"
 
-#include "support/bind.h"
-
 #include <cerrno>
+#include <functional>
 #include <queue>
 #include <sstream>
 #include <utility>
@@ -47,7 +46,6 @@
 using namespace std;
 
 
-
 namespace lyx {
 namespace support {
 
@@ -84,7 +82,7 @@ private:
 	Murder(int secs, pid_t pid)
 		: timeout_(1000*secs, Timeout::ONETIME), pid_(pid)
 	{
-		timeout_.timeout.connect(lyx::bind(&Murder::kill, this));
+		timeout_.timeout.connect([this]() { kill(); });
 		timeout_.start();
 	}
 
@@ -475,7 +473,7 @@ void callNext()
 	Process pro = callQueue_.front();
 	callQueue_.pop();
 	// Bind our chain caller
-	pro.second->connect(lyx::bind(&ForkedCallQueue::callback, _1, _2));
+	pro.second->connect(callback);
 	ForkedCall call;
 	//If we fail to fork the process, then emit the signal
 	//to tell the outside world that it failed.
@@ -553,7 +551,7 @@ string const getChildErrorMessage()
 
 namespace ForkedCallsController {
 
-typedef std::shared_ptr<ForkedProcess> ForkedProcessPtr;
+typedef shared_ptr<ForkedProcess> ForkedProcessPtr;
 typedef list<ForkedProcessPtr> ListType;
 typedef ListType::iterator iterator;
 
@@ -561,12 +559,10 @@ typedef ListType::iterator iterator;
 /// The child processes
 static ListType forkedCalls;
 
-iterator find_pid(pid_t pid)
+iterator find_pid(pid_t p)
 {
 	return find_if(forkedCalls.begin(), forkedCalls.end(),
-			    lyx::bind(equal_to<pid_t>(),
-			    lyx::bind(&ForkedCall::pid, _1),
-			    pid));
+	               [=](ForkedProcessPtr proc) { return proc->pid() == p; });
 }
 
 
diff --git a/src/support/Makefile.am b/src/support/Makefile.am
index 08f1c71..de67803 100644
--- a/src/support/Makefile.am
+++ b/src/support/Makefile.am
@@ -34,7 +34,6 @@ liblyxsupport_a_SOURCES = \
 	FileMonitor.h \
 	FileMonitor.cpp \
 	RandomAccessList.h \
-	bind.h \
 	Changer.h \
 	ConsoleApplication.cpp \
 	ConsoleApplication.h \
@@ -59,7 +58,6 @@ liblyxsupport_a_SOURCES = \
 	filetools.h \
 	ForkedCalls.cpp \
 	ForkedCalls.h \
-	functional.h \
 	gettext.cpp \
 	gettext.h \
 	gzstream.cpp \
diff --git a/src/support/bind.h b/src/support/bind.h
deleted file mode 100644
index 5a734ff..0000000
--- a/src/support/bind.h
+++ /dev/null
@@ -1,26 +0,0 @@
-// -*- C++ -*-
-/**
- * \file bind.h
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Peter Kümmel
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#ifndef LYX_BIND_H
-#define LYX_BIND_H
-
-#include "support/functional.h"
-
-namespace lyx
-{
-	using std::placeholders::_1;
-	using std::placeholders::_2;
-	using std::bind;
-	using std::ref;
-}
-
-
-#endif
diff --git a/src/support/functional.h b/src/support/functional.h
deleted file mode 100644
index 6673ded..0000000
--- a/src/support/functional.h
+++ /dev/null
@@ -1,23 +0,0 @@
-// -*- C++ -*-
-/**
- * \file functional.h
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Peter Kümmel
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#ifndef LYX_FUNCTIONAL_H
-#define LYX_FUNCTIONAL_H
-
-#include <functional>
-
-namespace lyx
-{
-	using std::function;
-}
-
-
-#endif
-- 
2.7.4

Reply via email to