VAMatlabFunctionMapping.h 6.62 KB
Newer Older
1 2 3
/*
 *  --------------------------------------------------------------------------------------------
 *
4 5 6 7 8 9
 *    VVV        VVV A           Virtual Acoustics (VA) | http://www.virtualacoustics.org
 *     VVV      VVV AAA          Licensed under the Apache License, Version 2.0
 *      VVV    VVV   AAA
 *       VVV  VVV     AAA        Copyright 2015-2017
 *        VVVVVV       AAA       Institute of Technical Acoustics (ITA)
 *         VVVV         AAA      RWTH Aachen University
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
 *
 *  --------------------------------------------------------------------------------------------
 */

#ifndef IW_FUNCTION_MAPPINGS
#define IW_FUNCTION_MAPPINGS

#include <assert.h>
#include <algorithm>
#include <map>
#include <string>
#include <vector>

#include <mex.h>

typedef void ( CommandFunction )( int, mxArray**, int, const mxArray** );

class FunctionMapping;
void AddFunctionMapping( FunctionMapping* pFuncMapping );

class FunctionInputArgument;
void AddFunctionInputArgument( const std::string& sFuncName, FunctionInputArgument* pArg );

class FunctionOutputArgument;
void AddFunctionOutputArgument( const std::string& sFuncName, FunctionOutputArgument* pArg );


class FunctionInputArgument
{
public:
	std::string sName;
	std::string sType;
	std::string sDesc;
	bool bOptional;
	std::string sDefaultValue;	// Default value expression in Matlab

	inline FunctionInputArgument( const std::string& sFunctionName, const std::string& sArgName, const std::string& sArgType, const std::string& sArgDesc, bool bArgOptional, const std::string& sArgDefaultValue )
		: sName( sArgName )
		, sType( sArgType )
		, sDesc( sArgDesc )
		, bOptional( bArgOptional )
		, sDefaultValue( sArgDefaultValue )
	{
		// Self register the argument globally
		AddFunctionInputArgument( sFunctionName, this );
	};
};

class FunctionOutputArgument
{
public:
	std::string sName;
	std::string sType;
	std::string sDesc;

	inline FunctionOutputArgument( const std::string& sFunctionName, const std::string& sArgName, const std::string& sArgType, const std::string& sArgDesc )
		: sName( sArgName )
		, sType( sArgType )
		, sDesc( sArgDesc )
	{
		// Self register the argument globally
		AddFunctionOutputArgument( sFunctionName, this );
	};
};

class FunctionMapping
{
public:
	std::string sName;
	CommandFunction* pAddr;
	bool bPublic;
	std::string sDesc;
	std::string sDoc;
	std::vector< FunctionInputArgument > vInputArgs;
	std::vector< FunctionOutputArgument > vOutputArgs;

	FunctionMapping( const std::string& sFunctionName, CommandFunction* pFunctionAddr, bool bPublicFunc, const std::string& sFunctionDesc, const std::string& sFunctionDoc )
		: sName( sFunctionName )
		, pAddr( pFunctionAddr )
		, bPublic( bPublicFunc )
		, sDesc( sFunctionDesc )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
91
		, sDoc( sFunctionDoc )
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	{
		// Self register the function globally
		AddFunctionMapping( this );
	};
};

// Function list (map used for O(log N) lookups via function name)
typedef std::map< std::string, FunctionMapping >::iterator FunctionMapIterator;
std::map< std::string, FunctionMapping > g_mFunctionMap;

void AddFunctionMapping( FunctionMapping* pFuncMapping )
{
	std::string sUpperName( pFuncMapping->sName );
	std::transform( sUpperName.begin(), sUpperName.end(), sUpperName.begin(), toupper );
	g_mFunctionMap.insert( std::pair< std::string, FunctionMapping >( sUpperName, *pFuncMapping ) );
};

void AddFunctionInputArgument( const std::string& sFuncName, FunctionInputArgument* pArg )
{
	std::string sUpperName( sFuncName );
	std::transform( sUpperName.begin(), sUpperName.end(), sUpperName.begin(), toupper );

	FunctionMapIterator it = g_mFunctionMap.find( sUpperName );
	assert( it != g_mFunctionMap.end() );
	it->second.vInputArgs.push_back( *pArg );
};

