Boost in LyX

Yuriy Skalko yuriy.skalko at gmail.com
Sat Nov 21 14:37:07 UTC 2020


> If Boost is only used for temporary implementation of really useful features from newer standards (and only for older compilers) it should be Ok. And this dependency will drop itself over time.

Some steps in this direction are in attached patches.

Yuriy
-------------- next part --------------
From 32ac6a047bda71c47c17a4ff1417bd273acea1bc Mon Sep 17 00:00:00 2001
From: Yuriy Skalko <yuriy.skalko at gmail.com>
Date: Sat, 21 Nov 2020 14:14:38 +0200
Subject: [PATCH 1/3] Remove unused headers

---
 src/frontends/qt/GuiApplication.cpp | 1 -
 src/frontends/qt/GuiViewSource.cpp  | 4 ----
 2 files changed, 5 deletions(-)

diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp
index 9fa8d941d3..b3cfd5e5bf 100644
--- a/src/frontends/qt/GuiApplication.cpp
+++ b/src/frontends/qt/GuiApplication.cpp
@@ -87,7 +87,6 @@
 #include <QByteArray>
 #include <QDateTime>
 #include <QDesktopWidget>
-#include <QDir>
 #include <QEvent>
 #include <QFileOpenEvent>
 #include <QFileInfo>
diff --git a/src/frontends/qt/GuiViewSource.cpp b/src/frontends/qt/GuiViewSource.cpp
index 40752cde14..1a7a36cdd1 100644
--- a/src/frontends/qt/GuiViewSource.cpp
+++ b/src/frontends/qt/GuiViewSource.cpp
@@ -25,7 +25,6 @@
 #include "GuiView.h"
 #include "FuncRequest.h"
 #include "LyX.h"
-#include "Paragraph.h"
 #include "TexRow.h"
 
 #include "support/debug.h"
@@ -34,10 +33,7 @@
 #include "support/docstring_list.h"
 #include "support/gettext.h"
 
-#include <boost/crc.hpp>
-
 #include <QBoxLayout>
-#include <QComboBox>
 #include <QScrollBar>
 #include <QSettings>
 #include <QTextCursor>
-- 
2.28.0.windows.1

-------------- next part --------------
From f73926678388060dbdd67e9ed80dcd612f2bf344 Mon Sep 17 00:00:00 2001
From: Yuriy Skalko <yuriy.skalko at gmail.com>
Date: Sat, 21 Nov 2020 15:40:31 +0200
Subject: [PATCH 2/3] Refactor checksum calculation

---
 src/ConverterCache.cpp              | 13 ++------
 src/frontends/qt/GuiApplication.cpp | 19 +++++-------
 src/frontends/qt/GuiClipboard.cpp   | 15 ++++------
 src/support/FileName.cpp            | 22 ++++----------
 src/support/Makefile.am             |  2 ++
 src/support/checksum.cpp            | 46 +++++++++++++++++++++++++++++
 src/support/checksum.h              | 30 +++++++++++++++++++
 7 files changed, 99 insertions(+), 48 deletions(-)
 create mode 100644 src/support/checksum.cpp
 create mode 100644 src/support/checksum.h

diff --git a/src/ConverterCache.cpp b/src/ConverterCache.cpp
index bee0253652..0f4d03efb9 100644
--- a/src/ConverterCache.cpp
+++ b/src/ConverterCache.cpp
@@ -25,8 +25,8 @@
 #include "support/lyxtime.h"
 #include "support/Package.h"
 
+#include "support/checksum.h"
 #include "support/lassert.h"
-#include <boost/crc.hpp>
 
 #include <algorithm>
 #include <fstream>
