Pol  Revision:f37d500
fmodule.cpp
Go to the documentation of this file.
1 
6 #include "fmodule.h"
7 
8 #include <cstddef>
9 
10 #include "userfunc.h"
11 
12 namespace Pol
13 {
14 namespace Bscript
15 {
16 ModuleFunction::ModuleFunction( const char* fname, int i_nargs, UserFunction* i_uf )
17  : name( fname ), nargs( i_nargs ), funcidx( -1 ), uf( i_uf ), used( false )
18 {
19 }
20 
22 
24  : have_indexes( false ), modulename( i_modname )
25 {
26 }
27 
29 {
30  while ( !functions.empty() )
31  {
32  delete functions.back();
33  functions.pop_back();
34  }
35  // compiler only:
36  while ( !owned_userfuncs.empty() )
37  {
38  delete owned_userfuncs.back();
39  owned_userfuncs.pop_back();
40  }
41 }
42 
43 void FunctionalityModule::addFunction( const char* funcname, int nparams, UserFunction* uf )
44 {
45  auto mf = new ModuleFunction( funcname, nparams, uf );
46  mf->funcidx = static_cast<unsigned int>( functions.size() );
47  functions.push_back( mf );
48 
49  // compiler only:
50  if ( uf != NULL )
51  owned_userfuncs.push_back( uf );
52 }
53 
54 // compiler only:
55 bool FunctionalityModule::isFunc( const char* funcName, ModuleFunction** pmf, int* funcidx )
56 {
57  auto itr = functionsByName.find( funcName );
58  if ( itr != functionsByName.end() )
59  {
60  ModuleFunction* mf = ( *itr ).second;
61 
62  if ( !mf->used )
63  {
64  mf->used = true;
65  int old_funcidx = mf->funcidx;
66  mf->funcidx = static_cast<unsigned int>( used_functions.size() );
67  used_functions.push_back( mf );
68  if ( old_funcidx != mf->funcidx )
69  {
70  std::swap( functions[old_funcidx], functions[mf->funcidx] );
71  functions[old_funcidx]->funcidx = old_funcidx;
72  }
73  }
74  *pmf = mf;
75  *funcidx = mf->funcidx;
76  return true;
77  }
78  return false;
79 }
80 
82 {
83  for ( auto& mf : functions )
84  {
85  functionsByName[mf->name] = mf;
86  }
87 }
88 }
89 }
std::vector< UserFunction * > owned_userfuncs
Definition: fmodule.h:49
FunctionalityModule(const char *modname)
Definition: fmodule.cpp:23
FunctionsByName functionsByName
Definition: fmodule.h:47
std::vector< ModuleFunction * > used_functions
Definition: fmodule.h:48
std::vector< ModuleFunction * > functions
Definition: fmodule.h:43
std::string name
Definition: osmod.cpp:943
void addFunction(const char *funcname, int nparams, UserFunction *uf=NULL)
Definition: fmodule.cpp:43
bool isFunc(const char *funcName, ModuleFunction **pmf, int *funcidx)
Definition: fmodule.cpp:55
ModuleFunction(const char *fname, int nargs, UserFunction *uf)
Definition: fmodule.cpp:16
Definition: berror.cpp:12