Simplify wxPGPropArgCls

This commit is contained in:
Artur Wieczorek
2022-04-10 13:43:09 +02:00
parent 1a31eef189
commit 891e6a26a3
2 changed files with 33 additions and 52 deletions

View File

@@ -27,78 +27,65 @@ class WXDLLIMPEXP_PROPGRID wxPGPropArgCls
public: public:
wxPGPropArgCls( const wxPGProperty* property ) wxPGPropArgCls( const wxPGProperty* property )
{ {
m_ptr.property = const_cast<wxPGProperty*>(property); m_property = const_cast<wxPGProperty*>(property);
m_flags = IsProperty; m_isProperty = true;
} }
wxPGPropArgCls( const wxString& str ) wxPGPropArgCls( const wxString& str )
{ {
m_ptr.stringName = &str; m_name = str;
m_flags = IsWxString; m_property = NULL;
m_isProperty = false;
} }
wxPGPropArgCls( const wxPGPropArgCls& id ) wxPGPropArgCls( const wxPGPropArgCls& id )
{ {
m_ptr = id.m_ptr; m_isProperty = id.m_isProperty;
m_flags = id.m_flags; m_property = id.m_property;
m_name = id.m_name;
} }
// This is only needed for wxPython bindings. // This is only needed for wxPython bindings.
wxPGPropArgCls( wxString* str, bool WXUNUSED(deallocPtr) ) wxPGPropArgCls( wxString* str, bool WXUNUSED(deallocPtr) )
{ {
m_ptr.stringName = str; m_name = *str;
m_flags = IsWxString | OwnsWxString; delete str; // we own this string
} m_property = NULL;
~wxPGPropArgCls() m_isProperty = false;
{
if ( m_flags & OwnsWxString )
delete m_ptr.stringName;
} }
wxPGProperty* GetPtr() const wxPGProperty* GetPtr() const
{ {
wxCHECK( m_flags == IsProperty, NULL ); wxCHECK( m_isProperty, NULL );
return m_ptr.property; return m_property;
} }
wxPGPropArgCls( const char* str ) wxPGPropArgCls( const char* str )
{ {
m_ptr.charName = str; m_name = str;
m_flags = IsCharPtr; m_property = NULL;
m_isProperty = false;
} }
wxPGPropArgCls( const wchar_t* str ) wxPGPropArgCls( const wchar_t* str )
{ {
m_ptr.wcharName = str; m_name = str;
m_flags = IsWCharPtr; m_property = NULL;
m_isProperty = false;
} }
// This constructor is required for NULL. // This constructor is required for NULL.
wxPGPropArgCls( int ) wxPGPropArgCls( int )
{ {
m_ptr.property = NULL; m_property = NULL;
m_flags = IsProperty; m_isProperty = true;
} }
wxPGProperty* GetPtr( wxPropertyGridInterface* iface ) const; wxPGProperty* GetPtr( wxPropertyGridInterface* iface ) const;
wxPGProperty* GetPtr( const wxPropertyGridInterface* iface ) const wxPGProperty* GetPtr( const wxPropertyGridInterface* iface ) const
{ {
return GetPtr(const_cast<wxPropertyGridInterface*>(iface)); return GetPtr(const_cast<wxPropertyGridInterface*>(iface));
} }
wxPGProperty* GetPtr0() const { return m_ptr.property; } wxPGProperty* GetPtr0() const { return m_property; }
bool HasName() const { return (m_flags != IsProperty); } bool HasName() const { return !m_isProperty; }
const wxString& GetName() const { return *m_ptr.stringName; } const wxString& GetName() const { return m_name; }
private: private:
bool m_isProperty;
enum wxPGProperty* m_property;
{ wxString m_name;
IsProperty = 0x00,
IsWxString = 0x01,
IsCharPtr = 0x02,
IsWCharPtr = 0x04,
OwnsWxString = 0x10
};
union
{
wxPGProperty* property;
const char* charName;
const wchar_t* wcharName;
const wxString* stringName;
} m_ptr;
unsigned char m_flags;
}; };
typedef const wxPGPropArgCls& wxPGPropArg; typedef const wxPGPropArgCls& wxPGPropArg;

View File

@@ -39,19 +39,13 @@ IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxFont, WXDLLIMPEXP_PROPGRID)
wxPGProperty* wxPGPropArgCls::GetPtr( wxPropertyGridInterface* iface ) const wxPGProperty* wxPGPropArgCls::GetPtr( wxPropertyGridInterface* iface ) const
{ {
if ( m_flags == IsProperty ) if ( m_isProperty )
{ {
wxASSERT_MSG( m_ptr.property, wxS("invalid property ptr") ); wxASSERT_MSG( m_property, "invalid property ptr" );
return m_ptr.property; return m_property;
} }
else if ( m_flags & IsWxString )
return iface->GetPropertyByNameA(*m_ptr.stringName);
else if ( m_flags & IsCharPtr )
return iface->GetPropertyByNameA(m_ptr.charName);
else if ( m_flags & IsWCharPtr )
return iface->GetPropertyByNameA(m_ptr.wcharName);
return NULL; return iface->GetPropertyByNameA(m_name);
} }
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------