1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include "wvstreamsdebugger.h"
#include "wvlinklist.h"
static const int command_map_size = 16;
DeclareWvList(WvStreamsDebugger);
static WvStreamsDebuggerList *debuggers;
WvStreamsDebugger::CommandMap *WvStreamsDebugger::commands;
class WvStreamsDebuggerStaticInitCleanup
{
public:
WvStreamsDebuggerStaticInitCleanup()
{
WvStreamsDebugger::add_command("help",
0, &WvStreamsDebugger::help_run_cb, 0);
}
~WvStreamsDebuggerStaticInitCleanup()
{
assert(!debuggers || debuggers->isempty());
if (WvStreamsDebugger::commands)
{
WvStreamsDebugger::CommandMap::Iter i(*WvStreamsDebugger::commands);
for (i.rewind(); i.next(); )
delete i->data;
WvStreamsDebugger::commands->zap();
delete WvStreamsDebugger::commands;
WvStreamsDebugger::commands = NULL;
}
if (debuggers)
{
delete debuggers;
debuggers = NULL;
}
}
};
static WvStreamsDebuggerStaticInitCleanup ___;
void *WvStreamsDebugger::get_command_data(WvStringParm cmd, Command *command)
{
if (command == NULL)
{
Command **pcommand = commands->find(cmd);
if (!pcommand)
return NULL;
command = *pcommand;
}
void **pcd = (void **)command_data.find(cmd);
void *cd;
if (pcd == NULL)
{
// In case the command has been added since our constructor
// was executed...
if (!!command->init_cb)
cd = command->init_cb(cmd);
else
cd = NULL;
command_data.add(cmd, (char *)cd);
}
else
cd = *pcd;
return cd;
}
WvStreamsDebugger::WvStreamsDebugger() :
command_data(command_map_size)
{
if (!debuggers)
debuggers = new WvStreamsDebuggerList;
debuggers->append(this, false);
// Add command data for existing commands
CommandMap::Iter i(*commands);
for (i.rewind(); i.next(); )
(void)get_command_data(i->key, i->data);
}
WvStreamsDebugger::~WvStreamsDebugger()
{
// Remove command data
CommandDataMap::Iter i(command_data);
for (i.rewind(); i.next(); )
{
WvString cmd = i->key;
void *cd = i->data;
Command *command = (*commands)[cmd];
if (!!command->cleanup_cb)
command->cleanup_cb(cmd, cd);
}
command_data.zap();
debuggers->unlink(this);
}
WvString WvStreamsDebugger::run(WvStringParm cmd, WvStringList &args,
ResultCallback result_cb)
{
Command **pcommand = commands->find(cmd);
if (!pcommand)
return "No such command";
Command *command = *pcommand;
return command->run_cb(cmd, args, result_cb, get_command_data(cmd, command));
}
bool WvStreamsDebugger::add_command(WvStringParm cmd,
InitCallback init_cb,
RunCallback run_cb,
CleanupCallback cleanup_cb)
{
if (!commands)
commands = new CommandMap(command_map_size);
if (commands->exists(cmd))
return false;
Command *command = new Command(init_cb, run_cb, cleanup_cb);
commands->add(cmd, command);
return true;
}
bool WvStreamsDebugger::foreach(WvStringParm cmd, ForeachCallback foreach_cb)
{
Command **pcommand = commands->find(cmd);
if (!pcommand)
return false;
Command *command = *pcommand;
if (debuggers)
{
WvStreamsDebuggerList::Iter i(*debuggers);
for (i.rewind(); i.next(); )
{
void *cd = i->get_command_data(cmd, command);
foreach_cb(cmd, cd);
}
}
return true;
}
WvString WvStreamsDebugger::help_run_cb(WvStringParm cmd,
WvStringList &args,
ResultCallback result_cb, void *)
{
WvStringList cmd_list;
cmd_list.append("Commands availible:");
CommandMap::Iter i(*commands);
for (i.rewind(); i.next(); )
cmd_list.append(i->key);
result_cb(cmd, cmd_list);
return WvString::null;
}
|