Loading...
Searching...
No Matches
juce::Expression Class Reference

## Detailed Description

A class for dynamically evaluating simple numeric expressions.

This class can parse a simple C-style string expression involving floating point numbers, named symbols and functions. The basic arithmetic operations of +, -, *, / are supported, as well as parentheses, and any alphanumeric identifiers are assumed to be named symbols which will be resolved when the expression is evaluated.

Expressions which use identifiers and functions require a subclass of Expression::Scope to be supplied when evaluating them, and this object is expected to be able to resolve the symbol names and perform the functions that are used.

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

- juce_core/maths/juce_Expression.h

## Classes

class | Scope
| When evaluating an Expression object, this class is used to resolve symbols and perform functions that the expression uses. More...
struct | Symbol
| Represents a symbol that is used in an Expression. More...

## Public Types

enum | Type { constantType , functionType , operatorType , symbolType }
| Expression type. More...

## Public Member Functions

| Expression ()
| Creates a simple expression with a value of 0.
| ~Expression ()
| Destructor.
| Expression (const Expression &)
| Creates a copy of an expression.
Expression & | operator= (const Expression &)
| Copies another expression.
| Expression (Expression &&) noexcept
| Move constructor.
Expression & | operator= (Expression &&) noexcept
| Move assignment operator.
| Expression (double constant)
| Creates a simple expression with a specified constant value.
| Expression (const String &stringToParse, String &parseError)
| Attempts to create an expression by parsing a string.
String | toString () const
| Returns a string version of the expression.
Expression | operator+ (const Expression &) const
| Returns an expression which is an addition operation of two existing expressions.
Expression | operator- (const Expression &) const
| Returns an expression which is a subtraction operation of two existing expressions.
Expression | operator* (const Expression &) const
| Returns an expression which is a multiplication operation of two existing expressions.
Expression | operator/ (const Expression &) const
| Returns an expression which is a division operation of two existing expressions.
Expression | operator- () const
| Returns an expression which performs a negation operation on an existing expression.
double | evaluate () const
| Evaluates this expression, without using a Scope.
double | evaluate (const Scope &scope) const
| Evaluates this expression, providing a scope that should be able to evaluate any symbols or functions that it uses.
double | evaluate (const Scope &scope, String &evaluationError) const
| Evaluates this expression, providing a scope that should be able to evaluate any symbols or functions that it uses.
Expression | adjustedToGiveNewResult (double targetValue, const Scope &scope) const
| Attempts to return an expression which is a copy of this one, but with a constant adjusted to make the expression resolve to a target value.
Expression | withRenamedSymbol (const Symbol &oldSymbol, const String &newName, const Scope &scope) const
| Returns a copy of this expression in which all instances of a given symbol have been renamed.
bool | referencesSymbol (const Symbol &symbol, const Scope &scope) const
| Returns true if this expression makes use of the specified symbol.
bool | usesAnySymbols () const
| Returns true if this expression contains any symbols.
void | findReferencedSymbols (Array< Symbol > &results, const Scope &scope) const
| Returns a list of all symbols that may be needed to resolve this expression in the given scope.
Type | getType () const noexcept
| Returns the type of this expression.
String | getSymbolOrFunction () const
| If this expression is a symbol, function or operator, this returns its identifier.
int | getNumInputs () const
| Returns the number of inputs to this expression.
Expression | getInput (int index) const
| Retrieves one of the inputs to this expression.

## Static Public Member Functions

static Expression | symbol (const String &symbol)
| Returns an Expression which is an identifier reference.
static Expression | function (const String &functionName, const Array< Expression > &parameters)
| Returns an Expression which is a function call.
static Expression | parse (String::CharPointerType &stringToParse, String &parseError)
| Returns an Expression which parses a string from a character pointer, and updates the pointer to indicate where it finished.

## Member Enums

## Type

enum juce::Expression::Type

Expression type.

See also
  Expression::getType()

Enumerator
constantType |
functionType |
operatorType |
symbolType |

## Constructors and Destructors

## Expression() [1/5]

juce::Expression::Expression | ( |  | ) |

Creates a simple expression with a value of 0.

Referenced by adjustedToGiveNewResult(), Expression(), Expression(), Expression(), Expression(), function(), getInput(), juce::Expression::Scope::getSymbolValue(), operator*(), operator+(), operator-(), operator-(), operator/(), operator=(), operator=(), parse(), symbol(), and withRenamedSymbol().

## ~Expression()

juce::Expression::~Expression | ( |  | ) |

Destructor.

## Expression() [2/5]

juce::Expression::Expression | ( | const Expression & |  | ) |

Creates a copy of an expression.

References Expression().

## Expression() [3/5]

juce::Expression::Expression | ( | Expression && |  | ) |

noexcept

Move constructor.

References Expression().

## Expression() [4/5]

juce::Expression::Expression | ( | double | constant | ) |

explicit

Creates a simple expression with a specified constant value.

References Expression().

## Expression() [5/5]

juce::Expression::Expression | ( | const String & | stringToParse,
|  | String & | parseError )

Attempts to create an expression by parsing a string.

Any errors are returned in the parseError argument provided.

References Expression().

## Member Functions

## operator=() [1/2]

Expression & juce::Expression::operator= | ( | const Expression & |  | ) |

Copies another expression.

References Expression().

## operator=() [2/2]

Expression & juce::Expression::operator= | ( | Expression && |  | ) |

noexcept

