I'm going through a piece of C++ source code for a personal project I'm working on and I can't for the world figure out what's happening in the following snippet:
Event EV_ScriptThread_Execute(
"execute", EV_DEFAULT, NULL, NULL, "Execute the thread."
);
class ScriptThread : Listener
{
{ &EV_ScriptThread_Execute, Execute },
{ NULL, NULL}
};
void ScriptThread::Execute(Event *ev)
{
//Stuff
}
As far as I can interpret, it creates an instance of the Event-class and stores it in EV_ScriptThread_Execute
. It then defines the ScriptThread-class, but I have no idea how to interpret the line after it.
Could someone explain me what happens? How would { &EV_ScriptThread_Execute, Execute },
look like if it was written in full, and not, as it seems, a short-hand notation.
EDIT: Apparantly I misintepreted the macro-definition which I thought would produce: class ScriptThread : Listener
, based on all the replies I went back and found out it actually construct an array of some sort. It still kind of leaves me in the dark on what happens... as I can't actually compile the code (since it's just a snippet), I can't really look at the precompiled files either.
What is the result of this?
ResponseDef<ScriptThread> ScriptThread::Responses[] =
{
{ &EV_ScriptThread_Execute, Execute },
{ NULL, NULL}
};
Another EDIT: So I found ResponseDef is a struct which looks like this:
template< class Type >
struct ResponseDef
{
Event *event;
void (Type::*response)(Event *event);
};
Conclusively this means the array gets filled up with a structure which takes both a pointer to an instance of Event
as well as a pointer to a method, which would explain the initializer!
Thanks to everyone for brining me back on track!
Look for a macro definition for Listener. I'm pretty sure that something horrible is hiding behind that. There is no other way to turn this snippet into something a C++ compiler will accept.
I expect something like:
#define Listener ScriptBaseClass { void Execute(Event *ev); } the_script[] =
Searching for the longest single token, EV_ScriptThread_Execute
, turns up this:
Event EV_ScriptDoor_DoInit( "doinit" );
Event EV_ScriptDoor_SetOpenThread( "openthread" );
Event EV_ScriptDoor_SetCloseThread( "closethread" );
ResponseDef ScriptDoor::Responses[] =
{
{ &EV_ScriptDoor_DoInit, ( Response )ScriptDoor::DoInit },
{ &EV_Door_DoClose, ( Response )ScriptDoor::DoClose },
{ &EV_Door_DoOpen, ( Response )ScriptDoor::DoOpen },
{ &EV_ScriptDoor_SetOpenThread, ( Response )ScriptDoor::SetOpenThread },
{ &EV_ScriptDoor_SetCloseThread, ( Response )ScriptDoor::SetCloseThread },
{ NULL, NULL }
};
From here: .htm">http://read.pudn.com/downloads99/sourcecode/windows/bitmap/406853/doors.cpp_.htm
It's clearly related code, and what is it from? Quake 2. So that's what we're looking at--for anything more detailed about how or why your specific bit of code works, we'd need to see (or find) more...and that may be a bit tricky given the revision history listed in the above file:
// 48 8/24/98 11:32a Markd
// Added Start method to threads, repladed all ProcessEvent(
// EV_ScriptThread_execute) with thread->Start( -1 )
Maybe someone else knows who Markd is; I don't.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.