rtop
key_dict.h
Go to the documentation of this file.
1 #include <map>
2 #include <vector>
3 #include <string>
4 
5 #ifndef KEY_H_
6 #define KEY_H_
7 #include "rtop_logger.h"
8 #include "mem_func.h"
9 #include "screen2.h"
10 #include "view2.h"
11 #include "panel2.h"
12 
13 extern src::severity_logger<severity_level> lg;
14 extern logSpacer log_spacer;
15 
16 namespace rtop
17 {
18  class ScreenManager;
19  class View;
21 
22  extern MemFuncPDict funcPDict;
23 
25  class KeyDict
26  {
27  private:
29  std::map<int, std::vector<Action>> key_actions_dict;
30  public:
31  KeyDict(int id): key_dict_uuid{id}{}
33  void setId(int id){key_dict_uuid = id;}
34  int getId() const {return key_dict_uuid;}
35  void resolve(int);
36  void resolve(int, ScreenManager*);
37  void resolve(int, View*);
38  void resolve(int, SimplePanelData*);
39  void addKeyActions(int, std::vector<Action>&);
40  };
41 
43  void KeyDict::resolve(int key)
44  {
46  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> KeyDict-"<<key_dict_uuid<<"_resolve::";
47  if (key_actions_dict.find(key) != key_actions_dict.end()) // if key is found in dictionary
48  {
49  for(auto action: key_actions_dict[key]) // iterate over action list
50  funcPDict.invoke(action); // pass action to callback database (MemFuncPDict) for invocation
51  }
52  else // if key not found in dictionary, invoke action list corresponding to key = -1
53  {
54  for(auto action: key_actions_dict[-1])
55  funcPDict.invoke(action);
56  }
57  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- KeyDict-"<<key_dict_uuid<<"_resolve::";
59  }
60 
62  void KeyDict::resolve(int key, ScreenManager* pscreen)
63  {
65  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> KeyDict-"<<key_dict_uuid<<"_resolve-screen::";
66  if (key_actions_dict.find(key) != key_actions_dict.end())
67  {
68  for(auto action: key_actions_dict[key]) // if key is found in dictionary
69  {
70  if(action.object_uuid == -1) // if object id is -1, invoke method for passed ScreenManager object pointer
71  funcPDict.invoke(action, pscreen);
72  else
73  funcPDict.invoke(action); // if object id is >0, invoke methods for by passing action info to callback database
74  }
75  }
76  else // if key is not found in dictionary, invoke action list corresponding to key=-1
77  {
78  for(auto action: key_actions_dict[-1])
79  funcPDict.invoke(action);
80  }
81  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- KeyDict-"<<key_dict_uuid<<"_resolve-screen::";
83  }
84 
86  void KeyDict::resolve(int key, View* pview)
87  {
89  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> KeyDict-"<<key_dict_uuid<<"_resolve-view::";
90  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- KeyDict-"<<key_dict_uuid<<"_resolve-view::";
92  }
93 
95  void KeyDict::resolve(int key, SimplePanelData* ppanel)
96  {
98  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> KeyDict-"<<key_dict_uuid<<"_resolve-panel::";
99 
100  if (key_actions_dict.find(key) != key_actions_dict.end())
101  {
102  for(auto action: key_actions_dict[key]) // if key is found in dictionary
103  {
104  if(action.object_uuid == -1) // if object id is -1, invoke method for passed panel data structure object pointer
105  funcPDict.invoke(action, ppanel);
106  else
107  funcPDict.invoke(action); // if object id is >0, invoke methods for by passing action info to callback database
108  }
109  }
110  else // if key is not found in dictionary, invoke action list corresponding to key=-1
111 
112  {
113  for(auto action: key_actions_dict[-1])
114  funcPDict.invoke(action);
115  }
116  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- KeyDict-"<<key_dict_uuid<<"_resolve-panel::";
118  }
119 
121  void KeyDict::addKeyActions(int key, std::vector<Action>& actions)
122  {
124  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> KeyDict-"<<key_dict_uuid<<"_addKeyActions::";
125  if (key_actions_dict.find(key) != key_actions_dict.end()) // if key is already present, reset its action list
126  key_actions_dict[key].clear();
127  key_actions_dict[key] = actions;
128  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- KeyDict-"<<key_dict_uuid<<"_addKeyActions::";
130  }
131 
132 
135  {
136  private:
137  std::map<int, KeyDict> map_of_keydict;
138  public:
140  void addKeyDict(int, KeyDict);
141  KeyDict getKeyDict(int);
142  };
143 
145  void MapOfKeyDicts::addKeyDict(int key_uuid, KeyDict key_dict)
146  {
147  log_spacer.addSpace();
148  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> MapOfKeyDicts_addKeyDict::";
149  map_of_keydict[key_uuid] = key_dict;
150  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- MapOfKeyDicts_addKeyDict::";
152  }
153 
155  {
157  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> MapOfKeyDicts_getKeyDict::";
158  if (map_of_keydict.find(key_uuid) != map_of_keydict.end()) // if key_dict_uuid found, return the corresponding entry
159  {
160  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- MapOfKeyDicts_getKeyDict::";
162  return map_of_keydict[key_uuid];
163  }
164  else // if key_dict_uuid not found, return NULL
165  {
166  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- MapOfKeyDicts_getKeyDict::";
168  return NULL;
169  }
170  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- MapOfKeyDicts_getKeyDict::";
172  }
173 
176 
177 } // namespace rtop
178 #endif
179 
180 
181 
src::severity_logger< severity_level > lg
Definition: rtop_logger.h:108
logSpacer log_spacer
Definition: rtop_logger.h:186
MapOfKeyDicts mapKeyDict
global variable for accessing key dictionaries by their uuid
Definition: key_dict.h:175
std::map< int, std::vector< Action > > key_actions_dict
Definition: key_dict.h:29
stores references to UI and non-UI objects and their callbacks.
Definition: mem_func.h:31
std::map< int, KeyDict > map_of_keydict
Definition: key_dict.h:137
int key_dict_uuid
Definition: key_dict.h:28
MemFuncPDict funcPDict
Definition: key_dict.h:20
2nd level UI object. Responsible for switching between panels and resolving key input
Definition: view2.h:30
void delSpace()
deletes one white-space from white-space string corresponding to invoking thread
Definition: rtop_logger.h:151
KeyDict(int id)
Definition: key_dict.h:31
void addSpace()
adds one white-space to white-space string corresponding to invoking thread
Definition: rtop_logger.h:135
stores key dictionary index by key_dict_uuid
Definition: key_dict.h:134
void resolve(int)
resolves the key i.e. based on key input invokes the associated list of Action objects
Definition: key_dict.h:43
Top Level UI object. Responsible to switching between views and capturing key input.
Definition: screen2.h:34
holds a dictionary of key values and corresponding Action list. Provides API to resolve the keys
Definition: key_dict.h:25
KeyDict getKeyDict(int)
Definition: key_dict.h:154
enables indentation of logs for easy viewing
Definition: rtop_logger.h:118
Base class in heirarchy of UI objects responsible to storing the content associated with a panel and ...
Definition: panel2.h:34
void addKeyDict(int, KeyDict)
adds key dictionary corresponding to the supplied key_dict_uuid
Definition: key_dict.h:145
void invoke(Action)
invokes member method based on supplied action using object_uuid and method_name
Definition: mem_func.h:325
void addKeyActions(int, std::vector< Action > &)
add key and corresponding action list to private key-action_list dictionary
Definition: key_dict.h:121
Definition: action.h:7
void setId(int id)
Definition: key_dict.h:33
int getId() const
Definition: key_dict.h:34