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.

129 lines
5.2KB

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