JUCE v8.0.9
JUCE API
 
Loading...
Searching...
No Matches
juce::DynamicObject Class Reference

Represents a dynamically implemented object. More...

#include <juce_DynamicObject.h>

Inheritance diagram for juce::DynamicObject:
Collaboration diagram for juce::DynamicObject:

Public Types

using Ptr = ReferenceCountedObjectPtr< DynamicObject >
 

Public Member Functions

 DynamicObject ()
 
 DynamicObject (const DynamicObject &)
 
void clear ()
 Removes all properties and methods from the object.
 
virtual std::unique_ptr< DynamicObjectclone () const
 Returns a clone of this object.
 
void cloneAllProperties ()
 Calls var::clone() on all the properties that this object contains.
 
void decReferenceCount () noexcept
 Decreases the object's reference count.
 
bool decReferenceCountWithoutDeleting () noexcept
 Decreases the object's reference count.
 
const NamedValueSetgetProperties () const noexcept
 Returns the NamedValueSet that holds the object's properties.
 
NamedValueSetgetProperties () noexcept
 Returns the NamedValueSet that holds the object's properties.
 
const vargetProperty (const Identifier &propertyName) const
 Returns a named property.
 
int getReferenceCount () const noexcept
 Returns the object's current reference count.
 
bool hasMethod (const Identifier &methodName) const
 Checks whether this object has a property with the given name that has a value of type NativeFunction.
 
bool hasProperty (const Identifier &propertyName) const
 Returns true if the object has a property with this name.
 
void incReferenceCount () noexcept
 Increments the object's reference count.
 
var invokeMethod (Identifier methodName, const var::NativeFunctionArgs &args)
 Invokes a named method on this object.
 
void removeProperty (const Identifier &propertyName)
 Removes a named property.
 
void setMethod (Identifier methodName, var::NativeFunction function)
 Adds a method to the class.
 
void setProperty (const Identifier &propertyName, const var &newValue)
 Sets a named property.
 
virtual void writeAsJSON (OutputStream &, const JSON::FormatOptions &)
 Writes this object to a text stream in JSON format.
 

Protected Member Functions

void resetReferenceCount () noexcept
 Resets the reference count to zero without deleting the object.
 

Private Member Functions

virtual void didModifyProperty (const Identifier &name, const std::optional< var > &value)
 Derived classes may override this function to take additional actions after properties are assigned or removed.
 

Private Attributes

NamedValueSet properties
 
Atomic< int > refCount { 0 }
 

Detailed Description

Represents a dynamically implemented object.

This class is primarily intended for wrapping scripting language objects, but could be used for other purposes.

An instance of a DynamicObject can be used to store named properties, and by subclassing hasMethod() and invokeMethod(), you can give your object methods.

@tags{Core}

Member Typedef Documentation

◆ Ptr

Constructor & Destructor Documentation

◆ DynamicObject() [1/2]

juce::DynamicObject::DynamicObject ( )

◆ DynamicObject() [2/2]

juce::DynamicObject::DynamicObject ( const DynamicObject )

Member Function Documentation

◆ clear()

void juce::DynamicObject::clear ( )

Removes all properties and methods from the object.

◆ clone()

virtual std::unique_ptr< DynamicObject > juce::DynamicObject::clone ( ) const
virtual

Returns a clone of this object.

The default implementation of this method just returns a new DynamicObject with a (deep) copy of all of its properties. Subclasses can override this to implement their own custom copy routines.

◆ cloneAllProperties()

void juce::DynamicObject::cloneAllProperties ( )

Calls var::clone() on all the properties that this object contains.

◆ decReferenceCount()

void juce::ReferenceCountedObject::decReferenceCount ( )
inlinenoexceptinherited

Decreases the object's reference count.

If the count gets to zero, the object will be deleted.

References jassert.

◆ decReferenceCountWithoutDeleting()

bool juce::ReferenceCountedObject::decReferenceCountWithoutDeleting ( )
inlinenoexceptinherited

Decreases the object's reference count.

If the count gets to zero, the object will not be deleted, but this method will return true, allowing the caller to take care of deletion.

References jassert.

◆ didModifyProperty()

virtual void juce::DynamicObject::didModifyProperty ( const Identifier name,
const std::optional< var > &  value 
)
inlineprivatevirtual

Derived classes may override this function to take additional actions after properties are assigned or removed.

Parameters
namethe name of the property that changed
valueif non-null, the value of the property after assignment if null, indicates that the property was removed

◆ getProperties() [1/2]

const NamedValueSet & juce::DynamicObject::getProperties ( ) const
inlinenoexcept

Returns the NamedValueSet that holds the object's properties.

◆ getProperties() [2/2]

NamedValueSet & juce::DynamicObject::getProperties ( )
inlinenoexcept

Returns the NamedValueSet that holds the object's properties.

◆ getProperty()

const var & juce::DynamicObject::getProperty ( const Identifier propertyName) const

Returns a named property.

This returns var() if no such property exists.

◆ getReferenceCount()

int juce::ReferenceCountedObject::getReferenceCount ( ) const
inlinenoexceptinherited

Returns the object's current reference count.

◆ hasMethod()

bool juce::DynamicObject::hasMethod ( const Identifier methodName) const

Checks whether this object has a property with the given name that has a value of type NativeFunction.

◆ hasProperty()

bool juce::DynamicObject::hasProperty ( const Identifier propertyName) const

Returns true if the object has a property with this name.

Note that if the property is actually a method, this will return false.

◆ incReferenceCount()

void juce::ReferenceCountedObject::incReferenceCount ( )
inlinenoexceptinherited

Increments the object's reference count.

This is done automatically by the smart pointer, but is public just in case it's needed for nefarious purposes.

◆ invokeMethod()

var juce::DynamicObject::invokeMethod ( Identifier  methodName,
const var::NativeFunctionArgs args 
)

Invokes a named method on this object.

The default implementation looks up the named property, and if it's a method call, then it invokes it.

◆ removeProperty()

void juce::DynamicObject::removeProperty ( const Identifier propertyName)

Removes a named property.

◆ resetReferenceCount()

void juce::ReferenceCountedObject::resetReferenceCount ( )
inlineprotectednoexceptinherited

Resets the reference count to zero without deleting the object.

You should probably never need to use this!

◆ setMethod()

void juce::DynamicObject::setMethod ( Identifier  methodName,
var::NativeFunction  function 
)

Adds a method to the class.

This is basically the same as calling setProperty (methodName, (var::NativeFunction) myFunction), but helps to avoid accidentally invoking the wrong type of var constructor. It also makes the code easier to read.

◆ setProperty()

void juce::DynamicObject::setProperty ( const Identifier propertyName,
const var newValue 
)

Sets a named property.

Referenced by juce::detail::DynamicObjectWrapper::setDispatcher().

◆ writeAsJSON()

virtual void juce::DynamicObject::writeAsJSON ( OutputStream ,
const JSON::FormatOptions  
)
virtual

Writes this object to a text stream in JSON format.

This method is used by JSON::toString and JSON::writeToStream, and you should never need to call it directly, but it's virtual so that custom object types can stringify themselves appropriately.

Member Data Documentation

◆ properties

NamedValueSet juce::DynamicObject::properties
private

◆ refCount

Atomic<int> juce::ReferenceCountedObject::refCount { 0 }
privateinherited

The documentation for this class was generated from the following file: