README 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. This directory provides extensions of platform APIs that PlatformWindow
  2. implementations can use to add support for more advanced APIs their
  3. platform provides.
  4. The sole intent of the extensions is to avoid casting from a PlatfromWindow
  5. to the extensions and avoid polluting the PlatformWindow APIs with
  6. unrelated methods.
  7. In order to let clients to access APIs of the extensions that an
  8. implementation of a PlatformWindow may have, one must set the
  9. extension as a property of the PlatformWindow. Then, the client can
  10. access the extension by providing the pointer to the PlatformWindow
  11. to corresponding GetMyExtension() global method that will check
  12. the properties of the PlatformWindow and return a pointer to the
  13. extension if such a property exists.
  14. For example,
  15. class MyExtension {
  16. public:
  17. virtual void MyExtensionMethod1() = 0;
  18. virtual void MyExtensionMethod2() = 0;
  19. protected:
  20. virtual ~MyExtension();
  21. void SetMyExtension(PlatformWindow* window, MyExtension* extension);
  22. };
  23. DEFINE_UI_CLASS_PROPERTY_TYPE(MyExtension*)
  24. DEFINE_UI_CLASS_PROPERTY_KEY(MyExtension*,
  25. kMyExtensionKey,
  26. nullptr)
  27. MyExtensions::~MyExtension() = default;
  28. void MyExtension::SetMyExtension(PlatformWindow* window, MyExtension* extension) {
  29. window->SetProperty(kMyExtensionKey, extension);
  30. }
  31. MyExtension* GetMyExtension(const PlatformWindow& window) {
  32. return window.GetProperty(kMyExtensionKey);
  33. }
  34. -------------------------------------------------------------------------------
  35. class MyWindow : public PlatformWindow,
  36. public MyExtension {
  37. public:
  38. MyWindow() {
  39. // Sets the MyExtension property of the PlatformWindow.
  40. // Cast to MyExtension for a better readibility.
  41. SetMyExtension(this, static_cast<MyExtension*>(this));
  42. }
  43. // PlatformWindow overrides:
  44. void Method1() override {}
  45. void Method2() override {}
  46. // MyExtension overrides:
  47. void MyExtensionMethod1() override {}
  48. void MyExtensionMethod2() override {}
  49. };
  50. -------------------------------------------------------------------------------
  51. class Client {
  52. public:
  53. void CreatePlatformWindow() {
  54. auto window = std::make_unique<MyWindow>();
  55. auto* extension = GetMyExtension(*window.get());
  56. }
  57. };