File: wvstreamsdebugger.cc

package info (click to toggle)
wvstreams 4.6.1-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,420 kB
  • sloc: cpp: 64,196; ansic: 4,154; sh: 4,025; makefile: 546; perl: 402
file content (159 lines) | stat: -rw-r--r-- 3,748 bytes parent folder | download | duplicates (8)
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
#include <set>
#include "wvstreamsdebugger.h"
#include "wvlinklist.h"

using std::set;


static set<WvStreamsDebugger*> *debuggers;


WvStreamsDebugger::CommandMap *WvStreamsDebugger::commands;


class WvStreamsDebuggerStaticInitCleanup
{
public:
    WvStreamsDebuggerStaticInitCleanup()
    {
        WvStreamsDebugger::add_command("help",
                0, &WvStreamsDebugger::help_run_cb, 0);
    }
    ~WvStreamsDebuggerStaticInitCleanup()
    {
        assert(!debuggers || debuggers->empty());
        
        if (WvStreamsDebugger::commands)
        {
            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)
    {
	CommandMap::iterator it = commands->find(cmd);
	if (it == commands->end())
	    return NULL;
	command = &it->second;
    }

    void *cd;
    CommandDataMap::iterator it = command_data.find(cmd);
    if (it == command_data.end())
    {
        // 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[cmd] = cd;
    }
    else
	cd = it->second;

    return cd;
}


WvStreamsDebugger::WvStreamsDebugger()
{
    if (!debuggers)
        debuggers = new set<WvStreamsDebugger*>;
    debuggers->insert(this);
    
    // Add command data for existing commands
    CommandMap::iterator it;
    for (it = commands->begin(); it != commands->end(); ++it)
        get_command_data(it->first, &it->second);
}


WvStreamsDebugger::~WvStreamsDebugger()
{
    // Remove command data
    CommandDataMap::iterator it;
    for (it = command_data.begin(); it != command_data.end(); ++it)
    {
	CommandMap::iterator it2 = commands->find(it->first);
	if (it2 != commands->end() && it2->second.cleanup_cb)
            it2->second.cleanup_cb(it->first, it->second);
    }
    command_data.clear();
    
    debuggers->erase(this);
}


WvString WvStreamsDebugger::run(WvStringParm cmd, WvStringList &args,
				ResultCallback result_cb)
{
    CommandMap::iterator it = commands->find(cmd);
    if (it == commands->end())
        return "No such command";
    Command *command = &it->second;
   
    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;

    return commands->insert(
	std::make_pair(cmd, Command(init_cb, run_cb, cleanup_cb))).second;
}


bool WvStreamsDebugger::foreach(WvStringParm cmd, ForeachCallback foreach_cb)
{
    CommandMap::iterator it = commands->find(cmd);

    if (it == commands->end())
        return false;
    
    if (debuggers)
    {
	set<WvStreamsDebugger*>::iterator it2;
        for (it2 = debuggers->begin(); it2 != debuggers->end(); ++it2)
        {
            void *cd = (*it2)->get_command_data(cmd, &it->second);
            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 available:");
    CommandMap::iterator it;
    for (it = commands->begin(); it != commands->end(); ++it)
	cmd_list.append(it->first);
    result_cb(cmd, cmd_list);
    return WvString::null;
}