Main Page   Compound List   File List   Compound Members   File Members  

Script Class Reference

A lightweight Lua wrapper. More...

#include <Script.h>

List of all members.

Public Types

typedef lua_CFunction CFunction
enum  {
  NOREF = LUA_NOREF
}
enum  {
  REFNIL = LUA_REFNIL
}
enum  {
  ANYTAG = LUA_ANYTAG
}

Public Methods

 Script (bool initStandardLibrary = true)
 Script (lua_State* state)
 ~Script ()
int GetTop ()
void SetTop (int index)
void PushValue (int index)
void Remove (int index)
void Insert (int index)
int StackSpace ()
Object GetObject (int index)
void PushObject (Object object)
int Equal (int index1, int index2)
int LessThan (int index1, int index2)
void PushBool (bool value)
void PushNil ()
void PushNumber (double n)
void PushLString (const char *s, size_t len)
void PushString (const char *s)
void PushCClosure (lua_CFunction fn, int n)
void PushUserTag (void *u, int tag)
Object GetGlobal (const char *name)
void GetTable (int index)
void RawGet (int index)
void RawGetI (int index, int n)
Object GetGlobals ()
void GetTagMethod (int tag, const char *event)
int GetRef (int ref)
Object NewTable ()
void SetGlobal (const char *name)
void SetTable (int index)
void RawSet (int index)
void RawSetI (int index, int n)
void SetGlobals ()
void SetTagMethod (int tag, const char *event)
int Ref (int lock)
int Call (int nargs, int nresults)
void RawCall (int nargs, int nresults)
int DoFile (const char *filename)
int DoString (const char *str)
int DoBuffer (const char *buff, size_t size, const char *name)
int NewTag ()
int CopyTagMethods (int tagto, int tagfrom)
void SetTag (int tag)
void Error (const char *s)
void Unref (int ref)
int Next (int index)
int GetN (int index)
void Concat (int n)
void Pop ()
void Pop (int amount)
void Register (const char* funcName, lua_CFunction function)
void PushUserData (void* u)
void PushCFunction (lua_CFunction f)
int CloneTag (int t)
bool IsFunction (int index)
bool IsString (int index)
bool IsTable (int index)
bool IsUserData (int index)
bool IsNil (int index)
bool IsNull (int index)
int ConfigGetInteger (const char* section, const char* entry, int defaultValue = 0)
float ConfigGetReal (const char* section, const char* entry, double defaultValue = 0.0)
const char* ConfigGetString (const char* section, const char* entry, const char* defaultValue = "")
void ConfigSetInteger (const char* section, const char* entry, int value)
 Assigns [value] to [section].[entry].

void ConfigSetReal (const char* section, const char* entry, double value)
 Assigns [value] to [section].[entry].

void ConfigSetString (const char* section, const char* entry, const char* value)
 Assigns [value] to [section].[entry].

void SaveText (const char* filename)
 Save the complete script state.

 operator lua_State * ()
lua_State* GetState () const

Public Attributes

lua_State* m_state
bool m_ownState

Friends

class  Object


Detailed Description

A lightweight Lua wrapper.

#include "Script.h"

static int Script_PrintNumber(lua_State* state)
{
    // But we can enable Script support using a special constructor.
    Script script(state);

    // Retrieve the number passed on the stack.
    Script::Object numberObj = script.GetObject(1);

    // Verify it is a number and print it.
    if (numberObj.IsNumber())
        printf("%f\n", numberObj.GetNumber());

    // No return values.
    return 0;
}


static int Script_Add(lua_State* state)
{
    // But we can enable Script support using a special constructor.
    Script script(state);
    using Script::Object;       // So we don't have to type Script::Object.

    // Retrieve the numbers passed on the stack.
    Object number1Obj = script.GetObject(1);
    Object number2Obj = script.GetObject(2);

    // Verify it is a number and print it.
    if (number1Obj.IsNumber()  &&  number2Obj.IsNumber())
    {
        script.PushNumber(number1Obj.GetNumber() + number2Obj.GetNumber());
    }
    else
    {
        script.PushNumber(0.0f);
    }

    // 1 return value.
    return 1;
}


