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.

142 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_DYNAMICOBJECT_H_INCLUDED
  24. #define JUCE_DYNAMICOBJECT_H_INCLUDED
  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. DynamicObject (const DynamicObject&);
  40. ~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 var() if no such property exists.
  49. */
  50. virtual const 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 (Identifier methodName,
  69. const var::NativeFunctionArgs& args);
  70. /** Adds a method to the class.
  71. This is basically the same as calling setProperty (methodName, (var::NativeFunction) myFunction), but
  72. helps to avoid accidentally invoking the wrong type of var constructor. It also makes
  73. the code easier to read,
  74. */
  75. void setMethod (Identifier methodName, var::NativeFunction function);
  76. //==============================================================================
  77. /** Removes all properties and methods from the object. */
  78. void clear();
  79. /** Returns the NamedValueSet that holds the object's properties. */
  80. NamedValueSet& getProperties() noexcept { return properties; }
  81. /** Calls var::clone() on all the properties that this object contains. */
  82. void cloneAllProperties();
  83. //==============================================================================
  84. /** Returns a clone of this object.
  85. The default implementation of this method just returns a new DynamicObject
  86. with a (deep) copy of all of its properties. Subclasses can override this to
  87. implement their own custom copy routines.
  88. */
  89. virtual Ptr clone();
  90. //==============================================================================
  91. /** Writes this object to a text stream in JSON format.
  92. This method is used by JSON::toString and JSON::writeToStream, and you should
  93. never need to call it directly, but it's virtual so that custom object types
  94. can stringify themselves appropriately.
  95. */
  96. virtual void writeAsJSON (OutputStream&, int indentLevel, bool allOnOneLine);
  97. private:
  98. //==============================================================================
  99. NamedValueSet properties;
  100. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  101. // These methods have been deprecated - use var::invoke instead
  102. virtual void invokeMethod (const Identifier&, const var*, int) {}
  103. #endif
  104. JUCE_LEAK_DETECTOR (DynamicObject)
  105. };
  106. #endif // JUCE_DYNAMICOBJECT_H_INCLUDED