Skip to content

Commit

Permalink
Modules share the same (helper) template base class
Browse files Browse the repository at this point in the history
Optimize function name search for all modules not only uo by using an additional map, replaced c array with vector for functionsdef.
Simplified pragma pack order. (MSVC version is supported in all
compilers)
Removed compilerassert.h (asserteql,..) replaced with static_assert
Removed some old #ifs
  • Loading branch information
turleypol committed Dec 20, 2016
1 parent 6c772dc commit f9c3db9
Show file tree
Hide file tree
Showing 59 changed files with 713 additions and 1,218 deletions.
6 changes: 0 additions & 6 deletions pol-core/bscript/bobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,18 +693,12 @@ class Double : public BObjectImp
public:
explicit Double( double dval = 0.0 ) : BObjectImp( OTDouble ), dval_( dval ) {}
Double( const Double& dbl ) : BObjectImp( OTDouble ), dval_( dbl.dval_ ) {}
// FIXME: 2008 Upgrades needed here? Still valid, or why the hell we even do this anyway???
#if defined( __GNUC__ ) || ( defined( _WIN32 ) && _MSC_VER >= 1300 )
protected:
#else
private:
#endif
~Double() {}
public:
void* operator new( std::size_t len );
void operator delete( void* );


static BObjectImp* unpack( const char* pstr );
static BObjectImp* unpack( std::istream& is );
virtual std::string pack() const POL_OVERRIDE;
Expand Down
54 changes: 33 additions & 21 deletions pol-core/bscript/execmodl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
#include "bobject.h"
#endif

#include "executor.h"
#include "../clib/boostutils.h"
#include "../clib/maputil.h"
#include "executor.h"

#include <map>
#include <string>
#include <vector>

#ifdef _MSC_VER
#pragma warning( push )
Expand All @@ -36,7 +39,7 @@ typedef BObject* ( ExecutorModule::*ExecutorModuleFn )();
class ExecutorModule
{
public:
virtual ~ExecutorModule(){};
virtual ~ExecutorModule() = default;

BObjectImp* getParamImp( unsigned param );
BObjectImp* getParamImp( unsigned param, BObjectImp::BObjectType type );
Expand Down Expand Up @@ -74,13 +77,13 @@ class ExecutorModule

friend class Executor;

virtual int functionIndex( const char* funcname ) = 0; // returns -1 on not found
virtual int functionIndex( const std::string& funcname ) = 0; // returns -1 on not found
virtual BObjectImp* execFunc( unsigned idx ) = 0;
virtual std::string functionName( unsigned idx ) = 0;

private: // not implemented
ExecutorModule( const ExecutorModule& exec );
ExecutorModule& operator=( const ExecutorModule& exec );
ExecutorModule( const ExecutorModule& exec ) = delete;
ExecutorModule& operator=( const ExecutorModule& exec ) = delete;
};

// FIXME: this function doesn't seem to work.
Expand All @@ -91,53 +94,62 @@ BApplicObj<T>* getApplicObjParam( ExecutorModule& ex, unsigned param,
return static_cast<BApplicObj<T>*>( ex.getApplicObjParam( param, object_type ) );
}

#define callMemberFunction( object, ptrToMember ) ( ( object ).*( ptrToMember ) )

template <class T>
class TmplExecutorModule : public ExecutorModule
{
protected:
TmplExecutorModule( const char* modname, Executor& exec );
void register_function( const char* funcname, BObject ( T::*fptr )() );

public:
struct FunctionDef
{
const char* funcname;
const std::string funcname;
BObjectImp* ( T::*fptr )();
};
static FunctionDef function_table[];
static int function_table_size;

static std::vector<FunctionDef> function_table;
private:
virtual int functionIndex( const char* funcname ) POL_OVERRIDE;
static std::map<std::string, int, Clib::ci_cmp_pred> _func_idx_map;
static bool _func_map_init;
protected:
virtual int functionIndex( const std::string& funcname ) POL_OVERRIDE;
virtual BObjectImp* execFunc( unsigned idx ) POL_OVERRIDE;
virtual std::string functionName( unsigned idx ) POL_OVERRIDE;
};

template <class T>
std::map<std::string, int, Clib::ci_cmp_pred> TmplExecutorModule<T>::_func_idx_map;

template <class T>
bool TmplExecutorModule<T>::_func_map_init = false;

template <class T>
TmplExecutorModule<T>::TmplExecutorModule( const char* modname, Executor& ex )
: ExecutorModule( modname, ex )
{
if ( !_func_map_init )
{
for ( unsigned idx = 0; idx < function_table.size(); idx++ )
{
_func_idx_map[function_table[idx].funcname] = idx;
}
_func_map_init = true;
}
}