void DoScriptIniWriteTest()
{
    Script script;

    script.ConfigSetReal("Environment", "WindSpeed", 55.0);
    script.ConfigSetString("Environment", "TypeStr", "Overcast");
    script.ConfigSetInteger("AnotherGroup", "IntegerValue", 1);

    script.SaveText("ScriptIniTest.dmp");
}


void DoScriptIniReadTest()
{
    Script script;

    script.DoFile("ScriptIniTest.dmp");

    float windSpeed = script.ConfigGetReal("Environment", "WindSpeed", 0.0);
    const char* typeStr = script.ConfigGetString("Environment", "TypeStr", "Clear");
    int integerValue = script.ConfigGetInteger("AnotherGroup", "IntegerValue");
}


void DoScriptCallbackTest()
{
    Script script;

    script.Register("PrintNumber", Script_PrintNumber);
    script.Register("Add", Script_Add);

    script.DoFile("ScriptCallbackTest.scr");
}


void DoScriptSaveTest()
{
    Script script;

    script.DoFile("ScriptSaveTest.scr");
    script.SaveText("ScriptSaveTest.dmp");
}


void DoScriptArrayTest()
{
    Script script;
    script.DoFile("ScriptArrayTest.scr");
    Script::Object testTableObj = script.GetGlobals().GetByName("TestArray");
    // or                           script.GetGlobal("TestArray");
    for (int i = 1; ; ++i)
    {
        Script::AutoBlock block(script);    // Automatic stack fixups.
        Script::Object entryObj = testTableObj.GetByIndex(i);
        if (entryObj.IsNil())
            break;
        if (entryObj.IsNumber())        // IsNumber() must ALWAYS come first.
            printf("%f\n", entryObj.GetNumber());
        else if (entryObj.IsString())   // IsString() returns true for a number or string.
            printf("%s\n", entryObj.GetString());
    }
}


int main(int argc, char* argv[])
{
    DoScriptIniWriteTest();
    DoScriptIniReadTest();
    DoScriptCallbackTest();
    DoScriptSaveTest();
    DoScriptArrayTest();

    return 0;
}


Member Typedef Documentation

typedef lua_CFunction Script::CFunction
 


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
NOREF  

anonymous enum
 

Enumeration values:
REFNIL  

anonymous enum
 

Enumeration values:
ANYTAG  


Constructor & Destructor Documentation

Script::Script ( bool initStandardLibrary = true )
 

Script::Script ( lua_State * state ) [inline]
 

Script::~Script ( ) [inline]
 


Member Function Documentation

int Script::Call ( int nargs,
int nresults ) [inline]
 

int Script::CloneTag ( int t ) [inline]
 

void Script::Concat ( int n ) [inline]
 

int Script::ConfigGetInteger ( const char * section,
const char * entry,
int defaultValue = 0 )
 

Returns:
Retrieves the value at [section].[entry]. If either [section] or [entry] doesn't exist, [defaultValue] is returned.

float Script::ConfigGetReal ( const char * section,
const char * entry,
double defaultValue = 0.0 )
 

Returns:
Retrieves the value at [section].[entry]. If either [section] or [entry] doesn't exist, [defaultValue] is returned.

const char * Script::ConfigGetString ( const char * section,
const char * entry,
const char * defaultValue = "" )
 

Returns:
Retrieves the value at [section].[entry]. If either [section] or [entry] doesn't exist, [defaultValue] is returned.

void Script::ConfigSetInteger ( const char * section,
const char * entry,
int value )
 

Assigns [value] to [section].[entry].

void Script::ConfigSetReal ( const char * section,
const char * entry,
double value )
 

Assigns [value] to [section].[entry].

