File Modes.cpp changed (mode: 100644) (index 660e6d6..34cfeff) |
20 |
20 |
|
|
21 |
21 |
#include <cassert> |
#include <cassert> |
22 |
22 |
|
|
|
23 |
|
#include <stdexcept> |
23 |
24 |
#include <utility> |
#include <utility> |
24 |
25 |
#include <vector> |
#include <vector> |
25 |
26 |
|
|
26 |
27 |
#include "engine/keys.h" |
#include "engine/keys.h" |
27 |
28 |
#include "Mode.hpp" |
#include "Mode.hpp" |
28 |
29 |
|
|
29 |
|
static void keyHandler(key_info_t key_info, keys_info_t *keys_info); |
|
30 |
|
|
|
31 |
30 |
using namespace vle; |
using namespace vle; |
32 |
31 |
|
|
33 |
|
Modes::Modes(std::vector<Mode> modesInfo) : modes(std::move(modesInfo)) |
|
|
32 |
|
// Dispatches handler invocations from the C layer. |
|
33 |
|
static inline void |
|
34 |
|
keyHandler(key_info_t key_info, keys_info_t */*keys_info*/) |
|
35 |
|
{ |
|
36 |
|
auto handler = static_cast<std::function<Shortcut::handler_t> *>( |
|
37 |
|
key_info.context |
|
38 |
|
); |
|
39 |
|
(*handler)(key_info.count); |
|
40 |
|
} |
|
41 |
|
|
|
42 |
|
Modes::Modes() : modeKeyFlags(1) |
|
43 |
|
{ |
|
44 |
|
auto silence = [](int /*more*/) { }; |
|
45 |
|
vle_keys_init(1, &modeKeyFlags[0], silence); |
|
46 |
|
} |
|
47 |
|
|
|
48 |
|
Modes::~Modes() |
|
49 |
|
{ |
|
50 |
|
reset(); |
|
51 |
|
} |
|
52 |
|
|
|
53 |
|
void |
|
54 |
|
Modes::setModes(std::vector<Mode> modesInfo) |
|
55 |
|
{ |
|
56 |
|
if (modesInfo.empty()) { |
|
57 |
|
throw std::invalid_argument("There must be at least one mode"); |
|
58 |
|
} |
|
59 |
|
|
|
60 |
|
reset(); |
|
61 |
|
init(std::move(modesInfo)); |
|
62 |
|
} |
|
63 |
|
|
|
64 |
|
void |
|
65 |
|
Modes::reset() |
|
66 |
|
{ |
|
67 |
|
vle_keys_reset(); |
|
68 |
|
modeKeyFlags.clear(); |
|
69 |
|
modes.clear(); |
|
70 |
|
} |
|
71 |
|
|
|
72 |
|
void |
|
73 |
|
Modes::init(std::vector<Mode> &&modesInfo) |
34 |
74 |
{ |
{ |
|
75 |
|
modes = std::move(modesInfo); |
|
76 |
|
|
35 |
77 |
modeKeyFlags.reserve(modes.size()); |
modeKeyFlags.reserve(modes.size()); |
36 |
78 |
for (const Mode &mode : modes) { |
for (const Mode &mode : modes) { |
37 |
79 |
modeKeyFlags.push_back(mode.usesCount() ? MF_USES_COUNT : 0); |
modeKeyFlags.push_back(mode.usesCount() ? MF_USES_COUNT : 0); |
|
... |
... |
Modes::Modes(std::vector<Mode> modesInfo) : modes(std::move(modesInfo)) |
58 |
100 |
(void)ret_code; |
(void)ret_code; |
59 |
101 |
} |
} |
60 |
102 |
} |
} |
61 |
|
|
|
62 |
|
// Dispatches handler invocations from the C layer. |
|
63 |
|
static void |
|
64 |
|
keyHandler(key_info_t key_info, keys_info_t */*keys_info*/) |
|
65 |
|
{ |
|
66 |
|
auto handler = static_cast<std::function<Shortcut::handler_t> *>( |
|
67 |
|
key_info.context |
|
68 |
|
); |
|
69 |
|
(*handler)(key_info.count); |
|
70 |
|
} |
|
71 |
|
|
|
72 |
|
Modes::~Modes() |
|
73 |
|
{ |
|
74 |
|
vle_keys_reset(); |
|
75 |
|
} |
|
File Modes.hpp changed (mode: 100644) (index 0728ae7..4008b3b) |
... |
... |
class Mode; |
29 |
29 |
class Modes |
class Modes |
30 |
30 |
{ |
{ |
31 |
31 |
public: |
public: |
32 |
|
// Initializes all modes. |
|
33 |
|
explicit Modes(std::vector<Mode> modesInfo); |
|
|
32 |
|
// Initializes no modes. |
|
33 |
|
Modes(); |
34 |
34 |
// Deinitializes all modes. |
// Deinitializes all modes. |
35 |
35 |
~Modes(); |
~Modes(); |
36 |
36 |
|
|
|
... |
... |
public: |
39 |
39 |
Modes & operator=(const Modes &rhs) = delete; |
Modes & operator=(const Modes &rhs) = delete; |
40 |
40 |
Modes & operator=(Modes &&rhs) = delete; |
Modes & operator=(Modes &&rhs) = delete; |
41 |
41 |
|
|
|
42 |
|
public: |
|
43 |
|
// Initializes modes. Throws `std::invalid_argument` on duplicated mode ids |
|
44 |
|
// or empty list of modes. |
|
45 |
|
void setModes(std::vector<Mode> modesInfo); |
|
46 |
|
|
|
47 |
|
private: |
|
48 |
|
// Deinitializes all modes. |
|
49 |
|
void reset(); |
|
50 |
|
// Initializes modes. Throws `std::invalid_argument` on duplicated mode |
|
51 |
|
// ids. |
|
52 |
|
void init(std::vector<Mode> &&modesInfo); |
|
53 |
|
|
42 |
54 |
private: |
private: |
43 |
55 |
std::vector<Mode> modes; // Data that describes modes. |
std::vector<Mode> modes; // Data that describes modes. |
44 |
56 |
std::vector<int> modeKeyFlags; // Key processing flags of each mode. |
std::vector<int> modeKeyFlags; // Key processing flags of each mode. |