MocoExtendProblem: Interface Between OpenSim and MATLAB for Rapidly Developing Direct Collocation Goals in Moco 1.1.0
add custom Moco goals to existing matlab scripts
dispatch.h File Reference
#include <mex.h>
#include <map>
#include <memory>
#include <string>
Include dependency graph for dispatch.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Operation
 Abstract operation class. More...
 
class  OperationCreator
 Base class for operation creators. More...
 
class  OperationCreatorImpl< OperationClass >
 Implementation of the operation creator to be used as composition in an Operator class. More...
 
class  OperationFactory
 Factory class for operations. More...
 
class  Session< T >
 Key-value storage to make a stateful MEX function. More...
 

Namespaces

namespace  mexplus
 MEX function arguments helper library.
 

Macros

#define MEXPLUS_AT_EXIT
 MEX dispatch library.
 
#define MEXPLUS_AT_INIT
 
#define MEXPLUS_AT_ERROR(name)
 
#define MEX_DEFINE(name)
 Define a MEX API function.
 
#define MEX_DEFINE2(name, admitter)
 Define a MEX API function using a private admitter.
 
#define MEX_DISPATCH
 Insert a function dispatching code.
 

Typedefs

typedef bool OperationNameAdmitter(const std::string &name)
 

Functions

void CreateOperation (OperationNameAdmitter *admitter, OperationCreator *creator)
 Register a new creator in OperationFactory.
 

Macro Definition Documentation

◆ MEX_DEFINE

#define MEX_DEFINE ( name)
Value:
class Operation_##name : public mexplus::Operation { \
public: \
virtual void operator()(int nlhs, \
mxArray *plhs[], \
int nrhs, \
const mxArray *prhs[]); \
private: \
static bool Operation_Admitter(const std::string& func) { \
return func == #name;\
} \
}; \
Operation_##name::creator_(Operation_##name::Operation_Admitter, NULL); \
void Operation_##name::operator()
Implementation of the operation creator to be used as composition in an Operator class.
Definition dispatch.h:124
Abstract operation class.
Definition dispatch.h:90
virtual void operator()(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])=0
Execute the operation.

Define a MEX API function.

Example:

MEX_DEFINE(myfunc) (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if (nrhs != 1 || nlhs > 1) mexErrMsgTxt("Wrong number of arguments."); ... }

◆ MEX_DEFINE2

#define MEX_DEFINE2 ( name,
admitter )
Value:
static const char* tag = NULL /*#name*/; \
class Operation_##name : public mexplus::Operation { \
public: \
virtual void operator()(int nlhs, \
mxArray *plhs[], \
int nrhs, \
const mxArray *prhs[]); \
private: \
}; \
Operation_##name::creator_(admitter, tag); \
void Operation_##name::operator()

Define a MEX API function using a private admitter.

Example:

static bool myfunc_admitter(std::string& name) { return name == "myfunc"; }

MEX_DEFINE2(myfunc, myfunc_admitter) (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if (nrhs != 1 || nlhs > 1) mexErrMsgTxt("Wrong number of arguments."); ... }

◆ MEX_DISPATCH

#define MEX_DISPATCH
Value:
void mexFunction(int nlhs, mxArray *plhs[], \
int nrhs, const mxArray *prhs[]) { \
if (nrhs < 1 || !mxIsChar(prhs[0])) \
mexErrMsgIdAndTxt("mexplus:dispatch:argumentError", \
"Invalid argument: missing operation."); \
std::string operation_name(\
mxGetChars(prhs[0]), \
mxGetChars(prhs[0]) + mxGetNumberOfElements(prhs[0])); \
std::unique_ptr<mexplus::Operation> operation(\
if (operation.get() == NULL) { \
MEXPLUS_AT_ERROR(operation_name); \
mexErrMsgIdAndTxt("mexplus:dispatch:argumentError", \
"Invalid operation: %s", operation_name.c_str()); \
} \
(*operation)(nlhs, plhs, nrhs - 1, prhs + 1); \
}
static Operation * create(const std::string &name)
Create a new instance of the registered operation.
Definition dispatch.h:147
#define MEXPLUS_AT_INIT
Definition dispatch.h:73
#define MEXPLUS_AT_EXIT
MEX dispatch library.
Definition dispatch.h:69

Insert a function dispatching code.

Use once per MEX binary.

◆ MEXPLUS_AT_ERROR

#define MEXPLUS_AT_ERROR ( name)

◆ MEXPLUS_AT_EXIT

#define MEXPLUS_AT_EXIT

MEX dispatch library.

Copyright 2014 Kota Yamaguchi.

This helper contains MEX_DEFINE() macro to help create a dispatchable MEX file. Two files are required to create a new mex function. Suppose you are creating two MEX functions myfunc and myfunc2. Then, make the following files.

myfunc.m

function output_args = myfunc(varargin)
%MYFUNC Description of the function.
%
%   Details go here.
%
  output_args = mylibrary(mfilename, varargin{:})
end

myfunc2.m

function output_args = myfunc(varargin)
%MYFUNC Description of the function.
%
%   Details go here.
%
  output_args = mylibrary(mfilename, varargin{:})
end

These files contains help documentation and a line to invoke the mex function.

mylibrary.cc

#include <mexplus/dispatch.h>

MEX_DEFINE(myfunc) (int nlhs, mxArray* plhs[],
                    int nrhs, const mxArray* prhs[]) {
  ...
}

MEX_DEFINE(myfunc2) (int nlhs, mxArray* plhs[],
                     int nrhs, const mxArray* prhs[]) {
  ...
}

MEX_DISPATCH

This file is the implementation of the mex function. The MEX_DEFINE macro defines an entry point of the function. MEX_DISPATCH macro at the end inserts necessary codes to dispatch function calls to an appropriate function.

Similarly, you can write another pair of .m (and C++) file to add to your library. You may split MEX_DEFINE macros in multiple C++ files. In that case, have MEX_DISPATCH macro in one of the files.

◆ MEXPLUS_AT_INIT

#define MEXPLUS_AT_INIT