polserver/pol-core/bscript/fmodule.cpp

88 lines
1.8 KiB
C++
Raw Permalink Normal View History

/** @file
*
* @par History
*/
2016-02-11 22:54:43 +01:00
#include "fmodule.h"
#include <cstddef>
#include "userfunc.h"
namespace Pol
{
namespace Bscript
{
ModuleFunction::ModuleFunction( const char* fname, int i_nargs, UserFunction* i_uf )
: name( fname ), nargs( i_nargs ), funcidx( -1 ), uf( i_uf ), used( false )
{
}
2016-02-11 22:54:43 +01:00
FunctionalityModule::FunctionalityModule( const char* i_modname )
: have_indexes( false ), modulename( i_modname )
{
}
2016-02-11 22:54:43 +01:00
FunctionalityModule::~FunctionalityModule()
{
while ( !functions.empty() )
{
delete functions.back();
functions.pop_back();
}
// compiler only:
while ( !owned_userfuncs.empty() )
{
delete owned_userfuncs.back();
owned_userfuncs.pop_back();
}
}
2016-02-11 22:54:43 +01:00
void FunctionalityModule::addFunction( const char* funcname, int nparams, UserFunction* uf )
{
auto mf = new ModuleFunction( funcname, nparams, uf );
mf->funcidx = static_cast<unsigned int>( functions.size() );
functions.push_back( mf );
2016-02-11 22:54:43 +01:00
// compiler only:
if ( uf != nullptr )
owned_userfuncs.push_back( uf );
}
2016-02-11 22:54:43 +01:00
// compiler only:
bool FunctionalityModule::isFunc( const char* funcName, ModuleFunction** pmf, int* funcidx )
{
auto itr = functionsByName.find( funcName );
if ( itr != functionsByName.end() )
{
ModuleFunction* mf = ( *itr ).second;
2016-02-11 22:54:43 +01:00
if ( !mf->used )
{
mf->used = true;
int old_funcidx = mf->funcidx;
mf->funcidx = static_cast<unsigned int>( used_functions.size() );
used_functions.push_back( mf );
if ( old_funcidx != mf->funcidx )
{
std::swap( functions[old_funcidx], functions[mf->funcidx] );
functions[old_funcidx]->funcidx = old_funcidx;
}
}
*pmf = mf;
*funcidx = mf->funcidx;
return true;
}
return false;
}
2016-02-11 22:54:43 +01:00
void FunctionalityModule::fillFunctionsByName()
{
for ( auto& mf : functions )
{
functionsByName[mf->name] = mf;
2016-02-11 22:54:43 +01:00
}
}
}
2018-01-01 21:49:30 +01:00
}