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