From 162e998c2f15f8773cbb63f0cceee8cdea4c421f Mon Sep 17 00:00:00 2001 From: Paul Cornett Date: Wed, 9 Jan 2008 04:08:33 +0000 Subject: [PATCH] check for self-assignment in operator= git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@51123 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/accel.h | 3 ++- include/wx/buffer.h | 18 ++++++++++++------ include/wx/cmndata.h | 25 ++++++++++++++----------- include/wx/dynarray.h | 5 ----- include/wx/event.h | 34 ++++++++++++++++++---------------- include/wx/gtk/dataform.h | 9 ++++++++- include/wx/hashmap.h | 22 +++++++++++++--------- include/wx/list.h | 19 +++++++++++++------ include/wx/string.h | 20 +++++++++++++++++--- include/wx/unichar.h | 4 ++-- include/wx/utils.h | 6 +++--- include/wx/windowid.h | 3 ++- 12 files changed, 104 insertions(+), 64 deletions(-) diff --git a/include/wx/accel.h b/include/wx/accel.h index 025b51b375..ee2e3a08ad 100644 --- a/include/wx/accel.h +++ b/include/wx/accel.h @@ -68,7 +68,8 @@ public: wxAcceleratorEntry& operator=(const wxAcceleratorEntry& entry) { - Set(entry.m_flags, entry.m_keyCode, entry.m_command, entry.m_item); + if (&entry != this) + Set(entry.m_flags, entry.m_keyCode, entry.m_command, entry.m_item); return *this; } diff --git a/include/wx/buffer.h b/include/wx/buffer.h index 81a445afe5..d06fbf6fc1 100644 --- a/include/wx/buffer.h +++ b/include/wx/buffer.h @@ -105,9 +105,12 @@ public: wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src) { - if ( m_owned ) - free(m_str); - CopyFrom(src); + if (&src != this) + { + if ( m_owned ) + free(m_str); + CopyFrom(src); + } return *this; } @@ -317,9 +320,12 @@ public: wxMemoryBuffer& operator=(const wxMemoryBuffer& src) { - m_bufdata->DecRef(); - m_bufdata = src.m_bufdata; - m_bufdata->IncRef(); + if (&src != this) + { + m_bufdata->DecRef(); + m_bufdata = src.m_bufdata; + m_bufdata->IncRef(); + } return *this; } diff --git a/include/wx/cmndata.h b/include/wx/cmndata.h index 0bfced12f3..630415f4c3 100644 --- a/include/wx/cmndata.h +++ b/include/wx/cmndata.h @@ -87,17 +87,20 @@ public: wxFontData& operator=(const wxFontData& data) { - wxObject::operator=(data); - m_fontColour = data.m_fontColour; - m_showHelp = data.m_showHelp; - m_allowSymbols = data.m_allowSymbols; - m_enableEffects = data.m_enableEffects; - m_initialFont = data.m_initialFont; - m_chosenFont = data.m_chosenFont; - m_minSize = data.m_minSize; - m_maxSize = data.m_maxSize; - m_encoding = data.m_encoding; - m_encodingInfo = data.m_encodingInfo; + if (&data != this) + { + wxObject::operator=(data); + m_fontColour = data.m_fontColour; + m_showHelp = data.m_showHelp; + m_allowSymbols = data.m_allowSymbols; + m_enableEffects = data.m_enableEffects; + m_initialFont = data.m_initialFont; + m_chosenFont = data.m_chosenFont; + m_minSize = data.m_minSize; + m_maxSize = data.m_maxSize; + m_encoding = data.m_encoding; + m_encodingInfo = data.m_encodingInfo; + } return *this; } diff --git a/include/wx/dynarray.h b/include/wx/dynarray.h index 9bdd300ee7..6dabfa5b2b 100644 --- a/include/wx/dynarray.h +++ b/include/wx/dynarray.h @@ -334,11 +334,6 @@ public: \ name() { } \ ~name() { } \ \ - name& operator=(const name& src) \ - { base* temp = (base*) this; \ - (*temp) = ((const base&)src); \ - return *this; } \ - \ T& operator[](size_t uiIndex) const \ { return (T&)(base::operator[](uiIndex)); } \ T& Item(size_t uiIndex) const \ diff --git a/include/wx/event.h b/include/wx/event.h index 7b41f1ff38..3e5bc79873 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -778,7 +778,7 @@ public: virtual wxEvent *Clone() const { return new wxMouseEvent(*this); } - wxMouseEvent& operator=(const wxMouseEvent& event) { Assign(event); return *this; } + wxMouseEvent& operator=(const wxMouseEvent& event) { if (&event != this) Assign(event); return *this; } public: wxCoord m_x, m_y; @@ -940,22 +940,24 @@ public: // example) wxKeyEvent& operator=(const wxKeyEvent& evt) { - m_x = evt.m_x; - m_y = evt.m_y; - - m_keyCode = evt.m_keyCode; - - m_controlDown = evt.m_controlDown; - m_shiftDown = evt.m_shiftDown; - m_altDown = evt.m_altDown; - m_metaDown = evt.m_metaDown; - m_scanCode = evt.m_scanCode; - m_rawCode = evt.m_rawCode; - m_rawFlags = evt.m_rawFlags; + if (&evt != this) + { + m_x = evt.m_x; + m_y = evt.m_y; + + m_keyCode = evt.m_keyCode; + + m_controlDown = evt.m_controlDown; + m_shiftDown = evt.m_shiftDown; + m_altDown = evt.m_altDown; + m_metaDown = evt.m_metaDown; + m_scanCode = evt.m_scanCode; + m_rawCode = evt.m_rawCode; + m_rawFlags = evt.m_rawFlags; #if wxUSE_UNICODE - m_uniChar = evt.m_uniChar; + m_uniChar = evt.m_uniChar; #endif - + } return *this; } @@ -2284,7 +2286,7 @@ protected: private: // It makes no sense to copy objects of this class - wxEventConnectionRef& operator = (const wxEventConnectionRef& WXUNUSED(other)) { wxASSERT(0); return *this; } + wxEventConnectionRef& operator = (const wxEventConnectionRef& WXUNUSED(other)) { wxFAIL; return *this; } }; diff --git a/include/wx/gtk/dataform.h b/include/wx/gtk/dataform.h index 1fc8372227..86e0cd7838 100644 --- a/include/wx/gtk/dataform.h +++ b/include/wx/gtk/dataform.h @@ -30,7 +30,14 @@ public: wxDataFormat( const wxCStrData& id ) { InitFromString(id); } wxDataFormat& operator=(const wxDataFormat& format) - { m_type = format.m_type; m_format = format.m_format; return *this; } + { + if (&format != this) + { + m_type = format.m_type; + m_format = format.m_format; + } + return *this; + } wxDataFormat& operator=(NativeFormat format) { SetId(format); return *this; } diff --git a/include/wx/hashmap.h b/include/wx/hashmap.h index 47d583b840..836c2e3e54 100644 --- a/include/wx/hashmap.h +++ b/include/wx/hashmap.h @@ -161,7 +161,7 @@ public: \ \ Iterator() : m_node(0), m_ht(0) {} \ Iterator( Node* node, const Self* ht ) \ - : m_node(node), m_ht((Self*)ht) {} \ + : m_node(node), m_ht(wx_const_cast(Self*, ht)) {} \ bool operator ==( const Iterator& it ) const \ { return m_node == it.m_node; } \ bool operator !=( const Iterator& it ) const \ @@ -204,7 +204,7 @@ public: \ const_iterator() : Iterator() {} \ const_iterator(iterator i) : Iterator(i) {} \ const_iterator( Node* node, const Self* ht ) \ - : Iterator( node, (Self*)ht ) {} \ + : Iterator(node, wx_const_cast(Self*, ht)) {} \ const_iterator& operator++() { PlusPlus();return *this; } \ const_iterator operator++(int) { const_iterator it=*this;PlusPlus();return it; } \ const_reference operator *() const { return m_node->m_value; } \ @@ -236,12 +236,15 @@ public: \ \ const Self& operator=( const Self& ht ) \ { \ - clear(); \ - m_hasher = ht.m_hasher; \ - m_equals = ht.m_equals; \ - m_getKey = ht.m_getKey; \ - m_items = ht.m_items; \ - HashCopy( ht ); \ + if (&ht != this) \ + { \ + clear(); \ + m_hasher = ht.m_hasher; \ + m_equals = ht.m_equals; \ + m_getKey = ht.m_getKey; \ + m_items = ht.m_items; \ + HashCopy( ht ); \ + } \ return *this; \ } \ \ @@ -407,7 +410,8 @@ public: \ typedef const KEY_T const_t1; \ typedef const VALUE_T const_t2; \ \ - CLASSNAME( const const_t1& f, const const_t2& s ):first(t1(f)),second(t2(s)) {} \ + CLASSNAME(const const_t1& f, const const_t2& s) \ + : first(wx_const_cast(t1&, f)), second(wx_const_cast(t2&, s)) {} \ \ t1 first; \ t2 second; \ diff --git a/include/wx/list.h b/include/wx/list.h index c83ac08be3..3aace939b4 100644 --- a/include/wx/list.h +++ b/include/wx/list.h @@ -22,8 +22,8 @@ like the old class. */ -#ifndef _WX_LISTH__ -#define _WX_LISTH__ +#ifndef _WX_LIST_H_ +#define _WX_LIST_H_ // ----------------------------------------------------------------------------- // headers @@ -469,7 +469,7 @@ protected: virtual void DeleteData() { } public: // for wxList::iterator - void** GetDataPtr() const { return &(((wxNodeBase*)this)->m_data); } + void** GetDataPtr() const { return &(wx_const_cast(wxNodeBase*, this)->m_data); } private: // optional key stuff wxListKeyValue m_key; @@ -732,7 +732,7 @@ private: : wxListBase(count, (void **)elements) { } \ \ name& operator=(const name& list) \ - { Assign(list); return *this; } \ + { if (&list != this) Assign(list); return *this; } \ \ nodetype *GetFirst() const \ { return (nodetype *)wxListBase::GetFirst(); } \ @@ -1178,7 +1178,7 @@ public: #if !wxUSE_STL wxList& operator=(const wxList& list) - { Assign(list); return *this; } + { if (&list != this) Assign(list); return *this; } // compatibility methods void Sort(wxSortCompareFunction compfunc) { wxListBase::Sort(compfunc); } @@ -1214,7 +1214,14 @@ public: // inefficient!) wxStringList(const wxStringList& other) : wxStringListBase() { DeleteContents(true); DoCopy(other); } wxStringList& operator=(const wxStringList& other) - { Clear(); DoCopy(other); return *this; } + { + if (&other != this) + { + Clear(); + DoCopy(other); + } + return *this; + } // operations // makes a copy of the string diff --git a/include/wx/string.h b/include/wx/string.h index 2e7b6ca1b6..2d7d3e3a9c 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -657,7 +657,14 @@ public: iterator(const iterator& i) : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} iterator& operator=(const iterator& i) - { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } + { + if (&i != this) + { + m_cur = i.m_cur; + m_node.set(i.str(), &m_cur); + } + return *this; + } reference operator*() { return wxUniCharRef::CreateForString(m_node, m_cur); } @@ -692,7 +699,14 @@ public: : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} const_iterator& operator=(const const_iterator& i) - { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } + { + if (&i != this) + { + m_cur = i.m_cur; + m_node.set(i.str(), &m_cur); + } + return *this; + } const_iterator& operator=(const iterator& i) { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } @@ -1324,7 +1338,7 @@ public: // overloaded assignment // from another wxString wxString& operator=(const wxString& stringSrc) - { m_impl = stringSrc.m_impl; return *this; } + { if (&stringSrc != this) m_impl = stringSrc.m_impl; return *this; } wxString& operator=(const wxCStrData& cstr) { return *this = cstr.AsString(); } // from a character diff --git a/include/wx/unichar.h b/include/wx/unichar.h index e2232d2817..defc237114 100644 --- a/include/wx/unichar.h +++ b/include/wx/unichar.h @@ -96,7 +96,7 @@ public: bool operator&&(bool v) const { return (bool)*this && v; } // Assignment operators: - wxUniChar& operator=(const wxUniChar& c) { m_value = c.m_value; return *this; } + wxUniChar& operator=(const wxUniChar& c) { if (&c != this) m_value = c.m_value; return *this; } wxUniChar& operator=(const wxUniCharRef& c); wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; } wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; } @@ -218,7 +218,7 @@ public: #endif wxUniCharRef& operator=(const wxUniCharRef& c) - { return *this = c.UniChar(); } + { if (&c != this) *this = c.UniChar(); return *this; } wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); } wxUniCharRef& operator=(unsigned char c) { return *this = wxUniChar(c); } diff --git a/include/wx/utils.h b/include/wx/utils.h index 0baa6099d6..c7ea286b9d 100644 --- a/include/wx/utils.h +++ b/include/wx/utils.h @@ -9,8 +9,8 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifndef _WX_UTILSH__ -#define _WX_UTILSH__ +#ifndef _WX_UTILS_H_ +#define _WX_UTILS_H_ // ---------------------------------------------------------------------------- // headers @@ -136,7 +136,7 @@ class WXDLLIMPEXP_BASE wxPlatform public: wxPlatform() { Init(); } wxPlatform(const wxPlatform& platform) { Copy(platform); } - void operator = (const wxPlatform& platform) { Copy(platform); } + void operator = (const wxPlatform& platform) { if (&platform != this) Copy(platform); } void Copy(const wxPlatform& platform); // Specify an optional default value diff --git a/include/wx/windowid.h b/include/wx/windowid.h index 4b2e3e171c..e16b4534ab 100644 --- a/include/wx/windowid.h +++ b/include/wx/windowid.h @@ -72,7 +72,8 @@ public: wxWindowIDRef& operator=(const wxWindowIDRef& id) { - Assign(id.m_id); + if (&id != this) + Assign(id.m_id); return *this; } -- 2.45.2