changeset 893:76f37e47ccfa

IN 28: Verify that all Qt types are properly parented.
author John Schneiderman <JohnMS@member.fsf.org>
date Tue, 27 Sep 2022 19:01:42 +0200
parents 8efa31a8a6d1
children be027d2555ba
files ChangeLog src/desktop-ui/internal/BanksWidget.cpp src/desktop-ui/internal/LedgersWidget.cpp src/desktop-ui/internal/QtMemory.hpp src/foundation/external/foundation/QtMemory.hpp
diffstat 5 files changed, 70 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Sep 27 17:53:59 2022 +0200
+++ b/ChangeLog	Tue Sep 27 19:01:42 2022 +0200
@@ -29,6 +29,7 @@
 	* Bug Fix #2: Added OpenSSL to the list of 3rd-party libraries to
 			install on Windows.
 	* Bug Fix #3: Fixed compilation failing on macOS.
+	* Development #28: All Qt memory is validated at runtime in the debug build.
 2021-12-27 John Schneiderman <JohnMS_AT_member_DOT_fsf_DOT_org> 0.4.0
 	* Feature: Any budget item that produces an error when saving will have the error message
 				displayed below that budget item.
--- a/src/desktop-ui/internal/BanksWidget.cpp	Tue Sep 27 17:53:59 2022 +0200
+++ b/src/desktop-ui/internal/BanksWidget.cpp	Tue Sep 27 19:01:42 2022 +0200
@@ -81,6 +81,9 @@
 #include <foundation/QtMemory.hpp>
 using drn::foundation::makeQtPtr;
 
+#include "QtMemory.hpp"
+using drn::desktop_ui::makeQtPtr;
+
 
 namespace
 {
--- a/src/desktop-ui/internal/LedgersWidget.cpp	Tue Sep 27 17:53:59 2022 +0200
+++ b/src/desktop-ui/internal/LedgersWidget.cpp	Tue Sep 27 19:01:42 2022 +0200
@@ -63,6 +63,9 @@
 #include "TransactionWidget.h"
 using drn::desktop_ui::internal::TransactionWidget;
 
+#include "QtMemory.hpp"
+using drn::desktop_ui::makeQtPtr;
+
 
 namespace
 {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/desktop-ui/internal/QtMemory.hpp	Tue Sep 27 19:01:42 2022 +0200
@@ -0,0 +1,54 @@
+/*******************************************************************************
+***  This file is part of Dux Rei Nummariae.                                 ***
+***                                                                          ***
+***  Copyright (C) 2017, 2018, 2019, 2020, 2021                              ***
+***  John Schneiderman <Licensing _AT_ Schneiderman _DOT_ me>                ***
+***                                                                          ***
+***  This program is free software: you can redistribute it and/or modify it ***
+***  under the terms of the GNU Affero General Public License as published   ***
+***  by the Free Software Foundation, either version 3 of the License, or    ***
+***  (at your option) any later version.                                     ***
+***                                                                          ***
+***  This program is distributed in the hope that it will be useful, but     ***
+***  WITHOUT ANY WARRANTY; without even the implied warranty of              ***
+***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    ***
+***  See the GNU Affero General Public License for more details.             ***
+***                                                                          ***
+***  You should have received a copy of the GNU Affero General Public License***
+***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
+*******************************************************************************/
+#ifndef DRN_DESKTOP_UI_INTERNAL_QTMEMORY_HPP_
+#define DRN_DESKTOP_UI_INTERNAL_QTMEMORY_HPP_
+
+#include <type_traits>
+
+#include <QTreeWidgetItem>
+
+#include <foundation/ObserverPtr.hpp>
+
+
+namespace drn
+{
+namespace desktop_ui
+{
+
+template<typename ObjectType, typename ... ObjectArgTypes>
+typename std::enable_if<
+	std::is_same<::QTreeWidgetItem, ObjectType>::value,
+	foundation::ObserverPtr<::QTreeWidgetItem>
+>::type makeQtPtr(ObjectArgTypes&& ... objectArgs);
+
+}}
+
+template<typename ObjectType, typename ... ObjectArgTypes>
+typename std::enable_if<
+	std::is_same<::QTreeWidgetItem, ObjectType>::value,
+	drn::foundation::ObserverPtr<::QTreeWidgetItem>
+>::type drn::desktop_ui::makeQtPtr(ObjectArgTypes&& ... objectArgs)
+{
+	auto* obj{new QTreeWidgetItem{std::forward<ObjectArgTypes>(objectArgs)...}};
+	assert(obj->treeWidget() != nullptr && "All Qt tree widget items must have a parent.");
+	return obj;
+}
+
+#endif
--- a/src/foundation/external/foundation/QtMemory.hpp	Tue Sep 27 17:53:59 2022 +0200
+++ b/src/foundation/external/foundation/QtMemory.hpp	Tue Sep 27 19:01:42 2022 +0200
@@ -29,8 +29,6 @@
 #include <foundation/ObserverPtr.hpp>
 
 
-class QTreeWidgetItem;
-
 namespace drn
 {
 namespace foundation
@@ -59,7 +57,10 @@
  * @return The Qt object that is created on the heap.
  */
 template<typename ObjectType, typename ... ObjectArgTypes>
-ObserverPtr<ObjectType> makeQtPtr(ObjectArgTypes&& ... objectArgs);
+typename std::enable_if<
+	std::is_base_of<::QObject, ObjectType>::value,
+	foundation::ObserverPtr<ObjectType>
+>::type makeQtPtr(ObjectArgTypes&& ... objectArgs);
 
 }}
 
@@ -86,15 +87,13 @@
 }
 
 template<typename ObjectType, typename ... ObjectArgTypes>
-drn::foundation::ObserverPtr<ObjectType> drn::foundation::makeQtPtr(ObjectArgTypes&& ... objectArgs)
+typename std::enable_if<
+	std::is_base_of<::QObject, ObjectType>::value,
+	drn::foundation::ObserverPtr<ObjectType>
+>::type drn::foundation::makeQtPtr(ObjectArgTypes&& ... objectArgs)
 {
-	static_assert(
-		std::is_base_of<::QObject, ObjectType>::value
-			// Additional Qt types that do not have QObject as a base.
-			|| std::is_same<::QTreeWidgetItem, ObjectType>::value,
-		"Only Qt types can be created via this function."
-	);
 	auto* obj{new ObjectType{std::forward<ObjectArgTypes>(objectArgs)...}};
+	assert(obj->parent() != nullptr && "All Qt objects must have a parent.");
 	return obj;
 }