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.

127 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef __JUCE_DYNAMICOBJECT_JUCEHEADER__
  22. #define __JUCE_DYNAMICOBJECT_JUCEHEADER__
  23. #include "juce_NamedValueSet.h"
  24. #include "../memory/juce_ReferenceCountedObject.h"
  25. //==============================================================================
  26. /**
  27. Represents a dynamically implemented object.
  28. This class is primarily intended for wrapping scripting language objects,
  29. but could be used for other purposes.
  30. An instance of a DynamicObject can be used to store named properties, and
  31. by subclassing hasMethod() and invokeMethod(), you can give your object
  32. methods.
  33. */
  34. class JUCE_API DynamicObject : public ReferenceCountedObject
  35. {
  36. public:
  37. //==============================================================================
  38. DynamicObject();
  39. /** Destructor. */
  40. virtual ~DynamicObject();
  41. typedef ReferenceCountedObjectPtr<DynamicObject> Ptr;
  42. //==============================================================================
  43. /** Returns true if the object has a property with this name.
  44. Note that if the property is actually a method, this will return false.
  45. */
  46. virtual bool hasProperty (const Identifier& propertyName) const;
  47. /** Returns a named property.
  48. This returns a void if no such property exists.
  49. */
  50. virtual var getProperty (const Identifier& propertyName) const;
  51. /** Sets a named property. */
  52. virtual void setProperty (const Identifier& propertyName, const var& newValue);
  53. /** Removes a named property. */
  54. virtual void removeProperty (const Identifier& propertyName);
  55. //==============================================================================
  56. /** Checks whether this object has the specified method.
  57. The default implementation of this just checks whether there's a property
  58. with this name that's actually a method, but this can be overridden for
  59. building objects with dynamic invocation.
  60. */
  61. virtual bool hasMethod (const Identifier& methodName) const;
  62. /** Invokes a named method on this object.
  63. The default implementation looks up the named property, and if it's a method
  64. call, then it invokes it.
  65. This method is virtual to allow more dynamic invocation to used for objects
  66. where the methods may not already be set as properies.
  67. */
  68. virtual var invokeMethod (const Identifier& methodName,
  69. const var* parameters,
  70. int numParameters);
  71. /** Sets up a method.
  72. This is basically the same as calling setProperty (methodName, (var::MethodFunction) myFunction), but
  73. helps to avoid accidentally invoking the wrong type of var constructor. It also makes
  74. the code easier to read,
  75. The compiler will probably force you to use an explicit cast your method to a (var::MethodFunction), e.g.
  76. @code
  77. setMethod ("doSomething", (var::MethodFunction) &MyClass::doSomething);
  78. @endcode
  79. */
  80. void setMethod (const Identifier& methodName,
  81. var::MethodFunction methodFunction);
  82. //==============================================================================
  83. /** Removes all properties and methods from the object. */
  84. void clear();
  85. /** Returns the NamedValueSet that holds the object's properties. */
  86. NamedValueSet& getProperties() noexcept { return properties; }
  87. private:
  88. //==============================================================================
  89. NamedValueSet properties;
  90. JUCE_LEAK_DETECTOR (DynamicObject)
  91. };
  92. #endif // __JUCE_DYNAMICOBJECT_JUCEHEADER__