inline void AddFunctionOutputArgument( const std::string& sFuncName, FunctionOutputArgument* pArg )
{
	std::string sUpperName( sFuncName );
	std::transform( sUpperName.begin(), sUpperName.end(), sUpperName.begin(), toupper );

	FunctionMapIterator it = g_mFunctionMap.find( sUpperName );
	assert( it != g_mFunctionMap.end() );
	it->second.vOutputArgs.push_back( *pArg );
};

// INFO: Forward declare the function first. Allows to put documentation above the function body...
#define REGISTER_PUBLIC_FUNCTION(NAME, DESC, DOC) void NAME(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);\
FunctionMapping g_oFuncMapping_##NAME(#NAME, &NAME, true, DESC, DOC)

#define REGISTER_PRIVATE_FUNCTION(NAME) void NAME(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);\
FunctionMapping g_oFuncMapping_##NAME(#NAME, &NAME, false, "", "")

#define DECLARE_FUNCTION_REQUIRED_INARG(FUNCNAME, ARGNAME, ARGTYPE, ARGDESC) FunctionInputArgument g_oFuncInArg_##FUNCNAME##_##ARGNAME##(#FUNCNAME, #ARGNAME, ARGTYPE, ARGDESC, false, "")
#define DECLARE_FUNCTION_OPTIONAL_INARG(FUNCNAME, ARGNAME, ARGTYPE, ARGDESC, ARGDEF) FunctionInputArgument g_oFuncInArg_##FUNCNAME##_##ARGNAME##(#FUNCNAME, #ARGNAME, ARGTYPE, ARGDESC, true, ARGDEF)

#define DECLARE_FUNCTION_OUTARG(FUNCNAME, ARGNAME, ARGTYPE, ARGDESC) FunctionOutputArgument g_oFuncOutArg_##FUNCNAME##_##ARGNAME##(#FUNCNAME, #ARGNAME, ARGTYPE, ARGDESC)


inline mxArray* CreateFunctionInputArgumentStruct( std::vector<FunctionInputArgument>& vInputArgs )
{
	const mwSize nFields = 5;
	const char* ppszFieldNames[] = { "name", "type", "desc", "optional", "default" };

	mxArray* pStruct = mxCreateStructMatrix( 1, int( vInputArgs.size() ), nFields, ppszFieldNames );
	for( size_t i = 0; i < vInputArgs.size(); i++ ) {
		mxSetField( pStruct, ( mwIndex ) i, ppszFieldNames[ 0 ], mxCreateString( vInputArgs[ i ].sName.c_str() ) );
		mxSetField( pStruct, ( mwIndex ) i, ppszFieldNames[ 1 ], mxCreateString( vInputArgs[ i ].sType.c_str() ) );
		mxSetField( pStruct, ( mwIndex ) i, ppszFieldNames[ 2 ], mxCreateString( vInputArgs[ i ].sDesc.c_str() ) );
		mxSetField( pStruct, ( mwIndex ) i, ppszFieldNames[ 3 ], mxCreateLogicalScalar( vInputArgs[ i ].bOptional ) );
		mxSetField( pStruct, ( mwIndex ) i, ppszFieldNames[ 4 ], mxCreateString( vInputArgs[ i ].sDefaultValue.c_str() ) );
	}

	return pStruct;
};

inline mxArray* CreateFunctionOutputArgumentStruct( std::vector<FunctionOutputArgument>& vOutputArgs )
{
	const mwSize nFields = 3;
	const char* ppszFieldNames[] = { "name", "type", "desc" };

	mxArray* pStruct = mxCreateStructMatrix( 1, int( vOutputArgs.size() ), nFields, ppszFieldNames );
	for( size_t i = 0; i < vOutputArgs.size(); i++ ) {
		mxSetField( pStruct, ( mwIndex ) i, ppszFieldNames[ 0 ], mxCreateString( vOutputArgs[ i ].sName.c_str() ) );
		mxSetField( pStruct, ( mwIndex ) i, ppszFieldNames[ 1 ], mxCreateString( vOutputArgs[ i ].sType.c_str() ) );
		mxSetField( pStruct, ( mwIndex ) i, ppszFieldNames[ 2 ], mxCreateString( vOutputArgs[ i ].sDesc.c_str() ) );
	}

	return pStruct;
};

#endif IW_FUNCTION_MAPPINGS