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.

133 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. Represents a dynamically implemented object.
  22. This class is primarily intended for wrapping scripting language objects,
  23. but could be used for other purposes.
  24. An instance of a DynamicObject can be used to store named properties, and
  25. by subclassing hasMethod() and invokeMethod(), you can give your object
  26. methods.
  27. @tags{Core}
  28. */
  29. class JUCE_API DynamicObject : public ReferenceCountedObject
  30. {
  31. public:
  32. //==============================================================================
  33. DynamicObject();
  34. DynamicObject (const DynamicObject&);
  35. ~DynamicObject() override;
  36. using Ptr = ReferenceCountedObjectPtr<DynamicObject>;
  37. //==============================================================================
  38. /** Returns true if the object has a property with this name.
  39. Note that if the property is actually a method, this will return false.
  40. */
  41. virtual bool hasProperty (const Identifier& propertyName) const;
  42. /** Returns a named property.
  43. This returns var() if no such property exists.
  44. */
  45. virtual const var& getProperty (const Identifier& propertyName) const;
  46. /** Sets a named property. */
  47. virtual void setProperty (const Identifier& propertyName, const var& newValue);
  48. /** Removes a named property. */
  49. virtual void removeProperty (const Identifier& propertyName);
  50. //==============================================================================
  51. /** Checks whether this object has the specified method.
  52. The default implementation of this just checks whether there's a property
  53. with this name that's actually a method, but this can be overridden for
  54. building objects with dynamic invocation.
  55. */
  56. virtual bool hasMethod (const Identifier& methodName) const;
  57. /** Invokes a named method on this object.
  58. The default implementation looks up the named property, and if it's a method
  59. call, then it invokes it.
  60. This method is virtual to allow more dynamic invocation to used for objects
  61. where the methods may not already be set as properties.
  62. */
  63. virtual var invokeMethod (Identifier methodName,
  64. const var::NativeFunctionArgs& args);
  65. /** Adds a method to the class.
  66. This is basically the same as calling setProperty (methodName, (var::NativeFunction) myFunction), but
  67. helps to avoid accidentally invoking the wrong type of var constructor. It also makes
  68. the code easier to read.
  69. */
  70. void setMethod (Identifier methodName, var::NativeFunction function);
  71. //==============================================================================
  72. /** Removes all properties and methods from the object. */
  73. void clear();
  74. /** Returns the NamedValueSet that holds the object's properties. */
  75. NamedValueSet& getProperties() noexcept { return properties; }
  76. /** Calls var::clone() on all the properties that this object contains. */
  77. void cloneAllProperties();
  78. //==============================================================================
  79. /** Returns a clone of this object.
  80. The default implementation of this method just returns a new DynamicObject
  81. with a (deep) copy of all of its properties. Subclasses can override this to
  82. implement their own custom copy routines.
  83. */
  84. virtual Ptr clone();
  85. //==============================================================================
  86. /** Writes this object to a text stream in JSON format.
  87. This method is used by JSON::toString and JSON::writeToStream, and you should
  88. never need to call it directly, but it's virtual so that custom object types
  89. can stringify themselves appropriately.
  90. */
  91. virtual void writeAsJSON (OutputStream&, int indentLevel, bool allOnOneLine, int maximumDecimalPlaces);
  92. private:
  93. //==============================================================================
  94. NamedValueSet properties;
  95. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  96. // This method has been deprecated - use var::invoke instead
  97. virtual void invokeMethod (const Identifier&, const var*, int) {}
  98. #endif
  99. JUCE_LEAK_DETECTOR (DynamicObject)
  100. };
  101. } // namespace juce