Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

/home/r0/dav/atlas.dir/atlas3/sources/interface/commands.h

Go to the documentation of this file.
00001 /*
00002   This is commands.h
00003 
00004   Copyright (C) 2004,2005 Fokko du Cloux
00005   part of the Atlas of Reductive Lie Groups
00006 
00007   See file main.cpp for full copyright notice
00008 */
00009 
00010 #ifndef COMMANDS_H  /* guard against multiple inclusions */
00011 #define COMMANDS_H
00012 
00013 #include "commands_fwd.h"
00014 
00015 #include <map>
00016 #include <set>
00017 #include <vector>
00018 
00019 #include "input.h"
00020 
00021 /******** typedefs **********************************************************/
00022 
00023 namespace atlas {
00024 
00025 namespace commands {
00026 
00027   enum CheckResult { Ambiguous, Found, NotFound, numCheckResults };
00028 
00029 }
00030 
00031 /******** function declarations ********************************************/
00032 
00033 namespace commands {
00034 
00035   void activate(const CommandMode&);
00036   void addCommands(CommandMode&, const CommandMode&);
00037   CheckResult checkName(const CommandMode&, const char*);
00038   input::InputBuffer& currentLine();
00039   const CommandMode* currentMode();
00040   void defaultError(const char*);
00041   void exitInteractive();
00042   void exitMode();
00043   void insertTag(TagDict&, const char*, const char*);
00044   void printTags(std::ostream&, const TagDict&);
00045   inline void relax_f() {}
00046   void run(const CommandMode&);
00047 
00048 }
00049 
00050 /******* Type definitions **************************************************/
00051 
00052 namespace commands {
00053 
00054 class StrCmp { // a function object for string comparison
00055  public:
00056   bool operator() (const char* a, const char* b) const {
00057     return strcmp(a,b) < 0;
00058   }
00059 };
00060 
00061 struct Command {
00062   void (*action)();
00063 // Constructors and destructors
00064   Command(void (*a)()):action(a) {
00065   };
00066   ~Command() {
00067   };
00068 // accessors
00069   void operator() () const {
00070     action();
00071   }
00072 };
00073 
00074 class CommandMode {
00075 
00076  private:
00077 
00078   typedef std::map<const char*,Command,StrCmp> CommandDict;
00079 
00080   std::vector<const CommandMode*> d_nextList; // direct successor modes
00081 
00082   CommandDict d_map;
00083   const char* d_prompt;
00084   void (*d_entry)();
00085   void (*d_exit)();
00086   void (*d_error)(const char*);
00087 
00088  public:
00089 
00090   typedef CommandDict::iterator iterator;
00091   typedef CommandDict::const_iterator const_iterator;
00092 
00093 // Constructors and destructors
00094   CommandMode(const char*,
00095               void (*entry)() = &relax_f,
00096               void (*exit)() = &relax_f,
00097               void (*error)(const char*) = &defaultError);
00098 
00099   virtual ~CommandMode() {}
00100 
00101 // accessors
00102   const_iterator begin() const {
00103     return d_map.begin();
00104   }
00105 
00106   const_iterator end() const {
00107     return d_map.end();
00108   }
00109 
00110   bool empty() const { return d_map.empty(); }
00111 
00112   const_iterator find(const char* name) const {
00113     return d_map.find(name);
00114   }
00115 
00116   const char* prompt() const {
00117     return d_prompt;
00118   }
00119 
00120   void entry() const {
00121     d_entry();
00122   }
00123 
00124   void error(const char* str) const {
00125     d_error(str);
00126   }
00127 
00128   void exit() const {
00129     d_exit();
00130   }
00131 
00132   void extensions(std::set<const char*,StrCmp>&, const char*) const;
00133 
00134   void extensions(std::vector<const char*>&, const char*) const;
00135 
00136   const_iterator findName(const char* name) const;
00137 
00138   virtual const std::vector<const CommandMode*>& next() const {
00139     return d_nextList;
00140   }
00141 
00142   size_t n_desc() const { return d_nextList.size(); }
00143 
00144   const CommandMode& nextMode(size_t j) const {
00145     return *(d_nextList[j]);
00146   }
00147 
00148 // manipulators
00149   void add(const char* const name, void (*action)()) {
00150     add(name,Command(action));
00151   }
00152 
00153   void add(const char* const, const Command&);
00154 
00155   void add_descendant(CommandMode& c) // make |c| a descendant of |*this|
00156   { d_nextList.push_back(&c); }
00157 
00158   iterator find(const char* name) {
00159     return d_map.find(name);
00160   }
00161 
00162   void setAction(const char*, void (*)());
00163 
00164 }; // class CommandMode
00165 
00166 }
00167 
00168 }
00169 
00170 #endif

Generated on Wed Mar 26 16:49:34 2008 for atlas by  doxygen 1.3.9.1