All Classes Namespaces Functions Variables Enumerations Enumerator Modules Pages
Public Member Functions | List of all members
ApiClass Class Referenceabstract
Inheritance diagram for ApiClass:
ScriptingApi::Console ScriptingApi::Engine ScriptingApi::Message ScriptingApi::ModuleIds ScriptingApi::Synth

Public Member Functions

 ApiClass (int numConstants_)
virtual Identifier getName () const =0
void addConstant (String constantName, var value)
const var getConstantValue (int index) const
int getConstantIndex (const Identifier &id) const
Identifier getConstantName (int index) const
void addFunction (const Identifier &id, call0 newFunction)
void addFunction1 (const Identifier &id, call1 newFunction)
void addFunction2 (const Identifier &id, call2 newFunction)
void addFunction3 (const Identifier &id, call3 newFunction)
void addFunction4 (const Identifier &id, call4 newFunction)
void addFunction5 (const Identifier &id, call5 newFunction)
void getIndexAndNumArgsForFunction (const Identifier &id, int &index, int &numArgs) const
var callFunction (int index, var *args, int numArgs)
void getAllFunctionNames (Array< Identifier > &ids) const
void getAllConstants (Array< Identifier > &ids) const

Detailed Description

A API class is a class with a fixed number of methods and constants that can be called from Javascript.

It is used to improve the performance of calling C++ functions from Javascript. This is achieved by resolving the function call at compile time making the call from Javascript almost as fast as a regular C++ function call (the arguments still need to be evaluated).

You can also define constants which are resolved into literal values at compile time making them exactly as fast as typing the literal value.

A ApiClass needs to be registered on the C++ side before the ScriptingEngine parses and executes the code using HiseJavascriptEngine::registerApiClass().

To use this class, subclass it, write the methods you want to expose to Javascript as member functions of your subclass and use these two macros to publish them to the Javascript Engine:

class MyApiClass: public ApiClass
ApiClass(0) // we don't need constants here...
ADD_API_METHOD_1(square); // This adds the method wrapper to the function list
double square(double x) const
return x*x;
struct Wrapper // You'll need a subclass called `Wrapper` for it to work...
// this defines a wrapper function that is using a static_cast to call the member function.
ADD_API_METHOD_WRAPPER_1(MyApiClass, square);

The Macros support up to 5 arguments. You'll need another macro for void functions: API_VOID_METHOD_WRAPPER_X, but apart from this, it is pretty straight forward...

For a living example of an API class take a look at eg. the Math class or the ScriptingApi::Engine class.

Constructor & Destructor Documentation

ApiClass ( int  numConstants_)

Creates a Api class with the given amount of constants.

If numConstants_ is not zero, you'll need to add the constants in your subclass constructor using addConstant(). You also need to register every function in your class there.

Member Function Documentation

void addConstant ( String  constantName,
var  value 

Adds a constant. You can give it a name (it must be a valid Identifier) and a value and will be resolved at compile time to accelerate the access to it.

void addFunction ( const Identifier &  id,
call0  newFunction 

Adds a function with no parameters.

You don't need to use this directly, but use the macro ADD_API_METHOD_0() for it.

void addFunction1 ( const Identifier &  id,
call1  newFunction 

Adds a function with one parameter.

You don't need to use this directly, but use the macro ADD_API_METHOD_1() for it.

void addFunction2 ( const Identifier &  id,
call2  newFunction 

Adds a function with two parameters.

You don't need to use this directly, but use the macro ADD_API_METHOD_2() for it.

void addFunction3 ( const Identifier &  id,
call3  newFunction 

Adds a function with three parameters.

You don't need to use this directly, but use the macro ADD_API_METHOD_3() for it.

void addFunction4 ( const Identifier &  id,
call4  newFunction 

Adds a function with four parameters.

You don't need to use this directly, but use the macro ADD_API_METHOD_4() for it.

void addFunction5 ( const Identifier &  id,
call5  newFunction 

Adds a function with five parameters.

You don't need to use this directly, but use the macro ADD_API_METHOD_5() for it.

var callFunction ( int  index,
var *  args,
int  numArgs 

Calls the function with the index and the argument data.

You'll need to call getIndexAndNumArgsForFunction() before calling this.

void getAllConstants ( Array< Identifier > &  ids) const

Returns all constant names as alphabetically sorted array. This is used by the autocomplete popup.

void getAllFunctionNames ( Array< Identifier > &  ids) const

This returns all function names alphabetically sorted. This is used by the autocomplete popup.

int getConstantIndex ( const Identifier &  id) const

Return the index for the given name.

Identifier getConstantName ( int  index) const

Returns the name for the constant as it is used in the scripting context.

const var getConstantValue ( int  index) const

Returns the constant at the given index.

void getIndexAndNumArgsForFunction ( const Identifier &  id,
int &  index,
int &  numArgs 
) const

This will fill in the information for the given function.

The JavascriptEngine uses this to resolve the function call into a function pointer at compile time. When the script is executed, this information will be used for blazing fast access to the methods.

virtual Identifier getName ( ) const
pure virtual

Overwrite this and return your API class name (which will be the name in Javascript for the object).

Implemented in ScriptingApi::ModuleIds.

©2017 HISE. This documentation is autogenerated from the HISE source code using Doxygen.