The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Helper class for dispatching callbacks to a lambda function.
  22. This class probably isn't something many users will use in their own code, but
  23. some juce classes use it as a helper to allow lambdas to be assigned to callback
  24. hooks - e.g. see its use in Button::onClick
  25. */
  26. template <typename OwnerClass>
  27. struct EventHandler
  28. {
  29. EventHandler() {}
  30. ~EventHandler() {}
  31. /** Assigns a void std::function to this callback.
  32. Note that this will replace any existing function that was previously assigned.
  33. */
  34. void operator= (std::function<void()> callbackToAttach)
  35. {
  36. callback = [=](OwnerClass&) { callbackToAttach(); };
  37. }
  38. /** Assigns a std::function to this callback which takes a reference to the
  39. source object that's making the callback.
  40. In the example of this class being used inside, e.g. a Button, then the
  41. callback parameter would be the Button that is doing the calling.
  42. Note that this will replace any existing function that was previously assigned.
  43. */
  44. void operator= (std::function<void(OwnerClass&)> callbackToAttach)
  45. {
  46. callback = static_cast<std::function<void(OwnerClass&)>&&> (callbackToAttach);
  47. }
  48. /** Removes any existing function that was previously assigned to the callback. */
  49. void reset() noexcept
  50. {
  51. callback = {};
  52. }
  53. /** @internal */
  54. void invoke (OwnerClass& owner)
  55. {
  56. if (callback != nullptr)
  57. callback (owner);
  58. }
  59. private:
  60. std::function<void(OwnerClass&)> callback;
  61. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EventHandler)
  62. };
  63. } // namespace juce