Move assignment operator.

References Expression().

## toString()

String juce::Expression::toString | ( |  | ) | const

Returns a string version of the expression.

References toString().

Referenced by toString().

## operator+()

Expression juce::Expression::operator+ | ( | const Expression & |  | ) | const

Returns an expression which is an addition operation of two existing expressions.

References Expression().

## operator-() [1/2]

Expression juce::Expression::operator- | ( | const Expression & |  | ) | const

Returns an expression which is a subtraction operation of two existing expressions.

References Expression().

## operator*()

Expression juce::Expression::operator* | ( | const Expression & |  | ) | const

Returns an expression which is a multiplication operation of two existing expressions.

References Expression().

## operator/()

Expression juce::Expression::operator/ | ( | const Expression & |  | ) | const

Returns an expression which is a division operation of two existing expressions.

References Expression().

## operator-() [2/2]

Expression juce::Expression::operator- | ( |  | ) | const

Returns an expression which performs a negation operation on an existing expression.

References Expression().

## symbol()

Expression juce::Expression::symbol | ( | const String & | symbol | ) |

static

Returns an Expression which is an identifier reference.

References Expression(), and symbol().

Referenced by juce::Expression::Scope::getSymbolValue(), referencesSymbol(), and symbol().

## function()

Expression juce::Expression::function | ( | const String & | functionName,
|  | const Array< Expression > & | parameters )

static

Returns an Expression which is a function call.

References Expression(), and function().

Referenced by function().

## parse()

Expression juce::Expression::parse | ( | String::CharPointerType & | stringToParse,
|  | String & | parseError )

static

Returns an Expression which parses a string from a character pointer, and updates the pointer to indicate where it finished.

The pointer is incremented so that on return, it indicates the character that follows the end of the expression that was parsed.

If there's a syntax error in parsing, the parseError argument will be set to a description of the problem.

References Expression(), and parse().

Referenced by parse().

## evaluate() [1/3]

double juce::Expression::evaluate | ( |  | ) | const

Evaluates this expression, without using a Scope.

Without a Scope, no symbols can be used, and only basic functions such as sin, cos, tan, min, max are available. To find out about any errors during evaluation, use the other version of this method which takes a String parameter.

## evaluate() [2/3]

double juce::Expression::evaluate | ( | const Scope & | scope | ) | const

Evaluates this expression, providing a scope that should be able to evaluate any symbols or functions that it uses.

To find out about any errors during evaluation, use the other version of this method which takes a String parameter.

## evaluate() [3/3]

double juce::Expression::evaluate | ( | const Scope & | scope,
|  | String & | evaluationError ) const

Evaluates this expression, providing a scope that should be able to evaluate any symbols or functions that it uses.

## adjustedToGiveNewResult()

Expression juce::Expression::adjustedToGiveNewResult | ( | double | targetValue,
|  | const Scope & | scope ) const

Attempts to return an expression which is a copy of this one, but with a constant adjusted to make the expression resolve to a target value.

E.g. if the expression is "x + 10" and x is 5, then asking for a target value of 8 will return the expression "x + 3". Obviously some expressions can't be reversed in this way, in which case they might just be adjusted by adding a constant to the original expression.

Exceptions

  Expression::EvaluationError |

References Expression().

## withRenamedSymbol()

Expression juce::Expression::withRenamedSymbol | ( | const Symbol & | oldSymbol,
|  | const String & | newName,
|  | const Scope & | scope ) const

Returns a copy of this expression in which all instances of a given symbol have been renamed.

References Expression().

## referencesSymbol()

bool juce::Expression::referencesSymbol | ( | const Symbol & | symbol,
|  | const Scope & | scope ) const

Returns true if this expression makes use of the specified symbol.

If a suitable scope is supplied, the search will dereference and recursively check all symbols, so that it can be determined whether this expression relies on the given symbol at any level in its evaluation. If the scope parameter is null, this just checks whether the expression contains any direct references to the symbol.

Exceptions

  Expression::EvaluationError |

References symbol().

## usesAnySymbols()

bool juce::Expression::usesAnySymbols | ( |  | ) | const

Returns true if this expression contains any symbols.

## findReferencedSymbols()

void juce::Expression::findReferencedSymbols | ( | Array< Symbol > & | results,
|  | const Scope & | scope ) const

Returns a list of all symbols that may be needed to resolve this expression in the given scope.

## getType()

Type juce::Expression::getType | ( |  | ) | const

noexcept

Returns the type of this expression.

## getSymbolOrFunction()

String juce::Expression::getSymbolOrFunction | ( |  | ) | const

If this expression is a symbol, function or operator, this returns its identifier.

References getSymbolOrFunction().

Referenced by getSymbolOrFunction().

## getNumInputs()

int juce::Expression::getNumInputs | ( |  | ) | const

Returns the number of inputs to this expression.

See also
  getInput

References getNumInputs().

Referenced by getNumInputs().

## getInput()

Expression juce::Expression::getInput | ( | int | index | ) | const

Retrieves one of the inputs to this expression.

See also
  getNumInputs

References Expression(), and getInput().

Referenced by getInput().

### Purchase

Get JUCE

### Discover

What's New in JUCE Features

### Learn

Documentaion Tutorials Made with JUCE Resources

### Support

JUCE Forum Newsletter Archive

### About

Contact Us JUCE Legal JUCE Licensing FAQ

### Events

Audio Developer Conference
© Raw Material Software Limited