JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_DynamicObject.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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
23namespace juce
24{
25
26DynamicObject::DynamicObject()
27{
28}
29
30DynamicObject::DynamicObject (const DynamicObject& other)
31 : ReferenceCountedObject(), properties (other.properties)
32{
33}
34
35DynamicObject::~DynamicObject()
36{
37}
38
39bool DynamicObject::hasProperty (const Identifier& propertyName) const
40{
41 const var* const v = properties.getVarPointer (propertyName);
42 return v != nullptr && ! v->isMethod();
43}
44
45const var& DynamicObject::getProperty (const Identifier& propertyName) const
46{
47 return properties [propertyName];
48}
49
50void DynamicObject::setProperty (const Identifier& propertyName, const var& newValue)
51{
52 properties.set (propertyName, newValue);
53}
54
55void DynamicObject::removeProperty (const Identifier& propertyName)
56{
57 properties.remove (propertyName);
58}
59
60bool DynamicObject::hasMethod (const Identifier& methodName) const
61{
62 return getProperty (methodName).isMethod();
63}
64
65var DynamicObject::invokeMethod (Identifier method, const var::NativeFunctionArgs& args)
66{
67 if (auto function = properties [method].getNativeFunction())
68 return function (args);
69
70 return {};
71}
72
73void DynamicObject::setMethod (Identifier name, var::NativeFunction function)
74{
75 properties.set (name, var (function));
76}
77
78void DynamicObject::clear()
79{
80 properties.clear();
81}
82
83void DynamicObject::cloneAllProperties()
84{
85 for (int i = properties.size(); --i >= 0;)
86 if (auto* v = properties.getVarPointerAt (i))
87 *v = v->clone();
88}
89
90std::unique_ptr<DynamicObject> DynamicObject::clone() const
91{
92 auto result = std::make_unique<DynamicObject> (*this);
93 result->cloneAllProperties();
94 return result;
95}
96
97void DynamicObject::writeAsJSON (OutputStream& out, const JSON::FormatOptions& format)
98{
99 out << '{';
100 if (format.getSpacing() == JSON::Spacing::multiLine)
101 out << newLine;
102
103 const int numValues = properties.size();
104
105 for (int i = 0; i < numValues; ++i)
106 {
107 if (format.getSpacing() == JSON::Spacing::multiLine)
108 JSONFormatter::writeSpaces (out, format.getIndentLevel() + JSONFormatter::indentSize);
109
110 out << '"';
111 JSONFormatter::writeString (out, properties.getName (i));
112 out << "\":";
113
114 if (format.getSpacing() != JSON::Spacing::none)
115 out << ' ';
116
117 JSON::writeToStream (out,
118 properties.getValueAt (i),
119 format.withIndentLevel (format.getIndentLevel() + JSONFormatter::indentSize));
120
121 if (i < numValues - 1)
122 {
123 out << ",";
124
125 switch (format.getSpacing())
126 {
127 case JSON::Spacing::none: break;
128 case JSON::Spacing::singleLine: out << ' '; break;
129 case JSON::Spacing::multiLine: out << newLine; break;
130 }
131 }
132 else if (format.getSpacing() == JSON::Spacing::multiLine)
133 out << newLine;
134 }
135
136 if (format.getSpacing() == JSON::Spacing::multiLine)
137 JSONFormatter::writeSpaces (out, format.getIndentLevel());
138
139 out << '}';
140}
141
142} // namespace juce
Represents a string identifier, designed for accessing properties by name.
Allows formatting var objects as JSON with various configurable options.
Definition juce_JSON.h:102
The base class for streams that write data to some kind of destination.
A variant class, that can be used to hold a range of primitive values.
void remove(int index)
If the var is an array, this removes one of its elements.
JUCE Namespace.
NewLine newLine
A predefined object representing a new-line, which can be written to a string or stream.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
This structure is passed to a NativeFunction callback, and contains invocation details about the func...