OpenShot Library | libopenshot-audio  0.2.0
juce_DynamicObject.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 DynamicObject::DynamicObject()
27 {
28 }
29 
30 DynamicObject::DynamicObject (const DynamicObject& other)
31  : ReferenceCountedObject(), properties (other.properties)
32 {
33 }
34 
35 DynamicObject::~DynamicObject()
36 {
37 }
38 
39 bool DynamicObject::hasProperty (const Identifier& propertyName) const
40 {
41  const var* const v = properties.getVarPointer (propertyName);
42  return v != nullptr && ! v->isMethod();
43 }
44 
45 const var& DynamicObject::getProperty (const Identifier& propertyName) const
46 {
47  return properties [propertyName];
48 }
49 
50 void DynamicObject::setProperty (const Identifier& propertyName, const var& newValue)
51 {
52  properties.set (propertyName, newValue);
53 }
54 
55 void DynamicObject::removeProperty (const Identifier& propertyName)
56 {
57  properties.remove (propertyName);
58 }
59 
60 bool DynamicObject::hasMethod (const Identifier& methodName) const
61 {
62  return getProperty (methodName).isMethod();
63 }
64 
66 {
67  if (auto function = properties [method].getNativeFunction())
68  return function (args);
69 
70  return {};
71 }
72 
73 void DynamicObject::setMethod (Identifier name, var::NativeFunction function)
74 {
75  properties.set (name, var (function));
76 }
77 
79 {
80  properties.clear();
81 }
82 
84 {
85  for (int i = properties.size(); --i >= 0;)
86  if (auto* v = properties.getVarPointerAt (i))
87  *v = v->clone();
88 }
89 
91 {
92  Ptr d (new DynamicObject (*this));
93  d->cloneAllProperties();
94  return d;
95 }
96 
97 void DynamicObject::writeAsJSON (OutputStream& out, const int indentLevel, const bool allOnOneLine, int maximumDecimalPlaces)
98 {
99  out << '{';
100  if (! allOnOneLine)
101  out << newLine;
102 
103  const int numValues = properties.size();
104 
105  for (int i = 0; i < numValues; ++i)
106  {
107  if (! allOnOneLine)
108  JSONFormatter::writeSpaces (out, indentLevel + JSONFormatter::indentSize);
109 
110  out << '"';
111  JSONFormatter::writeString (out, properties.getName (i));
112  out << "\": ";
113  JSONFormatter::write (out, properties.getValueAt (i), indentLevel + JSONFormatter::indentSize, allOnOneLine, maximumDecimalPlaces);
114 
115  if (i < numValues - 1)
116  {
117  if (allOnOneLine)
118  out << ", ";
119  else
120  out << ',' << newLine;
121  }
122  else if (! allOnOneLine)
123  out << newLine;
124  }
125 
126  if (! allOnOneLine)
127  JSONFormatter::writeSpaces (out, indentLevel);
128 
129  out << '}';
130 }
131 
132 } // namespace juce
juce::DynamicObject::setProperty
virtual void setProperty(const Identifier &propertyName, const var &newValue)
Sets a named property.
Definition: juce_DynamicObject.cpp:50
juce::NamedValueSet::getValueAt
const var & getValueAt(int index) const noexcept
Returns the value of the item at a given index.
Definition: juce_NamedValueSet.cpp:230
juce::DynamicObject::clone
virtual Ptr clone()
Returns a clone of this object.
Definition: juce_DynamicObject.cpp:90
juce::DynamicObject::cloneAllProperties
void cloneAllProperties()
Calls var::clone() on all the properties that this object contains.
Definition: juce_DynamicObject.cpp:83
juce::NamedValueSet::getVarPointer
var * getVarPointer(const Identifier &name) const noexcept
Returns a pointer to the var that holds a named value, or null if there is no value with this name.
Definition: juce_NamedValueSet.cpp:150
juce::NamedValueSet::getName
Identifier getName(int index) const noexcept
Returns the name of the value at a given index.
Definition: juce_NamedValueSet.cpp:221
juce::DynamicObject::hasMethod
virtual bool hasMethod(const Identifier &methodName) const
Checks whether this object has the specified method.
Definition: juce_DynamicObject.cpp:60
juce::DynamicObject
Represents a dynamically implemented object.
Definition: juce_DynamicObject.h:43
juce::NamedValueSet::remove
bool remove(const Identifier &name)
Removes a value from the set.
Definition: juce_NamedValueSet.cpp:205
juce::DynamicObject::clear
void clear()
Removes all properties and methods from the object.
Definition: juce_DynamicObject.cpp:78
juce::OutputStream
The base class for streams that write data to some kind of destination.
Definition: juce_OutputStream.h:41
juce::NamedValueSet::set
bool set(const Identifier &name, const var &newValue)
Changes or adds a named value.
Definition: juce_NamedValueSet.cpp:174
juce::NamedValueSet::getVarPointerAt
var * getVarPointerAt(int index) const noexcept
Returns the value of the item at a given index.
Definition: juce_NamedValueSet.cpp:239
juce::var
A variant class, that can be used to hold a range of primitive values.
Definition: juce_Variant.h:45
juce::DynamicObject::hasProperty
virtual bool hasProperty(const Identifier &propertyName) const
Returns true if the object has a property with this name.
Definition: juce_DynamicObject.cpp:39
juce::DynamicObject::setMethod
void setMethod(Identifier methodName, var::NativeFunction function)
Adds a method to the class.
Definition: juce_DynamicObject.cpp:73
juce::DynamicObject::getProperty
virtual const var & getProperty(const Identifier &propertyName) const
Returns a named property.
Definition: juce_DynamicObject.cpp:45
juce::DynamicObject::removeProperty
virtual void removeProperty(const Identifier &propertyName)
Removes a named property.
Definition: juce_DynamicObject.cpp:55
juce::Identifier
Represents a string identifier, designed for accessing properties by name.
Definition: juce_Identifier.h:42
juce::ReferenceCountedObjectPtr< DynamicObject >
juce::NamedValueSet::size
int size() const noexcept
Returns the total number of values that the set contains.
Definition: juce_NamedValueSet.cpp:125
juce::var::NativeFunctionArgs
This structure is passed to a NativeFunction callback, and contains invocation details about the func...
Definition: juce_Variant.h:52
juce::NamedValueSet::clear
void clear()
Removes all values.
Definition: juce_NamedValueSet.cpp:84
juce::DynamicObject::writeAsJSON
virtual void writeAsJSON(OutputStream &, int indentLevel, bool allOnOneLine, int maximumDecimalPlaces)
Writes this object to a text stream in JSON format.
Definition: juce_DynamicObject.cpp:97
juce::DynamicObject::invokeMethod
virtual var invokeMethod(Identifier methodName, const var::NativeFunctionArgs &args)
Invokes a named method on this object.
Definition: juce_DynamicObject.cpp:65