PDA

View Full Version : (nvidia SDK, nv_win lib) can someone explain to me whats going on here please?



torisutan
09-28-2003, 08:27 PM
hi guys,
i'm going through the src for the nv_win library packaged with the nvidia sdk, and i've come across some confusing bits of code, and was wondering if someone might beable to explain to me whats going on.

ok, whats confusing me the the vn_app_cmd and nv_cmd_mgr functions (located in vn_win_app.h, and vn_command.h/.cpp)

I will cut the code a bit to lessen the amount of explaination i have to do.

so, first we have the definition of the nv_app_cmd class




// sniplet from nv_win_app.h

class nv_app_cmd
{
public:
// force it to create a vtable?
virtual ~nv_app_cmd() { }
};

// function pointer prototype for command handling functions.
typedef void (nv_app_cmd::*PFN_APP_CMD)(nv_cmd_args * args);

struct nv_fn_app_cmd
{
nv_app_cmd * fn_app_cmd;
PFN_APP_CMD pfn;
};



first, what exactly is happening with the typedef?
i can understand it without the nv_app_cmd:: part, but i can't quite figure out how the nv_app_cmd:: bit works. how does PFN_APP_CMD relate to vn_app_cmd?

also, the nv_app_cmd * fn_app_cmd confuses me. the nv_app_cmd class has nothing but a deconstructor, so what is the point of it?





// sniplet from nv_command.h

typedef std::map<std::string,nv_fn_app_cmd*> app_cmd_map;
typedef app_cmd_map::value_type app_cmd_pair;
typedef app_cmd_map::iterator app_cmd_it;

class nv_cmd_mgr : public nv_app_cmd
{
nv_cmd_mgr();
...
public:
bool add_cmd(const char * name, nv_fn_app_cmd * pfn);
bool send_cmd(const char * str_cmd, nv_cmd_args * args);

void set(nv_cmd_args * args);
...
private:
app_cmd_map m_cmd_map;
...


this part is fine, except again for the inheretance from nv_app_cmd, whats the point inheriting from a class that has nothing but a deconstructor?




// sniplet from nv_command.cpp

nv_cmd_mgr::nv_cmd_mgr()
{
...
nv_fn_app_cmd * fn_set = new nv_fn_app_cmd;
fn_set->pfn = (PFN_APP_CMD)&amp;nv_cmd_mgr::set;
fn_set->fn_app_cmd = reinterpret_cast<nv_app_cmd*>(this);
add_cmd("set",fn_set);
...
}

bool nv_cmd_mgr::add_cmd(const char * name, nv_fn_app_cmd * pfn)
{
app_cmd_it it = m_cmd_map.find(name);
if (it == m_cmd_map.end())
{
m_cmd_map.insert(app_cmd_pair(name,pfn));
return true;
}
return false;
}

bool nv_cmd_mgr::send_cmd(const char * str_cmd, nv_cmd_args * args)
{
app_cmd_it it = m_cmd_map.find(str_cmd);
if (it != m_cmd_map.end())
{
nv_fn_app_cmd * pfn = (*it).second;
(pfn->fn_app_cmd->*(pfn->pfn))(args);
return true;
}
return false;
}


This is the most confusing section for me :p




fn_set->fn_app_cmd = reinterpret_cast<nv_app_cmd*>(this);

what does the reinterpret_cast thing do?
and what is the main purpose of line?




nv_fn_app_cmd * pfn = (*it).second;
(pfn->fn_app_cmd->*(pfn->pfn))(args);

Ok, so here we retreive the command, and a pointer to the commands nv_fn_app_cmd struct and we want to call the function associated with the command.
but what the hell is all that mess? maybe an answer to the above questions will help me realise, but at the moment, my head huts just looking at it :p

anyways. any help would be much appreciated!
thanks in advance
--tristan

[This message has been edited by torisutan (edited 09-28-2003).]

davepermen
09-28-2003, 11:50 PM
in very short: learn c++
in less short: they wrote some manager where they can register method-pointers including objects to strings, and all them by name.