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.

123 lines
4.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_DYNAMICOBJECT_JUCEHEADER__
  19. #define __JUCE_DYNAMICOBJECT_JUCEHEADER__
  20. #include "juce_NamedValueSet.h"
  21. #include "../memory/juce_ReferenceCountedObject.h"
  22. //==============================================================================
  23. /**
  24. Represents a dynamically implemented object.
  25. This class is primarily intended for wrapping scripting language objects,
  26. but could be used for other purposes.
  27. An instance of a DynamicObject can be used to store named properties, and
  28. by subclassing hasMethod() and invokeMethod(), you can give your object
  29. methods.
  30. */
  31. class JUCE_API DynamicObject : public ReferenceCountedObject
  32. {
  33. public:
  34. //==============================================================================
  35. DynamicObject();
  36. /** Destructor. */
  37. virtual ~DynamicObject();
  38. //==============================================================================
  39. /** Returns true if the object has a property with this name.
  40. Note that if the property is actually a method, this will return false.
  41. */
  42. virtual bool hasProperty (const Identifier& propertyName) const;
  43. /** Returns a named property.
  44. This returns a void if no such property exists.
  45. */
  46. virtual var getProperty (const Identifier& propertyName) const;
  47. /** Sets a named property. */
  48. virtual void setProperty (const Identifier& propertyName, const var& newValue);
  49. /** Removes a named property. */
  50. virtual void removeProperty (const Identifier& propertyName);
  51. //==============================================================================
  52. /** Checks whether this object has the specified method.
  53. The default implementation of this just checks whether there's a property
  54. with this name that's actually a method, but this can be overridden for
  55. building objects with dynamic invocation.
  56. */
  57. virtual bool hasMethod (const Identifier& methodName) const;
  58. /** Invokes a named method on this object.
  59. The default implementation looks up the named property, and if it's a method
  60. call, then it invokes it.
  61. This method is virtual to allow more dynamic invocation to used for objects
  62. where the methods may not already be set as properies.
  63. */
  64. virtual var invokeMethod (const Identifier& methodName,
  65. const var* parameters,
  66. int numParameters);
  67. /** Sets up a method.
  68. This is basically the same as calling setProperty (methodName, (var::MethodFunction) myFunction), but
  69. helps to avoid accidentally invoking the wrong type of var constructor. It also makes
  70. the code easier to read,
  71. The compiler will probably force you to use an explicit cast your method to a (var::MethodFunction), e.g.
  72. @code
  73. setMethod ("doSomething", (var::MethodFunction) &MyClass::doSomething);
  74. @endcode
  75. */
  76. void setMethod (const Identifier& methodName,
  77. var::MethodFunction methodFunction);
  78. //==============================================================================
  79. /** Removes all properties and methods from the object. */
  80. void clear();
  81. /** Returns the NamedValueSet that holds the object's properties. */
  82. NamedValueSet& getProperties() noexcept { return properties; }
  83. private:
  84. //==============================================================================
  85. NamedValueSet properties;
  86. JUCE_LEAK_DETECTOR (DynamicObject);
  87. };
  88. #endif // __JUCE_DYNAMICOBJECT_JUCEHEADER__