atlas  0.6
commands.h
Go to the documentation of this file.
1 /*
2  This is commands.h
3 
4  Copyright (C) 2004,2005 Fokko du Cloux
5  part of the Atlas of Lie Groups and Representations
6 
7  For copyright and license information see the LICENSE file
8 */
9 
10 #ifndef COMMANDS_H /* guard against multiple inclusions */
11 #define COMMANDS_H
12 
13 #include "commands_fwd.h"
14 
15 #include <map>
16 #include <set>
17 #include <vector>
18 #include <cstring>
19 
20 #include "input.h"
21 
22 /******** typedefs **********************************************************/
23 
24 namespace atlas {
25 
26 namespace commands {
27 
28  typedef void (*action_pointer)(); // pointer to void function, no arguments
29 
31 
32 }
33 
34 /******** function and variable declarations *******************************/
35 
36 namespace commands {
37 
39  const CommandTree* currentMode();
40 
41  extern TagDict tagDict; // used for helpmode
42 
43  void run_from(const CommandTree& initial_mode);
44  void drop_to(const CommandTree& mode); // exit modes until |mode| is left
45  void exitInteractive(); // used by 'qq' in empty mode
46  void exitMode(); // used by 'q' in help mode
47  inline void relax_f() {}
48 
49  void nohelp_h(); // this is used when no help command at all is defined
50  void std_help(); // searches help file for the curent command name
51  void use_tag(); // fallback when no help file exists
52 
53 }
54 
55 /******* Type definitions **************************************************/
56 
57 namespace commands {
58 
59 struct StrCmp // the string "less than" function disguised as function object
60 { // a zero-size class with just one accessor method
61  bool operator() (const char* a, const char* b) const
62  { return strcmp(a,b) < 0; }
63 };
64 
65 struct Command // a class wrapping a function pointer into a function object
66 {
67  action_pointer action; // one data field: a function pointer
68 // constructor
69 explicit Command(action_pointer a) : action(a) {}; // store |a| as |action|
70 // accessor
71  void operator() () const { action(); }
72 };
73 
74 /*
75  |CommandNode| instances are ephemerous objects that contain the attributes
76  specific to a command mode; they will be passed to the constructor of the
77  more long-lived |CommandTree| derived class, of which it (the copy) becomes
78  a base object. The contruction of the ephemerous object is done by a
79  non-member function, which repeatedly uses the public |add| method to
80  populate the node with commands. We have chosen for the |CommandTree|
81  objects for the modes to be stored in static rather than automatic
82  variables, as this facilitates access by the various (action) functions that
83  need to refer to explicitly names modes. This implies however that the
84  lifetime of |CommandNode| instances is at static initialisation time, before
85  the main program starts; this creates a potential for static initialisation
86  fiasco. Therefore no calls of |CommandNode| methods should rely on the prior
87  initialisation of static variables, except if these are defined in the same
88  compilation unit as the |CommandTree| objects for the modes are (which is
89  this unit, commands.cpp). Fortunately the arguments to the |add| method, a
90  |C|-style string and a function pointer, can be given by constants that need
91  no static initialisation.
92  */
94 {
95  typedef std::map<const char*,Command,StrCmp> CommandDict;
96 
97  CommandDict d_map;
98  const char* d_prompt;
100 
101  public:
102 
103 // Constructors and destructors
104  CommandNode(const char*,
105  void (*entry)() = &relax_f,
106  void (*exit)() = &relax_f);
107 
109 
110 // accessors
111  const char* prompt() const { return d_prompt; }
112 
113  void addCommands(const CommandNode& source); // inherit commands from |source|
114 
115  // bind |name| to |f| in current mode, possibly overriding previous
116  // also provide a descriptive tag and possibly a help mode action
117  void add(const char* const name, action_pointer f,
118  const char* const tag, action_pointer help_f = nohelp_h);
119 
120  void nohelp_add(const char* const name, action_pointer f)
121  { add(name,Command(f)); } // used to explicitly avoid creating help action
122 
123 
124  void exit() const { d_exit(); }
125 
126 
127  protected: // methods for use by |CommandNode| or |CommandTree| objects only
128 
129  typedef CommandDict::const_iterator const_iterator;
130 
131  // bind |name| to |action| in current mode, possibly overriding previous
132  void add(const char* const name, const Command& action);
133 
134  // basic search in our own command map
135  const_iterator find(const char* name) const { return d_map.find(name); }
136 
137  const_iterator find_prefix(const char* name) const
138  { return d_map.lower_bound(name); }
139 
140  void entry() const { d_entry(); }
141 
142  const_iterator begin() const { return d_map.begin(); }
143 
144  const_iterator end() const { return d_map.end(); }
145 
146 }; // |class CommandNode|
147 
148 class CommandTree : public CommandNode
149 {
150  // list direct descendant modes
151  std::vector<CommandTree*> d_nextList; // owned pointers
152 
153  public:
154  explicit CommandTree(const CommandNode& root)
155  : CommandNode(root) , d_nextList() {}
156  ~CommandTree(); // since we have owned pointers
157 
158  // extend mode tree, returning reference to added (singleton) subtree
159  CommandTree& add_descendant(const CommandNode& c); // make |c| a descendant
160 
161  void run() const; // start a command session based on this mode
162 
163  void activate() const; // enter (and push) this mode tree
164 
165  // the following is for use in guiding readline, and for ambiguity reporting
166  std::vector<const char*> extensions(const char*) const;
167 
168  private:
169 
170  size_t n_desc() const { return d_nextList.size(); }
171 
172  const CommandTree& nextMode(unsigned int i) const { return *(d_nextList[i]); }
173 
174  bool has_descendant (const CommandTree* mode) const; // search decendants tree
175 
176  // look up |name|, return result in |status|, which is an in-out argument.
177  // |status| should be anything except |Found| initially, and the result
178  // and |where| are set only when it becomes |Found| or |PartialMatch|
179  CommandNode::const_iterator look_up(const char* name,
181  CommandTree const* & where) const;
182 
183  // implementation of the public |extensions| method, eliminates duplicates
184  void extensions(std::set<const char*,StrCmp>&, const char*) const;
185 
186 }; // |class CommandTree|
187 
188 } // |namespace commands|
189 
190 } // |namespace atlas|
191 
192 #endif
mod_pointer root
Definition: common.c:127
void nohelp_h()
Definition: commands.cpp:495
Definition: commands.h:30
CommandDict::const_iterator const_iterator
Definition: commands.h:129
std::map< const char *, const char *, StrCmp > TagDict
Definition: commands_fwd.h:24
Definition: commands.h:30
Command(action_pointer a)
Definition: commands.h:69
const char * prompt() const
Definition: commands.h:111
TagDict tagDict
Definition: commands.cpp:96
mode
Definition: cweave.c:359
const CommandTree & nextMode(unsigned int i) const
Definition: commands.h:172
Definition: commands.h:148
const_iterator end() const
Definition: commands.h:144
const_iterator find_prefix(const char *name) const
Definition: commands.h:137
Definition: input.h:49
std::vector< CommandTree * > d_nextList
Definition: commands.h:151
Definition: commands.h:65
size_t n_desc() const
Definition: commands.h:170
action_pointer action
Definition: commands.h:67
~CommandNode()
Definition: commands.h:108
void use_tag()
Definition: commands.cpp:507
Definition: commands.h:59
gradings::Status::Value status(const KGB_base &kgb, KGBElt x, RootNbr alpha)
Definition: kgb.cpp:850
const_iterator find(const char *name) const
Definition: commands.h:135
void relax_f()
Definition: commands.h:47
void exitMode()
Definition: commands.cpp:455
std::map< const char *, Command, StrCmp > CommandDict
Definition: commands.h:95
void entry() const
Definition: commands.h:140
const_iterator begin() const
Definition: commands.h:142
action_pointer d_exit
Definition: commands.h:99
void nohelp_add(const char *const name, action_pointer f)
Definition: commands.h:120
Definition: commands.h:30
void exitInteractive()
Definition: commands.cpp:486
void(* action_pointer)()
Definition: commands.h:28
CommandDict d_map
Definition: commands.h:97
char * name
Definition: common.c:103
const CommandTree * currentMode()
Definition: commands.cpp:446
Definition: Atlas.h:38
Definition: commands.h:93
input::InputBuffer & currentLine()
Definition: commands.cpp:436
CheckResult
Definition: commands.h:30
const char * d_prompt
Definition: commands.h:98
void run_from(const CommandTree &initial_mode)
void drop_to(const CommandTree &mode)
Definition: commands.cpp:471
Definition: common.h:36
void exit() const
Definition: commands.h:124
void std_help()
Definition: commands.cpp:500
Definition: commands.h:30
CommandTree(const CommandNode &root)
Definition: commands.h:154