← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2836: auto-manage some menu memory

 

------------------------------------------------------------
revno: 2836
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Mon 2012-01-23 22:02:12 +0100
message:
  auto-manage some menu memory
modified:
  dwt/include/dwt/widgets/Menu.h
  dwt/src/widgets/Menu.cpp


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dwt/include/dwt/widgets/Menu.h'
--- dwt/include/dwt/widgets/Menu.h	2012-01-22 20:27:14 +0000
+++ dwt/include/dwt/widgets/Menu.h	2012-01-23 21:02:12 +0000
@@ -304,9 +304,9 @@
 	bool popup;
 
 	// its sub menus
-	std::vector<Menu*> itsChildren;
+	std::vector<std::unique_ptr<Menu>> itsChildren;
 	// its item data
-	std::vector<ItemDataWrapper*> itsItemData;
+	std::vector<std::unique_ptr<ItemDataWrapper>> itsItemData;
 
 	static const unsigned id_offset = 100;
 	typedef std::unique_ptr<std::vector<Dispatcher::F> > commands_type;

=== modified file 'dwt/src/widgets/Menu.cpp'
--- dwt/src/widgets/Menu.cpp	2012-01-22 20:27:14 +0000
+++ dwt/src/widgets/Menu.cpp	2012-01-23 21:02:12 +0000
@@ -202,32 +202,28 @@
 		// create item data
 		auto wrapper = new ItemDataWrapper(this, position, false, icon);
 		info.dwItemData = reinterpret_cast<ULONG_PTR>(wrapper);
-		itsItemData.push_back(wrapper);
+		itsItemData.emplace_back(wrapper);
 	}
 
 	// append to this menu at the end
 	if(!::InsertMenuItem(itsHandle, position, TRUE, &info)) {
 		throw Win32Exception("Could not add a sub-menu");
 	}
-	itsChildren.push_back(sub);
+	itsChildren.emplace_back(sub);
 	return sub;
 }
 
 Menu::~Menu() {
 	// destroy this menu.
 	::DestroyMenu(handle());
-
-	// delete data associated to owner-drawn menu items.
-	std::for_each(itsItemData.begin(), itsItemData.end(), [](ItemDataWrapper* wrapper) { delete wrapper; });
-
-	// destroy sub-menus.
-	std::for_each(itsChildren.begin(), itsChildren.end(), [](Menu* sub) { delete sub; });
 }
 
 void Menu::setFont(FontPtr font) {
 	this->font = font ? font : new Font(Font::DefaultGui);
 	titleFont = boldFont = this->font->makeBold();
-	std::for_each(itsChildren.begin(), itsChildren.end(), [this](Menu* sub) { sub->setFont(this->font); });
+	for(auto i = itsChildren.begin(), iend = itsChildren.end(); i != iend; ++i) {
+		(*i)->setFont(this->font);
+	}
 }
 
 void Menu::setTitleFont(FontPtr font) {
@@ -327,7 +323,7 @@
 				++itsItemData[i]->index;
 
 		// push back title
-		itsItemData.push_back(wrapper);
+		itsItemData.emplace_back(wrapper);
 
 		if(!(!hasTitle ? ::InsertMenuItem(itsHandle, 0, TRUE, &info) : ::SetMenuItemInfo(itsHandle, 0, TRUE, &info))) {
 			throw Win32Exception("Could not add a menu title");
@@ -731,7 +727,7 @@
 		// create item data wrapper
 		auto wrapper = new ItemDataWrapper(this, position);
 		itemInfo.dwItemData = reinterpret_cast<ULONG_PTR>(wrapper);
-		itsItemData.push_back(wrapper);
+		itsItemData.emplace_back(wrapper);
 	}
 
 	if(!::InsertMenuItem(itsHandle, position, TRUE, &itemInfo)) {
@@ -747,17 +743,15 @@
 	if ( ::RemoveMenu( itsHandle, index, MF_BYPOSITION ) )
 	{
 		if(ownerDrawn) {
-			ItemDataWrapper * wrapper = 0;
 			int itemRemoved = -1;
 
 			for(size_t i = 0; i < itsItemData.size(); ++i) {
 				// get current data wrapper
-				wrapper = itsItemData[i];
+				auto& wrapper = itsItemData[i];
 
 				if ( wrapper->index == index ) // if found
 				{
 					itemRemoved = int(i);
-					delete wrapper;
 					itsItemData[i] = 0;
 				}
 				else if ( wrapper->index > index )
@@ -771,7 +765,7 @@
 		// remove sub menus if any
 		if(popup) {
 			itsChildren.erase(std::remove_if(itsChildren.begin(), itsChildren.end(),
-				[popup](Menu* sub) { return sub->handle() == popup; }), itsChildren.end());
+				[popup](std::unique_ptr<Menu>& sub) { return sub->handle() == popup; }), itsChildren.end());
 		}
 	} else {
 		dwtWin32DebugFail("Couldn't remove item in removeItem()");
@@ -835,7 +829,7 @@
 		if(defaultItem)
 			wrapper->isDefault = true;
 		info.dwItemData = reinterpret_cast<ULONG_PTR>(wrapper);
-		itsItemData.push_back(wrapper);
+		itsItemData.emplace_back(wrapper);
 	}
 
 	if(!::InsertMenuItem(itsHandle, index, TRUE, &info)) {
@@ -867,7 +861,7 @@
 Menu* Menu::getChild(unsigned position) {
 	HMENU h = ::GetSubMenu(handle(), position);
 	for(size_t i = 0, n = itsChildren.size(); i < n; ++i) {
-		auto menu = itsChildren[i];
+		auto menu = itsChildren[i].get();
 		if(menu->handle() == h) {
 			return menu;
 		}