Advertisement
Guest User

Untitled

a guest
Dec 25th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #include "angelscript.h"
  2. #include "scriptstdstring.h"
  3. #include "scripthelper.h"
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include <stdexcept>
  7. using namespace std;
  8.  
  9. class EventSource
  10. {
  11. public:
  12.     EventSource() {refCount = 1; value = 42;};
  13.     virtual ~EventSource() {}
  14.     virtual void AddRef() {refCount++;}
  15.     virtual void Release() {if( --refCount == 0 ) delete this;}
  16.     int refCount;
  17.  
  18.     int value;
  19. };
  20.  
  21. class ASConsole : public EventSource
  22. {
  23. public:
  24.     static ASConsole *factory() { return new ASConsole(); }
  25. };
  26.  
  27. template<class A, class B> B* ASRefCast(A* a)
  28. {
  29.     // If the handle already is a null handle, then just return the null handle
  30.     if (a==NULL) return NULL;
  31.     // Now try to dynamically cast the pointer to the wanted type
  32.     B* b = dynamic_cast<B*>(a);
  33.     if (b!=NULL) {
  34.         // Since the cast was made, we need to increase the ref counter for the returned handle
  35.         b->AddRef();
  36.     }
  37.     cout << "ASRefCast: returning " << hex << b << endl;
  38.     return b;
  39. }
  40.  
  41. void Assert(bool condition)
  42. {
  43.     if (!condition) {
  44.         int line;
  45.         const char* section;
  46.         asGetActiveContext()->GetLineNumber(0, &line, &section);
  47.         cerr << "Assertion failed; Section " << section << " line " << line << cout;
  48.     }
  49. }
  50.  
  51.  
  52. void MessageCallback(const asSMessageInfo *msg, void *param)
  53. {
  54.   const char *type = "ERR ";
  55.   if( msg->type == asMSGTYPE_WARNING )
  56.     type = "WARN";
  57.   else if( msg->type == asMSGTYPE_INFORMATION )
  58.     type = "INFO";
  59.   printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
  60. }
  61.  
  62. void addListener(EventSource* source, int mask) {
  63.     cout << "addListener: source = " << hex << source << endl;
  64.     cout << "addListener: source.value = " << (int)source->value << endl;
  65.     cout << endl;
  66.     cout << endl;
  67. }
  68.  
  69. bool Test()
  70. {
  71.     bool fail = false;
  72.     int r;
  73.     asIScriptEngine *engine;
  74.  
  75.     // http://www.gamedev.n...sting-directly/
  76.     {
  77.  
  78.         ASConsole* c = new ASConsole();
  79.  
  80.         engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
  81.         engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
  82.         engine->RegisterGlobalFunction("void assert(bool)", asFUNCTION(Assert), asCALL_CDECL);
  83.         RegisterStdString(engine);
  84.  
  85.         engine->RegisterObjectType("EventSource", 0, asOBJ_REF);
  86.         engine->RegisterObjectBehaviour("EventSource", asBEHAVE_ADDREF, "void f()", asMETHOD(EventSource, AddRef), asCALL_THISCALL);
  87.         engine->RegisterObjectBehaviour("EventSource", asBEHAVE_RELEASE, "void f()", asMETHOD(EventSource, Release), asCALL_THISCALL);
  88.         engine->RegisterObjectProperty("EventSource", "int value", asOFFSET(EventSource, value));
  89.  
  90.         engine->RegisterObjectType("ASConsole", 0, asOBJ_REF);
  91.         engine->RegisterObjectBehaviour("ASConsole", asBEHAVE_FACTORY, "ASConsole @f()", asFUNCTION(ASConsole::factory), asCALL_CDECL);
  92.         engine->RegisterObjectBehaviour("ASConsole", asBEHAVE_ADDREF, "void f()", asMETHOD(ASConsole, AddRef), asCALL_THISCALL);
  93.         engine->RegisterObjectBehaviour("ASConsole", asBEHAVE_RELEASE, "void f()", asMETHOD(ASConsole, Release), asCALL_THISCALL);
  94.         engine->RegisterObjectBehaviour("ASConsole", asBEHAVE_IMPLICIT_REF_CAST, "EventSource@ f()",
  95.                 asFUNCTION((ASRefCast<ASConsole, EventSource>)), asCALL_CDECL_OBJLAST);
  96.  
  97.         engine->RegisterGlobalFunction("void addListener(EventSource &inout, const int &in)", asFUNCTION(addListener), asCALL_CDECL);
  98.  
  99.         asIScriptModule *mod = engine->GetModule("test", asGM_ALWAYS_CREATE);
  100.         mod->AddScriptSection("test",
  101.             "enum E { ET_READLINE = 24 } \n"
  102.             "ASConsole @console;\n"
  103.             "void main() \n"
  104.             "{ \n"
  105.             "  EventSource@ x = cast<EventSource>(console); \n"
  106.             "  addListener(x, ET_READLINE); \n"
  107.             "  addListener(cast<EventSource>(console), ET_READLINE); \n"
  108.             "} \n");
  109.         r = mod->Build();
  110.  
  111.         void** consoleVarAddr = (void**)mod->GetAddressOfGlobalVar(mod->GetGlobalVarIndexByName("console"));
  112.         *consoleVarAddr = c;
  113.  
  114.         if( r < 0 )
  115.             throw runtime_error("Compilation failed");
  116.  
  117.         r = ExecuteString(engine, "main()", mod);
  118.         if( r != asEXECUTION_FINISHED )
  119.             throw runtime_error("Execution failed");
  120.  
  121.         engine->Release();
  122.     }
  123.  
  124.     return fail;
  125. }
  126.  
  127.  
  128. int main(int argc, char** argv) {
  129.     Test();
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement