xaizek / libvle (License: GPLv3+) (since 2019-04-21)
Library for building Vim-like applications.
Commit e0626da2657b18fd7ef3b38b4a7e70113a1993d4

Throw on adding a duplicate shortcut
Author: xaizek
Author date (UTC): 2019-04-21 14:17
Committer name: xaizek
Committer date (UTC): 2019-04-21 14:17
Parent(s): 329e45fc6a8e92f7f2c7459714e581e91a36aa5d
Signing key: 99DC5E4DB05F6BE2
Tree: 8d19faaed78d19a6bfc3b1f345eed78f5b12737d
File Lines added Lines deleted
Mode.cpp 6 1
Mode.hpp 10 4
File Mode.cpp changed (mode: 100644) (index 88d58b7..4711dc9)
... ... Shortcut::Shortcut(std::wstring shortcut, std::function<void(int)> handler,
39 39 descr(std::move(descr)) descr(std::move(descr))
40 40 { } { }
41 41
42 Mode::Mode(std::string id) : id(std::move(id))
42 Mode::Mode(std::string id) : id(std::move(id)), useCount(false)
43 43 { } { }
44 44
45 45 void void
 
... ... Mode::addShortcut(Shortcut shortcut)
48 48 if (shortcut.shortcut.length() > 4U) { if (shortcut.shortcut.length() > 4U) {
49 49 throw std::invalid_argument("Shortcut value is too long"); throw std::invalid_argument("Shortcut value is too long");
50 50 } }
51
52 if (!seenShortcuts.emplace(shortcut.shortcut).second) {
53 throw std::invalid_argument("Duplicated shortcut");
54 }
55
51 56 shortcuts.emplace_back(std::move(shortcut)); shortcuts.emplace_back(std::move(shortcut));
52 57 } }
File Mode.hpp changed (mode: 100644) (index 21322f0..e95ec1b)
22 22 #include <deque> #include <deque>
23 23 #include <functional> #include <functional>
24 24 #include <string> #include <string>
25 #include <unordered_set>
25 26
26 27 namespace vle { namespace vle {
27 28
 
... ... public:
69 70 { return useCount; } { return useCount; }
70 71
71 72 // Registers a shortcut. Throws `std::invalid_argument` if shortcut is too // Registers a shortcut. Throws `std::invalid_argument` if shortcut is too
72 // long.
73 // long or a duplicate.
73 74 void addShortcut(Shortcut shortcut); void addShortcut(Shortcut shortcut);
74 75 // Retrieves shortcuts of this mode. // Retrieves shortcuts of this mode.
75 76 const std::deque<Shortcut> & getShortcuts() const const std::deque<Shortcut> & getShortcuts() const
76 77 { return shortcuts; } { return shortcuts; }
77 78
78 79 private: private:
79 std::string id; // Identifier of the mode.
80 std::deque<Shortcut> shortcuts; // Shortcuts of this mode.
81 bool useCount = false; // Whether count is used by shortcuts.
80 // Identifier of the mode.
81 std::string id;
82 // Known hortcut values of this mode.
83 std::unordered_set<std::wstring> seenShortcuts;
84 // Shortcuts of this mode.
85 std::deque<Shortcut> shortcuts;
86 // Whether count is used by shortcuts.
87 bool useCount;
82 88 }; };
83 89
84 90 } }
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/libvle

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/libvle

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master