(svn r8947) [cpp_gui] -Codechange: proprietary smart pointer adapter replaced by generic one cpp_gui
authorKUDr
Wed, 28 Feb 2007 22:18:17 +0000
branchcpp_gui
changeset 6269 3b3bd4fe0736
parent 6268 4b5241e5dd10
child 6270 5b2d0642fb81
(svn r8947) [cpp_gui] -Codechange: proprietary smart pointer adapter replaced by generic one
src/misc/countedptr.hpp
src/widget/widget.h
src/widget/widget_base.cpp
--- a/src/misc/countedptr.hpp	Wed Feb 28 00:33:40 2007 +0000
+++ b/src/misc/countedptr.hpp	Wed Feb 28 22:18:17 2007 +0000
@@ -96,6 +96,40 @@
 	}
 }
 
+/**
+ * Adapter wrapper for CCountedPtr like classes that can't be used directly by stl
+ * collections as item type. For example CCountedPtr has overloaded operator & which
+ * prevents using CCountedPtr in stl collections (i.e. std::list<CCountedPtr<MyType> >)
+ */
+template <class T> struct AdaptT {
+	T m_t;
+
+	/** construct by wrapping the given object */
+	AdaptT(const T &t)
+		: m_t(t)
+	{}
+
+	/** assignment operator */
+	T& operator = (const T &t)
+	{
+		m_t = t;
+		return t;
+	}
+
+	/** type-cast operator (used when AdaptT is used instead of T) */
+	operator T& ()
+	{
+		return m_t;
+	}
+
+	/** const type-cast operator (used when AdaptT is used instead of const T) */
+	operator const T& () const
+	{
+		return m_t;
+	}
+};
+
+
 /** Simple counted object. Use it as base of your struct/class if you want to use
  *  basic reference counting. Your struct/class will destroy and free itself when
  *  last reference to it is released (using Relese() method). The initial reference
--- a/src/widget/widget.h	Wed Feb 28 00:33:40 2007 +0000
+++ b/src/widget/widget.h	Wed Feb 28 22:18:17 2007 +0000
@@ -38,13 +38,7 @@
 typedef int32 WidgetId;
 
 struct Widget : public SimpleCountedObject {
-	struct Handler {
-		EventHandlerDelegatePtr m_delegate;
-
-		Handler(EventHandlerDelegate *d)
-			: m_delegate(d)
-		{}
-	};
+	typedef AdaptT<EventHandlerDelegatePtr> Handler;
 	typedef std::list<Handler> Handlers;
 
 	CompositeWidget *m_container;       ///< widget container (can be any panel or window)
--- a/src/widget/widget_base.cpp	Wed Feb 28 00:33:40 2007 +0000
+++ b/src/widget/widget_base.cpp	Wed Feb 28 22:18:17 2007 +0000
@@ -140,7 +140,7 @@
 	BaseWindow *w = GetWindow();
 	if (w == NULL) return;
 	for (Handlers::iterator it = m_handlers.begin(); it != m_handlers.end(); ++it) {
-		EventHandlerDelegate *d = (*it).m_delegate;
+		EventHandlerDelegatePtr d = (*it);
 		if (d->m_code != e.GetCode()) continue;
 		d->HandleEvent(w, e);
 	}