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.

140 lines
5.8KB

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