OpenShot Library | libopenshot-audio  0.2.0
juce_DynamicObject.h
1 
2 /** @weakgroup juce_core-containers
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Represents a dynamically implemented object.
33 
34  This class is primarily intended for wrapping scripting language objects,
35  but could be used for other purposes.
36 
37  An instance of a DynamicObject can be used to store named properties, and
38  by subclassing hasMethod() and invokeMethod(), you can give your object
39  methods.
40 
41  @tags{Core}
42 */
44 {
45 public:
46  //==============================================================================
47  DynamicObject();
49  ~DynamicObject() override;
50 
52 
53  //==============================================================================
54  /** Returns true if the object has a property with this name.
55  Note that if the property is actually a method, this will return false.
56  */
57  virtual bool hasProperty (const Identifier& propertyName) const;
58 
59  /** Returns a named property.
60  This returns var() if no such property exists.
61  */
62  virtual const var& getProperty (const Identifier& propertyName) const;
63 
64  /** Sets a named property. */
65  virtual void setProperty (const Identifier& propertyName, const var& newValue);
66 
67  /** Removes a named property. */
68  virtual void removeProperty (const Identifier& propertyName);
69 
70  //==============================================================================
71  /** Checks whether this object has the specified method.
72 
73  The default implementation of this just checks whether there's a property
74  with this name that's actually a method, but this can be overridden for
75  building objects with dynamic invocation.
76  */
77  virtual bool hasMethod (const Identifier& methodName) const;
78 
79  /** Invokes a named method on this object.
80 
81  The default implementation looks up the named property, and if it's a method
82  call, then it invokes it.
83 
84  This method is virtual to allow more dynamic invocation to used for objects
85  where the methods may not already be set as properies.
86  */
87  virtual var invokeMethod (Identifier methodName,
88  const var::NativeFunctionArgs& args);
89 
90  /** Adds a method to the class.
91 
92  This is basically the same as calling setProperty (methodName, (var::NativeFunction) myFunction), but
93  helps to avoid accidentally invoking the wrong type of var constructor. It also makes
94  the code easier to read,
95  */
96  void setMethod (Identifier methodName, var::NativeFunction function);
97 
98  //==============================================================================
99  /** Removes all properties and methods from the object. */
100  void clear();
101 
102  /** Returns the NamedValueSet that holds the object's properties. */
103  NamedValueSet& getProperties() noexcept { return properties; }
104 
105  /** Calls var::clone() on all the properties that this object contains. */
106  void cloneAllProperties();
107 
108  //==============================================================================
109  /** Returns a clone of this object.
110  The default implementation of this method just returns a new DynamicObject
111  with a (deep) copy of all of its properties. Subclasses can override this to
112  implement their own custom copy routines.
113  */
114  virtual Ptr clone();
115 
116  //==============================================================================
117  /** Writes this object to a text stream in JSON format.
118  This method is used by JSON::toString and JSON::writeToStream, and you should
119  never need to call it directly, but it's virtual so that custom object types
120  can stringify themselves appropriately.
121  */
122  virtual void writeAsJSON (OutputStream&, int indentLevel, bool allOnOneLine, int maximumDecimalPlaces);
123 
124 private:
125  //==============================================================================
126  NamedValueSet properties;
127 
128  #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
129  // This method has been deprecated - use var::invoke instead
130  virtual void invokeMethod (const Identifier&, const var*, int) {}
131  #endif
132 
133  JUCE_LEAK_DETECTOR (DynamicObject)
134 };
135 
136 } // namespace juce
137 
138 /** @}*/
juce::ReferenceCountedObject
A base class which provides methods for reference-counting.
Definition: juce_ReferenceCountedObject.h:64
juce::NamedValueSet
Holds a set of named var objects.
Definition: juce_NamedValueSet.h:38
juce::DynamicObject
Represents a dynamically implemented object.
Definition: juce_DynamicObject.h:43
JUCE_API
#define JUCE_API
This macro is added to all JUCE public class declarations.
Definition: juce_StandardHeader.h:143
juce::OutputStream
The base class for streams that write data to some kind of destination.
Definition: juce_OutputStream.h:41
juce::var
A variant class, that can be used to hold a range of primitive values.
Definition: juce_Variant.h:45
juce::Identifier
Represents a string identifier, designed for accessing properties by name.
Definition: juce_Identifier.h:42
juce::ReferenceCountedObjectPtr< DynamicObject >
juce::var::NativeFunctionArgs
This structure is passed to a NativeFunction callback, and contains invocation details about the func...
Definition: juce_Variant.h:52
juce::DynamicObject::getProperties
NamedValueSet & getProperties() noexcept
Returns the NamedValueSet that holds the object's properties.
Definition: juce_DynamicObject.h:103