template <class T>
inline int TmplExecutorModule<T>::functionIndex( const char* name )
inline int TmplExecutorModule<T>::functionIndex( const std::string& name )
{
for ( int idx = 0; idx < function_table_size; idx++ )
{
if ( stricmp( name, function_table[idx].funcname ) == 0 )
return idx;
}
auto itr = _func_idx_map.find( name );
if ( itr != _func_idx_map.end() )
return itr->second;
return -1;
}

template <class T>
inline BObjectImp* TmplExecutorModule<T>::execFunc( unsigned funcidx )
{
T* derived = static_cast<T*>( this );

return callMemberFunction ( *derived, function_table[funcidx].fptr )();
return ( ( *derived ).*( function_table[funcidx].fptr ) )();
};

template <class T>
Expand Down
2 changes: 1 addition & 1 deletion pol-core/bscript/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ bool Executor::AttachFunctionalityModules()
// FIXME: should check number of params, blah.
if ( !func->name.get().empty() )
{
func->funcidx = em->functionIndex( func->name.get().c_str() );
func->funcidx = em->functionIndex( func->name.get() );
if ( func->funcidx == -1 )
{
ERROR_PRINT << "Unable to find " << fm->modulename.get() << "::" << func->name.get()
Expand Down
3 changes: 2 additions & 1 deletion pol-core/bscript/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ class BLong;
class String;

#ifdef ESCRIPT_PROFILE
#include <map>
struct profile_instr
{
unsigned long sum;
unsigned long max;
unsigned long min;
unsigned long count;
};
typedef map<std::string, profile_instr> escript_profile_map;
typedef std::map<std::string, profile_instr> escript_profile_map;
extern escript_profile_map EscriptProfileMap;
#endif

Expand Down
26 changes: 9 additions & 17 deletions pol-core/bscript/filefmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ namespace Pol
{
namespace Bscript
{
#ifdef _MSC_VER
#pragma pack( push, 1 )
#else
#pragma pack( 1 )
#endif

#define BSCRIPT_FILE_MAGIC0 'C'
#define BSCRIPT_FILE_MAGIC1 'E'
Expand Down Expand Up @@ -53,14 +49,14 @@ struct BSCRIPT_FILE_HDR
unsigned short version;
unsigned short globals;
};
asserteql( sizeof( BSCRIPT_FILE_HDR ), 6 );
static_assert( sizeof( BSCRIPT_FILE_HDR ) == 6, "size missmatch" );

struct BSCRIPT_SECTION_HDR
{
unsigned short type;
unsigned int length;
};
asserteql( sizeof( BSCRIPT_SECTION_HDR ), 6 );
static_assert( sizeof( BSCRIPT_SECTION_HDR ) == 6, "size missmatch" );

enum BSCRIPT_SECTION
{
Expand All @@ -78,32 +74,32 @@ struct BSCRIPT_MODULE_HDR
char modulename[14];
unsigned int nfuncs;
};
asserteql( sizeof( BSCRIPT_MODULE_HDR ), 18 );
static_assert( sizeof( BSCRIPT_MODULE_HDR ) == 18, "size missmatch" );

struct BSCRIPT_MODULE_FUNCTION
{
char funcname[33];
unsigned char nargs;
};
asserteql( sizeof( BSCRIPT_MODULE_FUNCTION ), 34 );
static_assert( sizeof( BSCRIPT_MODULE_FUNCTION ) == 34, "size missmatch" );

struct BSCRIPT_PROGDEF_HDR
{
unsigned expectedArgs;
unsigned char rfu[12];
};
asserteql( sizeof( BSCRIPT_PROGDEF_HDR ), 16 );
static_assert( sizeof( BSCRIPT_PROGDEF_HDR ) == 16, "size missmatch" );

struct BSCRIPT_GLOBALVARNAMES_HDR
{
unsigned nGlobalVars;
};
asserteql( sizeof( BSCRIPT_GLOBALVARNAMES_HDR ), 4 );
static_assert( sizeof( BSCRIPT_GLOBALVARNAMES_HDR ) == 4, "size missmatch" );
struct BSCRIPT_GLOBALVARNAME_HDR
{
unsigned namelen;
};
asserteql( sizeof( BSCRIPT_GLOBALVARNAME_HDR ), 4 );
static_assert( sizeof( BSCRIPT_GLOBALVARNAME_HDR ) == 4, "size missmatch" );

struct BSCRIPT_DBG_INSTRUCTION
{
Expand All @@ -114,21 +110,17 @@ struct BSCRIPT_DBG_INSTRUCTION
unsigned rfu1;
unsigned rfu2;
};
asserteql( sizeof( BSCRIPT_DBG_INSTRUCTION ), 24 );
static_assert( sizeof( BSCRIPT_DBG_INSTRUCTION ) == 24, "size missmatch" );

struct BSCRIPT_EXPORTED_FUNCTION
{
char funcname[33];
unsigned nargs;
unsigned PC;
};
asserteql( sizeof( BSCRIPT_EXPORTED_FUNCTION ), 41 );
static_assert( sizeof( BSCRIPT_EXPORTED_FUNCTION ) == 41, "size missmatch" );

#ifdef _MSC_VER
#pragma pack( pop )
#else
#pragma pack()
#endif
}
}
#endif
7 changes: 1 addition & 6 deletions pol-core/bscript/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1530,13 +1530,8 @@ BObjectImp* ObjArray::call_method_id( const int id, Executor& ex, bool /*forcebu
imp != NULL ) // 1-based index
{
--idx;
// FIXME: 2008 Upgrades needed here? Make sure still working correctly under 2008
#if ( defined( _WIN32 ) && _MSC_VER >= 1300 ) || ( !defined( USE_STLPORT ) && __GNUC__ )
BObjectRef tmp;
ref_arr.insert( ref_arr.begin() + idx, tmp );
#else
ref_arr.insert( ref_arr.begin() + idx );
#endif
BObjectRef& ref = ref_arr[idx];
ref.set( new BObject( imp->copy() ) );
}
Expand Down Expand Up @@ -1766,4 +1761,4 @@ void BApplicObjBase::printOn( std::ostream& os ) const
os << getStringRep();
}
}
}
}
1 change: 0 additions & 1 deletion pol-core/clib/StdAfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
// Project Includes (be really really carefull what to include!)

// explicit included since needed anyway for later includes
#include "compileassert.h"
#include "compilerspecifics.h"
#include "Debugging/LogSink.h"
#include "message_queue.h"
Expand Down
1 change: 0 additions & 1 deletion pol-core/clib/clib-2012.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@
<ClInclude Include="cfgsect.h" />
<ClInclude Include="clib.h" />
<ClInclude Include="clibopt.h" />
<ClInclude Include="compileassert.h" />
<ClInclude Include="compilerspecifics.h" />
<ClInclude Include="Debugging\ExceptionParser.h"/>
<ClInclude Include="Debugging\LogSink.h"/>
Expand Down
3 changes: 0 additions & 3 deletions pol-core/clib/clib-2012.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,6 @@
<ClInclude Include="clibopt.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="compileassert.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="dirlist.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
1 change: 0 additions & 1 deletion pol-core/clib/clib-2013.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@
<ClInclude Include="clib.h" />
<ClInclude Include="clibopt.h" />
<ClInclude Include="clib_MD5.h" />
<ClInclude Include="compileassert.h" />
<ClInclude Include="compilerspecifics.h" />
<ClInclude Include="Debugging\ExceptionParser.h" />
<ClInclude Include="Debugging\LogSink.h" />
Expand Down
5 changes: 1 addition & 4 deletions pol-core/clib/clib-2013.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@
<ClInclude Include="clibopt.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="compileassert.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="dirlist.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -359,4 +356,4 @@
<Filter>Program</Filter>
</ClInclude>
</ItemGroup>
</Project>
</Project>
3 changes: 1 addition & 2 deletions pol-core/clib/clib-2015.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@
<ClInclude Include="clib.h" />
<ClInclude Include="clibopt.h" />
<ClInclude Include="clib_MD5.h" />
<ClInclude Include="compileassert.h" />
<ClInclude Include="compilerspecifics.h" />
<ClInclude Include="Debugging\ExceptionParser.h" />
<ClInclude Include="Debugging\LogSink.h" />
Expand Down Expand Up @@ -346,4 +345,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
5 changes: 1 addition & 4 deletions pol-core/clib/clib-2015.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@
<ClInclude Include="clibopt.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="compileassert.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="dirlist.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -359,4 +356,4 @@
<Filter>Program</Filter>
</ClInclude>
</ItemGroup>
</Project>
</Project>
7 changes: 0 additions & 7 deletions pol-core/clib/clib.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,6 @@ void incStr( char* str );
#define PARAMS
#endif

#if 0
/* critical error handler. always returns failure. */
/* now fopen("A:\\test.txt", "rt") returns NULL if no disk in A.
without doing the abort retry fail crap
*/
void interrupt newcrit(PARAMS);
#endif
/// returns the current process size in bytes
size_t getCurrentMemoryUsage();

Expand Down
Loading

0 comments on commit f9c3db9

Please sign in to comment.