polserver/pol-core/bscript/token.cpp

255 lines
4.6 KiB
C++
Raw Permalink Normal View History

/** @file
*
* @par History
* - 2006/10/06 Shinigami: malloc.h -> stdlib.h
*/
2016-02-11 22:54:43 +01:00
#include "token.h"
#include <cstdio>
#include <cstring>
namespace Pol
{
namespace Bscript
{
unsigned int Token::_instances = 0;
2016-02-11 22:54:43 +01:00
#if STORE_INSTANCELIST
set<Token*> Token::_instancelist;
2016-02-11 22:54:43 +01:00
#endif
unsigned int Token::instances()
{
return _instances;
}
void Token::show_instances()
{
2016-02-11 22:54:43 +01:00
#if STORE_INSTANCELIST
for ( Instances::iterator itr = _instancelist.begin(), end = _instancelist.end(); itr != end;
++itr )
{
Token* tkn = ( *itr );
cout << tkn << ": " << ( *tkn ) << endl;
}
2016-02-11 22:54:43 +01:00
#endif
}
2016-02-11 22:54:43 +01:00
void Token::register_instance()
{
++_instances;
2016-02-11 22:54:43 +01:00
#if STORE_INSTANCELIST
_instancelist.insert( this );
2016-02-11 22:54:43 +01:00
#endif
}
2016-02-11 22:54:43 +01:00
void Token::unregister_instance()
{
#if defined( _DEBUG ) && defined( _DBG_TRYING_TO_FIND_WIN32_SHUTDOWN_ASSERTION )
if ( exit_signalled )
{
cout << "TOK/unreginst: " << ( token ? token : "<unknown>" ) << endl;
printOn( cout );
cout << "----" << endl;
}
2016-02-11 22:54:43 +01:00
#endif
--_instances;
2016-02-11 22:54:43 +01:00
#if STORE_INSTANCELIST
_instancelist.erase( this );
2016-02-11 22:54:43 +01:00
#endif
}
2016-02-11 22:54:43 +01:00
/**
* Initializes an empty token
*/
Token::Token()
: id( TOK_TERM ),
type( TYP_TERMINATOR ),
dval( 0.0 ),
precedence( -1 ),
dbg_filenum( 0 ),
dbg_linenum( 0 ),
lval( 0 ),
userfunc( nullptr ),
deprecated( false ),
2016-02-11 22:54:43 +01:00
ownsStr( false ),
module( Mod_Basic ),
token( nullptr )
{
register_instance();
}
2016-02-11 22:54:43 +01:00
/**
* Initializes a token copying data from a given one
*/
Token::Token( const Token& tok )
: id( tok.id ),
type( tok.type ),
dval( tok.dval ),
precedence( tok.precedence ),
dbg_filenum( tok.dbg_filenum ),
dbg_linenum( tok.dbg_linenum ),
lval( tok.lval ),
userfunc( tok.userfunc ),
deprecated( tok.deprecated ),
2016-02-11 22:54:43 +01:00
ownsStr( false ),
module( tok.module ),
token( nullptr )
{
register_instance();
if ( tok.token )
{
if ( !tok.ownsStr )
setStr( tok.token );
else
copyStr( tok.token );
}
}
2016-02-11 22:54:43 +01:00
/**
* Assigns the values from a given token to this one
*/
Token& Token::operator=( const Token& tok )
{
module = tok.module;
id = tok.id;
type = tok.type;
precedence = tok.precedence;
deprecated = tok.deprecated;
2016-02-11 22:54:43 +01:00
nulStr();
ownsStr = false;
if ( tok.token )
{
if ( !tok.ownsStr )
setStr( tok.token );
else
copyStr( tok.token );
}
dval = tok.dval;
lval = tok.lval;
2016-02-11 22:54:43 +01:00
dbg_filenum = tok.dbg_filenum;
dbg_linenum = tok.dbg_linenum;
2016-02-11 22:54:43 +01:00
userfunc = tok.userfunc;
2016-02-11 22:54:43 +01:00
return *this;
}
2016-02-11 22:54:43 +01:00
Token::Token( ModuleID i_module, BTokenId i_id, BTokenType i_type )
: id( i_id ),
type( i_type ),
dval( 0.0 ),
precedence( -1 ),
dbg_filenum( 0 ),
dbg_linenum( 0 ),
lval( 0 ),
userfunc( nullptr ),
deprecated( false ),
ownsStr( false ),
module( static_cast<unsigned char>( i_module ) ),
token( nullptr )
{
register_instance();
}
2016-02-11 22:54:43 +01:00
Token::Token( BTokenId i_id, BTokenType i_type )
: id( i_id ),
type( i_type ),
dval( 0.0 ),
precedence( -1 ),
dbg_filenum( 0 ),
dbg_linenum( 0 ),
lval( 0 ),
userfunc( nullptr ),
deprecated( false ),
ownsStr( false ),
module( Mod_Basic ),
token( nullptr )
{
register_instance();
}
2016-02-11 22:54:43 +01:00
Token::Token( ModuleID i_module, BTokenId i_id, BTokenType i_type, UserFunction* i_userfunc )
: id( i_id ),
type( i_type ),
dval( 0.0 ),
precedence( -1 ),
dbg_filenum( 0 ),
dbg_linenum( 0 ),
lval( 0 ),
userfunc( i_userfunc ),
deprecated( false ),
ownsStr( false ),
module( static_cast<unsigned char>( i_module ) ),
token( nullptr )
{
register_instance();
}
2016-02-11 22:54:43 +01:00
/**
* Erases all the content from the String (s), that becomes empty
*/
void Token::nulStr()
{
if ( token && ownsStr )
{
char* tmp = (char*)token;
delete[] tmp;
}
token = nullptr;
}
2016-02-11 22:54:43 +01:00
void Token::setStr( const char* s )
{
nulStr();
ownsStr = false;
token = s;
}
2016-02-11 22:54:43 +01:00
/**
* Copies value form the given null terminated char array into the String (s)
*/
void Token::copyStr( const char* s )
{
nulStr();
ownsStr = true;
size_t len = strlen( s );
auto tmp = new char[len + 1];
if ( tmp )
{
memcpy( tmp, s, len + 1 );
token = tmp;
}
else
{
token = nullptr;
}
}
2016-02-11 22:54:43 +01:00
void Token::copyStr( const char* s, int len )
{
nulStr();
ownsStr = true;
auto tmp = new char[static_cast<size_t>( len + 1 )];
if ( tmp )
{
memcpy( tmp, s, len );
tmp[len] = '\0';
token = tmp;
}
else
{
token = nullptr;
}
}
2016-02-11 22:54:43 +01:00
Token::~Token()
{
nulStr();
unregister_instance();
}
} // namespace Bscript
} // namespace Pol