teditor  1.8.0@@fee5e94
Terminal based editor written in C++
key_cmd_map.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "trie.h"
4 
5 namespace teditor {
6 
7 enum TrieStatus {
11 };
12 
13 
14 struct KeyCmdPair {
15  std::string keySeq, cmd;
16 };
17 
18 
19 class KeyCmdMap {
20 public:
21  KeyCmdMap(): key2cmd(), cmd2key(), keyTree(), currNode(nullptr) {}
22  void add(char key, const std::string& cmd);
23  void add(const std::string& keySeq, const std::string& cmd);
24  void add(const KeyCmdPair& pair) { add(pair.keySeq, pair.cmd); }
25  TrieStatus traverse(const std::string& currKey);
26  void resetTraversal() { currNode = keyTree.getRoot(); }
27  const std::string getCmd() const;
28  void clear();
29  void eraseKey(const std::string& key);
30 
31 private:
32  std::unordered_map<std::string, std::string> key2cmd, cmd2key;
33  Trie keyTree;
34  Node* currNode;
35 };
36 
37 
38 template <typename Keys>
39 void populateKeyMap(KeyCmdMap& kcm, bool clear=false) {
40  if(clear) kcm.clear();
41  for(auto& kc : Keys::All) {
42  if (kc.cmd.empty()) kcm.eraseKey(kc.keySeq);
43  else kcm.add(kc);
44  }
45 }
46 
47 }; // end namespace teditor
teditor::TrieStatus
TrieStatus
Definition: key_cmd_map.h:7
teditor::KeyCmdMap::eraseKey
void eraseKey(const std::string &key)
Definition: key_cmd_map.cpp:31
teditor::Trie::getRoot
Node * getRoot()
Definition: trie.h:35
teditor::KeyCmdPair::keySeq
std::string keySeq
Definition: key_cmd_map.h:15
teditor::KeyCmdMap::traverse
TrieStatus traverse(const std::string &currKey)
Definition: key_cmd_map.cpp:18
teditor::Node::data
const std::string & data()
Definition: trie.h:19
key_cmd_map.h
teditor::KeyCmdMap::KeyCmdMap
KeyCmdMap()
Definition: key_cmd_map.h:21
teditor::KeyCmdMap
Definition: key_cmd_map.h:19
teditor::KeyCmdMap::clear
void clear()
Definition: key_cmd_map.cpp:44
teditor::KeyCmdMap::resetTraversal
void resetTraversal()
Definition: key_cmd_map.h:26
utils.h
teditor::TS_NULL
@ TS_NULL
Definition: key_cmd_map.h:8
teditor::Trie
Definition: trie.h:29
teditor::KeyCmdMap::getCmd
const std::string getCmd() const
Definition: key_cmd_map.cpp:26
teditor::Trie::add
void add(const std::string &keys, const std::string &str)
Definition: trie.cpp:59
teditor::TS_NON_LEAF
@ TS_NON_LEAF
Definition: key_cmd_map.h:9
teditor::Node::getNode
Node * getNode(const std::string &key)
Definition: trie.cpp:24
teditor::KeyCmdMap::add
void add(char key, const std::string &cmd)
Definition: key_cmd_map.cpp:6
teditor::populateKeyMap
void populateKeyMap(KeyCmdMap &kcm, bool clear=false)
Definition: key_cmd_map.h:39
teditor::Node
Definition: trie.h:10
teditor::Trie::clear
void clear()
Definition: trie.h:36
teditor::KeyCmdMap::add
void add(const KeyCmdPair &pair)
Definition: key_cmd_map.h:24
teditor::KeyCmdPair
Definition: key_cmd_map.h:14
teditor::TS_LEAF
@ TS_LEAF
Definition: key_cmd_map.h:10
teditor::KeyCmdPair::cmd
std::string cmd
Definition: key_cmd_map.h:15
teditor
Definition: any.hpp:10
trie.h
teditor::Trie::del
void del(const std::string &keys)
Definition: trie.cpp:73
teditor::Node::leaf
bool leaf() const
Definition: trie.h:18