void Script::ConfigSetString ( const char * section,
const char * entry,
const char * value )
 

Assigns [value] to [section].[entry].

int Script::CopyTagMethods ( int tagto,
int tagfrom ) [inline]
 

int Script::DoBuffer ( const char * buff,
size_t size,
const char * name ) [inline]
 

int Script::DoFile ( const char * filename ) [inline]
 

int Script::DoString ( const char * str ) [inline]
 

int Script::Equal ( int index1,
int index2 ) [inline]
 

void Script::Error ( const char * s ) [inline]
 

Object Script::GetGlobal ( const char * name ) [inline]
 

Object Script::GetGlobals ( ) [inline]
 

int Script::GetN ( int index ) [inline]
 

Object Script::GetObject ( int index ) [inline]
 

int Script::GetRef ( int ref ) [inline]
 

lua_State * Script::GetState ( ) const [inline]
 

void Script::GetTable ( int index ) [inline]
 

void Script::GetTagMethod ( int tag,
const char * event ) [inline]
 

int Script::GetTop ( ) [inline]
 

void Script::Insert ( int index ) [inline]
 

bool Script::IsFunction ( int index ) [inline]
 

bool Script::IsNil ( int index ) [inline]
 

bool Script::IsNull ( int index ) [inline]
 

bool Script::IsString ( int index ) [inline]
 

bool Script::IsTable ( int index ) [inline]
 

bool Script::IsUserData ( int index ) [inline]
 

int Script::LessThan ( int index1,
int index2 ) [inline]
 

Object Script::NewTable ( ) [inline]
 

int Script::NewTag ( ) [inline]
 

int Script::Next ( int index ) [inline]
 

void Script::Pop ( int amount ) [inline]
 

void Script::Pop ( ) [inline]
 

void Script::PushBool ( bool value ) [inline]
 

void Script::PushCClosure ( lua_CFunction fn,
int n ) [inline]
 

void Script::PushCFunction ( lua_CFunction f ) [inline]
 

void Script::PushLString ( const char * s,
size_t len ) [inline]
 

void Script::PushNil ( ) [inline]
 

void Script::PushNumber ( double n ) [inline]
 

void Script::PushObject ( Object object ) [inline]
 

void Script::PushString ( const char * s ) [inline]
 

void Script::PushUserData ( void * u ) [inline]
 

void Script::PushUserTag ( void * u,
int tag ) [inline]
 

void Script::PushValue ( int index ) [inline]
 

void Script::RawCall ( int nargs,
int nresults ) [inline]
 

void Script::RawGet ( int index ) [inline]
 

void Script::RawGetI ( int index,
int n ) [inline]
 

void Script::RawSet ( int index ) [inline]
 

void Script::RawSetI ( int index,
int n ) [inline]
 

int Script::Ref ( int lock ) [inline]
 

void Script::Register ( const char * funcName,
lua_CFunction function ) [inline]
 

void Script::Remove ( int index ) [inline]
 

void Script::SaveText ( const char * filename )
 

Save the complete script state.

void Script::SetGlobal ( const char * name ) [inline]
 

void Script::SetGlobals ( ) [inline]
 

void Script::SetTable ( int index ) [inline]
 

void Script::SetTag ( int tag ) [inline]
 

void Script::SetTagMethod ( int tag,
const char * event ) [inline]
 

void Script::SetTop ( int index ) [inline]
 

int Script::StackSpace ( ) [inline]
 

void Script::Unref ( int ref ) [inline]
 

Script::operator lua_State * ( ) [inline]
 


Friends And Related Function Documentation

class Object [friend]
 


Member Data Documentation

bool Script::m_ownState
 

lua_State * Script::m_state
 


The documentation for this class was generated from the following files:
Generated at Wed Dec 13 00:55:54 2000 for Script (Lua Wrapper) by doxygen1.2.2 written by Dimitri van Heesch, © 1997-2000