WvStreams
wvstreamsdebugger.h
00001 /* -*- Mode: C++ -*- */
00002 #ifndef WVSTREAMSDEBUGGER_H
00003 #define WVSTREAMSDEBUGGER_H
00004 
00005 #include <map>
00006 
00007 #include "wverror.h"
00008 #include "wvstringlist.h"
00009 #include "wvtclstring.h"
00010 #include "wvtr1.h"
00011 
00012 class WvStreamsDebugger
00013 {
00014 public:
00015 
00016     // The callback type used to pass the results back to the application
00017     // that calls WvStreamsDebugger::run.  The application is allowed
00018     // to consume the WvStringList of results.
00019     typedef wv::function<void(WvStringParm, WvStringList&)> ResultCallback;
00020 
00021     // Debugging commands are implemented through the following three
00022     // callbacks:
00023     //   - InitCallback is optional and is used to allocate state
00024     //     for an instance of WvStreamsDebugger for the given command
00025     //   - RunCallback is required and is used to actually execute
00026     //     the command as a result of a call to WvStreamsDebugger::run
00027     //   - CleanupCallback is optional and is used to free state
00028     //     for an instance of WvStreamsDebugger for the given command
00029     typedef wv::function<void*(WvStringParm)> InitCallback;
00030     typedef wv::function<WvString(WvStringParm, WvStringList&,
00031                                   ResultCallback, void*)> RunCallback;
00032 
00033     typedef wv::function<void(WvStringParm, void*)> CleanupCallback;
00034 
00035     // The WvStreamsDebugger::foreach function can be used to update
00036     // state in every instance of WvStreamsDebugger for a given command.
00037     typedef wv::function<void(WvStringParm, void*)> ForeachCallback;
00038     
00039 private:
00040 
00041     struct Command
00042     {
00043         InitCallback init_cb;
00044         RunCallback run_cb;
00045         CleanupCallback cleanup_cb;
00046         
00047         Command(InitCallback _init_cb, RunCallback _run_cb,
00048                 CleanupCallback _cleanup_cb)
00049         {
00050             init_cb = _init_cb;
00051             run_cb = _run_cb;
00052             cleanup_cb = _cleanup_cb;
00053         }
00054     };
00055     typedef std::map<WvString, Command> CommandMap;
00056     static CommandMap *commands;
00057     typedef std::map<WvString, void*> CommandDataMap;
00058     CommandDataMap command_data;
00059     
00060     void *get_command_data(WvStringParm cmd, Command *command);
00061     friend class WvStreamsDebuggerStaticInitCleanup;
00062     
00063 public:
00064 
00065     WvStreamsDebugger();
00066     ~WvStreamsDebugger();
00067 
00068     WvString run(WvStringParm cmd, WvStringList &args,
00069             ResultCallback result_cb);
00070     
00071     static bool add_command(WvStringParm cmd,
00072             InitCallback init_cb,
00073             RunCallback run_cb,
00074             CleanupCallback cleanup_cb);
00075             
00076     static bool foreach(WvStringParm cmd, ForeachCallback foreach_cb);
00077 
00078 private:
00079 
00080     static WvString help_run_cb(WvStringParm cmd,
00081             WvStringList &args,
00082             ResultCallback result_cb, void *);
00083 };
00084 
00085 #endif