File: action_router.hpp

package info (click to toggle)
polybar 3.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,108 kB
  • sloc: cpp: 30,424; python: 3,750; sh: 284; makefile: 83
file content (60 lines) | stat: -rw-r--r-- 1,533 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

#include <cassert>
#include <stdexcept>
#include <unordered_map>

#include "common.hpp"

POLYBAR_NS

/**
 * Maps action names to lambdas and invokes them.
 *
 * Each module has one instance of this class and uses it to register action.
 * For each action the module has to register the name, whether it can take
 * additional data, and a callback that is called whenever the action is triggered.
 *
 * The input() function in the base class uses this for invoking the actions
 * of that module.
 *
 * Any module that does not reimplement that function will automatically use
 * this class for action routing.
 */
class action_router {
  using callback = std::function<void(void)>;
  using callback_data = std::function<void(const std::string&)>;

 public:
  void register_action(const string& name, callback func);
  void register_action_with_data(const string& name, callback_data func);
  bool has_action(const string& name);
  void invoke(const string& name, const string& data);

 protected:
  struct entry {
    union {
      callback without;
      callback_data with;
    };
    bool with_data;

    entry(callback func);
    entry(callback_data func);
    ~entry();
  };

  template <typename F>
  void register_entry(const string& name, const F& e) {
    if (has_action(name)) {
      throw std::invalid_argument("Tried to register action '" + name + "' twice. THIS IS A BUG!");
    }

    callbacks.emplace(name, std::move(e));
  }

 private:
  std::unordered_map<string, entry> callbacks;
};

POLYBAR_NS_END