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
3.5KB

  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. DynamicObject::DynamicObject()
  20. {
  21. }
  22. DynamicObject::DynamicObject (const DynamicObject& other)
  23. : ReferenceCountedObject(), properties (other.properties)
  24. {
  25. }
  26. DynamicObject::~DynamicObject()
  27. {
  28. }
  29. bool DynamicObject::hasProperty (const Identifier& propertyName) const
  30. {
  31. const var* const v = properties.getVarPointer (propertyName);
  32. return v != nullptr && ! v->isMethod();
  33. }
  34. const var& DynamicObject::getProperty (const Identifier& propertyName) const
  35. {
  36. return properties [propertyName];
  37. }
  38. void DynamicObject::setProperty (const Identifier& propertyName, const var& newValue)
  39. {
  40. properties.set (propertyName, newValue);
  41. }
  42. void DynamicObject::removeProperty (const Identifier& propertyName)
  43. {
  44. properties.remove (propertyName);
  45. }
  46. bool DynamicObject::hasMethod (const Identifier& methodName) const
  47. {
  48. return getProperty (methodName).isMethod();
  49. }
  50. var DynamicObject::invokeMethod (Identifier method, const var::NativeFunctionArgs& args)
  51. {
  52. if (auto function = properties [method].getNativeFunction())
  53. return function (args);
  54. return {};
  55. }
  56. void DynamicObject::setMethod (Identifier name, var::NativeFunction function)
  57. {
  58. properties.set (name, var (function));
  59. }
  60. void DynamicObject::clear()
  61. {
  62. properties.clear();
  63. }
  64. void DynamicObject::cloneAllProperties()
  65. {
  66. for (int i = properties.size(); --i >= 0;)
  67. if (auto* v = properties.getVarPointerAt (i))
  68. *v = v->clone();
  69. }
  70. DynamicObject::Ptr DynamicObject::clone()
  71. {
  72. Ptr d (new DynamicObject (*this));
  73. d->cloneAllProperties();
  74. return d;
  75. }
  76. void DynamicObject::writeAsJSON (OutputStream& out, const int indentLevel, const bool allOnOneLine, int maximumDecimalPlaces)
  77. {
  78. out << '{';
  79. if (! allOnOneLine)
  80. out << newLine;
  81. const int numValues = properties.size();
  82. for (int i = 0; i < numValues; ++i)
  83. {
  84. if (! allOnOneLine)
  85. JSONFormatter::writeSpaces (out, indentLevel + JSONFormatter::indentSize);
  86. out << '"';
  87. JSONFormatter::writeString (out, properties.getName (i));
  88. out << "\": ";
  89. JSONFormatter::write (out, properties.getValueAt (i), indentLevel + JSONFormatter::indentSize, allOnOneLine, maximumDecimalPlaces);
  90. if (i < numValues - 1)
  91. {
  92. if (allOnOneLine)
  93. out << ", ";
  94. else
  95. out << ',' << newLine;
  96. }
  97. else if (! allOnOneLine)
  98. out << newLine;
  99. }
  100. if (! allOnOneLine)
  101. JSONFormatter::writeSpaces (out, indentLevel);
  102. out << '}';
  103. }
  104. } // namespace juce