clntdata.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/clntdata.h
  3. // Purpose: A mixin class for holding a wxClientData or void pointer
  4. // Author: Robin Dunn
  5. // Modified by:
  6. // Created: 9-Oct-2001
  7. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_CLNTDATAH__
  11. #define _WX_CLNTDATAH__
  12. #include "wx/defs.h"
  13. #include "wx/string.h"
  14. #include "wx/hashmap.h"
  15. typedef int (*wxShadowObjectMethod)(void*, void*);
  16. WX_DECLARE_STRING_HASH_MAP_WITH_DECL(
  17. wxShadowObjectMethod,
  18. wxShadowObjectMethods,
  19. class WXDLLIMPEXP_BASE
  20. );
  21. WX_DECLARE_STRING_HASH_MAP_WITH_DECL(
  22. void *,
  23. wxShadowObjectFields,
  24. class WXDLLIMPEXP_BASE
  25. );
  26. class WXDLLIMPEXP_BASE wxShadowObject
  27. {
  28. public:
  29. wxShadowObject() { }
  30. void AddMethod( const wxString &name, wxShadowObjectMethod method )
  31. {
  32. wxShadowObjectMethods::iterator it = m_methods.find( name );
  33. if (it == m_methods.end())
  34. m_methods[ name ] = method;
  35. else
  36. it->second = method;
  37. }
  38. bool InvokeMethod( const wxString &name, void* window, void* param, int* returnValue )
  39. {
  40. wxShadowObjectMethods::iterator it = m_methods.find( name );
  41. if (it == m_methods.end())
  42. return false;
  43. wxShadowObjectMethod method = it->second;
  44. int ret = (*method)(window, param);
  45. if (returnValue)
  46. *returnValue = ret;
  47. return true;
  48. }
  49. void AddField( const wxString &name, void* initialValue = NULL )
  50. {
  51. wxShadowObjectFields::iterator it = m_fields.find( name );
  52. if (it == m_fields.end())
  53. m_fields[ name ] = initialValue;
  54. else
  55. it->second = initialValue;
  56. }
  57. void SetField( const wxString &name, void* value )
  58. {
  59. wxShadowObjectFields::iterator it = m_fields.find( name );
  60. if (it == m_fields.end())
  61. return;
  62. it->second = value;
  63. }
  64. void* GetField( const wxString &name, void *defaultValue = NULL )
  65. {
  66. wxShadowObjectFields::iterator it = m_fields.find( name );
  67. if (it == m_fields.end())
  68. return defaultValue;
  69. return it->second;
  70. }
  71. private:
  72. wxShadowObjectMethods m_methods;
  73. wxShadowObjectFields m_fields;
  74. };
  75. // ----------------------------------------------------------------------------
  76. // what kind of client data do we have?
  77. enum wxClientDataType
  78. {
  79. wxClientData_None, // we don't know yet because we don't have it at all
  80. wxClientData_Object, // our client data is typed and we own it
  81. wxClientData_Void // client data is untyped and we don't own it
  82. };
  83. class WXDLLIMPEXP_BASE wxClientData
  84. {
  85. public:
  86. wxClientData() { }
  87. virtual ~wxClientData() { }
  88. };
  89. class WXDLLIMPEXP_BASE wxStringClientData : public wxClientData
  90. {
  91. public:
  92. wxStringClientData() : m_data() { }
  93. wxStringClientData( const wxString &data ) : m_data(data) { }
  94. void SetData( const wxString &data ) { m_data = data; }
  95. const wxString& GetData() const { return m_data; }
  96. private:
  97. wxString m_data;
  98. };
  99. // This class is a mixin that provides storage and management of "client
  100. // data." The client data stored can either be a pointer to a wxClientData
  101. // object in which case it is managed by the container (i.e. it will delete
  102. // the data when it's destroyed) or an untyped pointer which won't be deleted
  103. // by the container - but not both of them
  104. //
  105. // NOTE: This functionality is currently duplicated in wxEvtHandler in order
  106. // to avoid having more than one vtable in that class hierarchy.
  107. class WXDLLIMPEXP_BASE wxClientDataContainer
  108. {
  109. public:
  110. wxClientDataContainer();
  111. virtual ~wxClientDataContainer();
  112. void SetClientObject( wxClientData *data ) { DoSetClientObject(data); }
  113. wxClientData *GetClientObject() const { return DoGetClientObject(); }
  114. void SetClientData( void *data ) { DoSetClientData(data); }
  115. void *GetClientData() const { return DoGetClientData(); }
  116. protected:
  117. // The user data: either an object which will be deleted by the container
  118. // when it's deleted or some raw pointer which we do nothing with. Only
  119. // one type of data can be used with the given window, i.e. you cannot set
  120. // the void data and then associate the container with wxClientData or vice
  121. // versa.
  122. union
  123. {
  124. wxClientData *m_clientObject;
  125. void *m_clientData;
  126. };
  127. // client data accessors
  128. virtual void DoSetClientObject( wxClientData *data );
  129. virtual wxClientData *DoGetClientObject() const;
  130. virtual void DoSetClientData( void *data );
  131. virtual void *DoGetClientData() const;
  132. // what kind of data do we have?
  133. wxClientDataType m_clientDataType;
  134. };
  135. #endif // _WX_CLNTDATAH__