@@ -41,14 +41,6 @@ namespace lyx {
 
 namespace {
 
-unsigned long do_crc(string const & s)
-{
-	boost::crc_32_type crc;
-	crc = for_each(s.begin(), s.end(), crc);
-	return crc.checksum();
-}
-
-
 // FIXME THREAD
 // This should be OK because it is only assigned during init()
 static FileName cache_dir;
@@ -62,7 +54,8 @@ public:
 		: timestamp(t), checksum(c)
 	{
 		ostringstream os;
-		os << setw(10) << setfill('0') << do_crc(orig_from.absFileName())
+		os << setw(10) << setfill('0')
+		   << support::checksum(orig_from.absFileName())
 		   << '-' << to_format;
 		cache_name = FileName(addName(cache_dir.absFileName(), os.str()));
 		LYXERR(Debug::FILES, "Add file cache item " << orig_from
diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp
index b3cfd5e5bf..ac7bea25fd 100644
--- a/src/frontends/qt/GuiApplication.cpp
+++ b/src/frontends/qt/GuiApplication.cpp
@@ -59,6 +59,7 @@
 
 #include "insets/InsetText.h"
 
+#include "support/checksum.h"
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/ExceptionMessage.h"
@@ -146,8 +147,6 @@
 #include <QMacPasteboardMime>
 #endif // Q_OS_MAC
 
-#include <boost/crc.hpp>
-
 #include <exception>
 #include <sstream>
 #include <vector>
@@ -1701,16 +1700,14 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 		// FIXME: create a new method shared with LFUN_HELP_OPEN.
 		string const fname = to_utf8(cmd.argument());
 		bool const is_open = FileName::isAbsolute(fname)
-			&& theBufferList().getBuffer(FileName(fname));
+				&& theBufferList().getBuffer(FileName(fname));
 		if (!current_view_
-		    || (!lyxrc.open_buffers_in_tabs
-			&& current_view_->documentBufferView() != 0
-			&& !is_open)) {
+			|| (!lyxrc.open_buffers_in_tabs
+				&& current_view_->documentBufferView() != 0
+				&& !is_open)) {
 			// We want the ui session to be saved per document and not per
 			// window number. The filename crc is a good enough identifier.
-			boost::crc_32_type crc;
-			crc = for_each(fname.begin(), fname.end(), crc);
-			createView(crc.checksum());
+			createView(support::checksum(fname));
 			current_view_->openDocument(fname);
 			if (!current_view_->documentBufferView())
 				current_view_->close();
@@ -2670,10 +2667,8 @@ void GuiApplication::restoreGuiSession()
 		FileName const & file_name = last.file_name;
 		if (!current_view_ || (!lyxrc.open_buffers_in_tabs
 			  && current_view_->documentBufferView() != 0)) {
-			boost::crc_32_type crc;
 			string const & fname = file_name.absFileName();
-			crc = for_each(fname.begin(), fname.end(), crc);
-			createView(crc.checksum());
+			createView(support::checksum(fname));
 		}
 		current_view_->loadDocument(file_name, false);
 
diff --git a/src/frontends/qt/GuiClipboard.cpp b/src/frontends/qt/GuiClipboard.cpp
index 59e59005b7..f78ab1cda4 100644
--- a/src/frontends/qt/GuiClipboard.cpp
+++ b/src/frontends/qt/GuiClipboard.cpp
@@ -35,6 +35,8 @@
 
 #include "frontends/alert.h"
 
+#include "support/checksum.h"
+
 #include <QApplication>
 #include <QBuffer>
 #include <QClipboard>
@@ -47,8 +49,6 @@
 #include <QTextDocument>
 #include <QTimer>
 
-#include <boost/crc.hpp>
-
 #include <memory>
 #include <map>
 #include <iostream>
@@ -431,11 +431,8 @@ void GuiClipboard::put(string const & lyx, docstring const & html, docstring con
 		data->setData(lyxMimeType(), qlyx);
 		// If the OS has not the concept of clipboard ownership,
 		// we recognize internal data through its checksum.
-		if (!hasInternal()) {
-			boost::crc_32_type crc32;
-			crc32.process_bytes(lyx.c_str(), lyx.size());
-			checksum = crc32.checksum();
-		}
+		if (!hasInternal())
+			checksum = support::checksum(lyx);
 	}
 	// Don't test for text.empty() since we want to be able to clear the
 	// clipboard.
@@ -528,9 +525,7 @@ bool GuiClipboard::isInternal() const
 	// ourself by comparing its checksum with the stored one.
 	QByteArray const ar = cache_.data(lyxMimeType());
 	string const data(ar.data(), ar.count());
-	boost::crc_32_type crc32;
-	crc32.process_bytes(data.c_str(), data.size());
-	return checksum == crc32.checksum();
+	return checksum == static_cast<std::uint32_t>(support::checksum(data));
 }
 
 
diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp
index 4e075f2ed8..3d31c7d37c 100644
--- a/src/support/FileName.cpp
+++ b/src/support/FileName.cpp
@@ -35,7 +35,7 @@
 #include <QThread>
 #endif
 
-#include <boost/crc.hpp>
+#include "support/checksum.h"
 
 #include <algorithm>
 #include <iterator>
@@ -537,20 +537,14 @@ bool FileName::link(FileName const & name) const
 
 unsigned long checksum_ifstream_fallback(char const * file)
 {
-	unsigned long result = 0;
 	//LYXERR(Debug::FILES, "lyx::sum() using istreambuf_iterator (fast)");
 	ifstream ifs(file, ios_base::in | ios_base::binary);
 	if (!ifs)
-		return result;
-
-	istreambuf_iterator<char> beg(ifs);
-	istreambuf_iterator<char> end;
-	boost::crc_32_type crc;
-	crc = for_each(beg, end, crc);
-	result = crc.checksum();
-	return result;
+		return 0;
+	return support::checksum(ifs);
 }
 
+
 unsigned long FileName::checksum() const
 {
 	if (!exists()) {
@@ -583,11 +577,9 @@ unsigned long FileName::checksum() const
 	qint64 size = fi.size();
 	uchar * ubeg = qf.map(0, size);
 	uchar * uend = ubeg + size;
-	boost::crc_32_type ucrc;
-	ucrc.process_block(ubeg, uend);
+	result = support::checksum(ubeg, uend);
 	qf.unmap(ubeg);
 	qf.close();
-	result = ucrc.checksum();
 
 #else // QT_VERSION
 
@@ -619,9 +611,7 @@ unsigned long FileName::checksum() const
 	char * beg = static_cast<char*>(mm);
 	char * end = beg + info.st_size;
 
-	boost::crc_32_type crc;
-	crc.process_block(beg, end);
-	result = crc.checksum();
+	result = support::checksum(beg, end);
 
 	munmap(mm, info.st_size);
 	close(fd);
diff --git a/src/support/Makefile.am b/src/support/Makefile.am
index addeab86b8..45d30b823f 100644
--- a/src/support/Makefile.am
+++ b/src/support/Makefile.am
@@ -38,6 +38,8 @@ liblyxsupport_a_SOURCES = \
 	bind.h \
 	Cache.h \
 	Changer.h \
+	checksum.cpp \
+	checksum.h \
 	ConsoleApplication.cpp \
 	ConsoleApplication.h \
 	ConsoleApplicationPrivate.h \
diff --git a/src/support/checksum.cpp b/src/support/checksum.cpp
new file mode 100644
index 0000000000..151b5330d6
--- /dev/null
+++ b/src/support/checksum.cpp
@@ -0,0 +1,46 @@
+// -*- C++ -*-
+/**
+ * \file checksum.cpp
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Yuriy Skalko
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include "support/checksum.h"
+#include "boost/crc.hpp"
+#include <iterator>
+
+namespace lyx {
+
+namespace support {
+
+unsigned long checksum(std::string const & s)
+{
+	boost::crc_32_type crc;
+	crc.process_bytes(s.c_str(), s.size());
+	return crc.checksum();
+}
+
+unsigned long checksum(std::ifstream & ifs)
+{
+	std::istreambuf_iterator<char> beg(ifs);
+	std::istreambuf_iterator<char> end;
+
+	boost::crc_32_type crc;
+	crc = for_each(beg, end, crc);
+	return crc.checksum();
+}
+
+unsigned long checksum(char const * beg, char const * end)
+{
+	boost::crc_32_type crc;
+	crc.process_block(beg, end);
+	return crc.checksum();
+}
+
+} // namespace support
+
+} // namespace lyx
diff --git a/src/support/checksum.h b/src/support/checksum.h
new file mode 100644
index 0000000000..ab14339765
--- /dev/null
+++ b/src/support/checksum.h
@@ -0,0 +1,30 @@
+// -*- C++ -*-
+/**
+ * \file checksum.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Yuriy Skalko
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef LYX_CHECKSUM_H
+#define LYX_CHECKSUM_H
+
+#include <fstream>
+#include <string>
+
+namespace lyx {
+
+namespace support {
+
+unsigned long checksum(std::string const & s);
+unsigned long checksum(std::ifstream & ifs);
+unsigned long checksum(char const * beg, char const * end);
+
+} // namespace support
+
+} // namespace lyx
+
+#endif // LYX_CHECKSUM_H
-- 
2.28.0.windows.1

-------------- next part --------------
From 909c368d38351c59905fb558a7402909f847163f Mon Sep 17 00:00:00 2001
From: Yuriy Skalko <yuriy.skalko at gmail.com>
Date: Sat, 21 Nov 2020 16:27:13 +0200
Subject: [PATCH 3/3] Use `std::any` when compiler supports C++17 or later

---
 src/insets/ExternalTransforms.cpp |  4 ++--
 src/insets/ExternalTransforms.h   |  7 +++----
 src/support/Makefile.am           |  1 +
 src/support/any.h                 | 23 +++++++++++++++++++++++
 4 files changed, 29 insertions(+), 6 deletions(-)
 create mode 100644 src/support/any.h

diff --git a/src/insets/ExternalTransforms.cpp b/src/insets/ExternalTransforms.cpp
index 4f85f9d479..9ff5772cd9 100644
--- a/src/insets/ExternalTransforms.cpp
+++ b/src/insets/ExternalTransforms.cpp
@@ -326,13 +326,13 @@ string const sanitizeLatexOption(string const & input)
 namespace {
 
 template <typename Factory, typename Data, typename Transformer>
-void extractIt(boost::any const & any_factory,
+void extractIt(any const & any_factory,
 	       Data const & data, Transformer & transformer)
 {
 	if (any_factory.type() != typeid(Factory))
 		return;
 
-	Factory factory = boost::any_cast<Factory>(any_factory);
+	Factory factory = any_cast<Factory>(any_factory);
 	if (factory)
 		transformer = factory(data);
 }
diff --git a/src/insets/ExternalTransforms.h b/src/insets/ExternalTransforms.h
index 8b22f622c8..62e3342352 100644
--- a/src/insets/ExternalTransforms.h
+++ b/src/insets/ExternalTransforms.h
@@ -14,11 +14,10 @@
 
 #include "graphics/GraphicsParams.h"
 
+#include "support/any.h"
 #include "support/Length.h"
 #include "support/unique_ptr.h"
 
-#include <boost/any.hpp>
-
 #include <functional>
 #include <map>
 #include <memory>
@@ -339,7 +338,7 @@ public:
 	 */
 	template <typename Factory>
 	TransformStore(TransformID id_, Factory const & factory)
-		: id(id_), any_factory(boost::any(factory)) {}
+		: id(id_), any_factory(any(factory)) {}
 
 	typedef TransformCommand::ptr_type ComPtr;
 	typedef TransformOption::ptr_type  OptPtr;
@@ -353,7 +352,7 @@ public:
 
 private:
 	TransformID id;
-	boost::any any_factory;
+	any any_factory;
 };
 
 } // namespace external
diff --git a/src/support/Makefile.am b/src/support/Makefile.am
index 45d30b823f..4ff8618129 100644
--- a/src/support/Makefile.am
+++ b/src/support/Makefile.am
@@ -35,6 +35,7 @@ liblyxsupport_a_SOURCES = \
 	FileMonitor.h \
 	FileMonitor.cpp \
 	RandomAccessList.h \
+	any.h \
 	bind.h \
 	Cache.h \
 	Changer.h \
diff --git a/src/support/any.h b/src/support/any.h
new file mode 100644
index 0000000000..e8f6a111fb
--- /dev/null
+++ b/src/support/any.h
@@ -0,0 +1,23 @@
+// -*- C++ -*-
+/**
+ * \file any.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Yuriy Skalko
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef LYX_ANY_H
+#define LYX_ANY_H
+
+#if __cplusplus >= 201703L
+	#include <any>
+	namespace lyx { using std::any; }
+#else
+	#include <boost/any.hpp>
+	namespace lyx { using boost::any; }
+#endif
+
+#endif // LYX_ANY_H
-- 
2.28.0.windows.1



More information about the lyx-devel mailing list