Guest User

std::map and custom compare parameter

a guest
Apr 3rd, 2016
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  1.  
  2. void initiateMenu()
  3. {
  4.     std::string selection;
  5.  
  6.     /**
  7.      * Entry data content
  8.      */
  9.     struct entry {
  10.         std::string state;
  11.         std::string description;
  12.         int minAuth;
  13.         int maxAuth;
  14.         void (*fptr)();
  15.         bool active = false;
  16.     };
  17.  
  18.     /**
  19.      * Menu list
  20.      */
  21.     std::map<std::string, entry> entries;
  22.  
  23.     /**
  24.      * add menu entries!
  25.      */
  26.     entries["KR"] = {"/",       "Customer registration",                    GUEST,      ADMIN, registerCustomer};
  27.     entries["KL"] = {"/",       "Customer login",                           GUEST,      GUEST, loginCustomer};
  28.     entries["KU"] = {"/",       "Customer logout",                          CUSTOMER,   ADMIN, logoutCustomer};
  29.     entries["KS"] = {"/",       "Display my items",                         CUSTOMER,   ADMIN, displayCustomerItems};
  30.     entries["KE"] = {"/",       "Update customers authorization access",    ADMIN,      ADMIN, setAuthorityLevel};
  31.  
  32.     /**
  33.      * Start the program loop.
  34.      */
  35.     do {
  36.         bool authenticated = customers->authenticated();
  37.         int authorization  = authenticated ? customers->session()->returnAuth() : GUEST;
  38.  
  39.         /**
  40.          * Display that a customer is logged in
  41.          *  if his / her session is successfully set.
  42.          */
  43.         if (customers->authenticated()) {
  44.             std::cout
  45.                 << "Logged in as: "
  46.                 << customers->session()->username()
  47.                 << std::endl;
  48.         }
  49.  
  50.         /**
  51.          * Print the menu, with command options.
  52.          *
  53.          * ROOT_STATE == /
  54.          */
  55.         std::cout << "------------------ MENU ------------------" << std::endl;
  56.         for (auto & i : entries) {
  57.             i.second.active = false;
  58.  
  59.             std::string state   = i.second.state;
  60.             int minAuth         = i.second.minAuth;
  61.             int maxAuth         = i.second.maxAuth;
  62.  
  63.             /**
  64.              * If the current entry does not follow the rules for being displayed,
  65.              *  we ignore the rest of the round and go to the next entry.
  66.              */
  67.             if (state != "*") {
  68.                 if (authenticated && state != customers->state()) {
  69.                     continue;
  70.                 }
  71.  
  72.                 if (!authenticated && minAuth > GUEST) {
  73.                     continue;
  74.                 }
  75.  
  76.                 if (authenticated && minAuth > authorization) {
  77.                     continue;
  78.                 }
  79.  
  80.                 if (authenticated && maxAuth < authorization) {
  81.                     continue;
  82.                 }
  83.  
  84.                 if (!authenticated && state != ROOT_STATE) {
  85.                     continue;
  86.                 }
  87.             }
  88.  
  89.             /**
  90.              * prints out the menu entry: COMMAND - description
  91.              */
  92.             std::cout
  93.                 << i.first
  94.                 << " - "
  95.                 << i.second.description
  96.                 << std::endl;
  97.  
  98.             /**
  99.              * let the program know which commands were displayed.
  100.              */
  101.             i.second.active = true;
  102.         }
  103.  
  104.         /**
  105.          * Retrieves the selected option as a string by the customer.
  106.          *  The string is then converted to an enum, and
  107.          */
  108.         selection = getInput("Menu choice", 1, 2, true);   // Get command from user. Lengths; min: 1, max: 2. Uppercase.
  109.  
  110.         /**
  111.          * When the returned position isn't set to the end()[index value]
  112.          *  there was a match.
  113.          */
  114.         if (entries.find(selection) != entries.end()) {
  115.             //set user state
  116.             //TODO: add this after function pointer, and add a way for categories to use their ID in stead.
  117.             customers->updateState(selection);
  118.  
  119.             // call function stored in entry
  120.             (*entries.at(selection).fptr)(); //TODO: do i need to add a * in front of entries. ?
  121.         }
  122.  
  123.     } while ( !(selection == "Q" && customers->state() == "/") );
  124. }
Advertisement
Add Comment
Please, Sign In to add comment