Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <string.h> // strstr()
- #include <assert.h>
- #include <math.h>
- #include <angelscript.h>
- #include "../../../add_on/scriptstdstring/scriptstdstring.h"
- #include "../../../add_on/scripthelper/scripthelper.h"
- #include "../../../add_on/scriptarray/scriptarray.h"
- #include "../source/as_scriptengine.h"
- using namespace std;
- void ExecString(asIScriptEngine* engine, const string& arg);
- void ConfigureEngine(asIScriptEngine* engine);
- static asCScriptEngine* GetEngine()
- {
- static asCScriptEngine* eng = (asCScriptEngine*)asCreateScriptEngine();
- return eng;
- }
- int main(int argc, char** argv)
- {
- // Create the script engine
- asIScriptEngine* engine = GetEngine();
- if (engine == 0)
- {
- cout << "Failed to create script engine." << endl;
- return -1;
- }
- ConfigureEngine(engine);
- // Print some useful information and start the input loop
- cout << "Sample console using AngelScript " << asGetLibraryVersion() << " to perform scripted tasks." << endl;
- cout << "Type 'help' for more information." << endl;
- // Shut down the engine
- engine->ShutDownAndRelease();
- return 0;
- }
- void print(const string& s)
- {
- cout << s << "\n" << endl;
- }
- void MessageCallback(const asSMessageInfo* msg, void* param)
- {
- const char* type = "ERR ";
- if (msg->type == asMSGTYPE_WARNING)
- type = "WARN";
- else if (msg->type == asMSGTYPE_INFORMATION)
- type = "INFO";
- cout << msg->section << " (" << msg->row << ", " << msg->col << ") : " << type << " : " << msg->message << endl;
- }
- // Function implementation with native calling convention
- void PrintString(const string& str)
- {
- cout << str << "\n";
- }
- class testA
- {
- };
- testA* testA_Factory()
- {
- return new testA();
- }
- class testB : public testA
- {
- };
- testB* testB_Factory()
- {
- return new testB();
- }
- testA* testA_opImplCast_testB(testB* Object)
- {
- PrintString("testA opImplCast(testB@)");
- return Object;
- }
- void testA_Opcast(testA* Object, void** OutAddress, int TypeId)
- {
- if (!(TypeId & asTYPEID_OBJHANDLE))
- {
- PrintString("!(TypeId & asTYPEID_OBJHANDLE)");
- return;
- }
- asIScriptEngine* engine = GetEngine();
- asCObjectType* ScriptType = (asCObjectType*)engine->GetTypeInfoById(TypeId);
- if (ScriptType->GetFlags() & asOBJ_VALUE)
- {
- PrintString("(ScriptType->GetFlags() & asOBJ_VALUE)");
- return;
- }
- if (ScriptType->GetFlags() & asOBJ_POD)
- {
- PrintString("(ScriptType->GetFlags() & asOBJ_POD)");
- return;
- }
- PrintString("testA_Opcast");
- *(testA**)OutAddress = Object;
- }
- void testCasting()
- {
- auto* engine = GetEngine();
- std::string testfuncstr =
- "print(\"testing testCasting\"); \n"
- "testB tb = testB(); \n"
- "testB @ta = tb; \n"
- "if(ta is null) print(\"ta is null\"); \n"
- "testA @tc = ta; \n" //testA opImplCast(testB@):
- "if(tc is null) print(\"tc is null\"); \n"
- "testB @tab = cast<testB>(tc); \n" //testA_Opcast
- "if(tab is null) print(\"tab is null\"); \n" // tab is not null... ??
- ;
- ExecString(engine, testfuncstr);
- }
- void ConfigureEngine(asIScriptEngine* engine)
- {
- int r;
- engine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, 1);
- engine->SetEngineProperty(asEP_USE_CHARACTER_LITERALS, 1);
- engine->SetEngineProperty(asEP_ALLOW_MULTILINE_STRINGS, 1);
- engine->SetEngineProperty(asEP_SCRIPT_SCANNER, 1);
- engine->SetEngineProperty(asEP_OPTIMIZE_BYTECODE, 1);
- engine->SetEngineProperty(asEP_AUTO_GARBAGE_COLLECT, 0);
- engine->SetEngineProperty(asEP_ALTER_SYNTAX_NAMED_ARGS, 1);
- engine->SetEngineProperty(asEP_DISALLOW_VALUE_ASSIGN_FOR_REF_TYPE, 1);
- engine->SetEngineProperty(asEP_REQUIRE_ENUM_SCOPE, 1);
- engine->SetEngineProperty(asEP_ALWAYS_IMPL_DEFAULT_CONSTRUCT, 1);
- engine->SetEngineProperty(asEP_ALLOW_IMPLICIT_HANDLE_TYPES, 1);
- // Tell the engine to output any error messages to printf
- engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
- // Register the script string type
- // Look at the implementation for this function for more information
- // on how to register a custom string type, and other object types.
- RegisterStdString(engine);
- // Register the script array type
- RegisterScriptArray(engine, false);
- r = engine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(print), asCALL_CDECL); assert(r >= 0);
- r = engine->RegisterObjectType("testA", 0, asOBJ_REF | asOBJ_NOCOUNT | asOBJ_IMPLICIT_HANDLE); assert(r >= 0);
- r = engine->RegisterObjectBehaviour("testA", asBEHAVE_FACTORY, "testA @f()", asFUNCTION(testA_Factory), asCALL_CDECL); assert(r >= 0);
- r = engine->RegisterObjectMethod("testA", "void opCast(?&out)", asFUNCTION(testA_Opcast), asCALL_CDECL_OBJFIRST); assert(r >= 0);
- r = engine->RegisterObjectType("testB", 0, asOBJ_REF | asOBJ_NOCOUNT | asOBJ_IMPLICIT_HANDLE); assert(r >= 0);
- r = engine->RegisterObjectBehaviour("testB", asBEHAVE_FACTORY, "testB @f()", asFUNCTION(testB_Factory), asCALL_CDECL); assert(r >= 0);
- r = engine->RegisterObjectMethod("testB", "testA@ opImplCast() const", asFUNCTION(testA_opImplCast_testB), asCALL_CDECL_OBJFIRST); assert(r >= 0);
- testCasting();
- // Do not output anything else to printf
- engine->ClearMessageCallback();
- }
- void ExecString(asIScriptEngine* engine, const string& arg)
- {
- int r = ExecuteString(engine, arg.c_str(), engine->GetModule("console", asGM_CREATE_IF_NOT_EXISTS));
- if (r < 0)
- cout << "Invalid script statement. " << endl;
- else if (r == asEXECUTION_EXCEPTION)
- cout << "A script exception was raised." << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement