librime 1.2
Rime Input Method Engine, the core library
switches.h
Go to the documentation of this file.
1#ifndef RIME_SWITCHES_H_
2#define RIME_SWITCHES_H_
3
4#include <rime/common.h>
5
6namespace rime {
7
8class Config;
9class ConfigItemRef;
10class ConfigMap;
11class ConfigValue;
12
14 const char* str;
15 size_t length;
16
17 operator string() const {
18 return str && length ? string(str, length) : string();
19 }
20
21 operator bool() const { return bool(str) && bool(length); }
22};
23
24class Switches {
25 public:
26 explicit Switches(Config* config) : config_(config) {}
27
31 };
32
33 struct SwitchOption {
37 // reset state value on initialize. -1 if unspecified.
38 int reset_value = -1;
39 // index of the switch configuration.
40 size_t switch_index = 0;
41 // the index of the option in the radio group.
42 size_t option_index = 0;
43
44 bool found() const { return bool(the_switch); }
45 };
46
50 };
51
52 SwitchOption FindOption(function<FindResult(SwitchOption option)> callback);
53
54 SwitchOption OptionByName(const string& option_name);
55
56 // Returns the switch option defined at switch_index.
57 // If the switch is a radio group, return the first option in the group.
58 SwitchOption ByIndex(size_t switch_index);
59
60 static SwitchOption Cycle(const SwitchOption& option);
61
62 static SwitchOption Reset(const SwitchOption& option);
63
65 an<ConfigMap> the_switch,
66 function<FindResult(SwitchOption option)> callback);
67
68 static StringSlice GetStateLabel(an<ConfigMap> the_switch,
69 size_t state_index,
70 bool abbreviated);
71
72 StringSlice GetStateLabel(const string& option_name,
73 int state,
74 bool abbreviated);
75
76 private:
77 SwitchOption FindOptionFromConfigItem(
78 ConfigItemRef& item,
79 size_t switch_index,
80 function<FindResult(SwitchOption option)> callback);
81
82 Config* config_;
83};
84
85} // namespace rime
86
87#endif // RIME_SWITCHES_H_
Definition algebra.cc:12
std::shared_ptr< T > an
Definition common.h:60
Switches::SwitchOption SwitchOption
Definition switch_translator.cc:24
Definition config_component.h:21
Definition config_types.h:34
Definition config_types.h:87
Definition config_types.h:126
Definition switches.h:13
size_t length
Definition switches.h:15
const char * str
Definition switches.h:14
FindResult
Definition switches.h:47
@ kFound
Definition switches.h:49
@ kContinue
Definition switches.h:48
static SwitchOption Reset(const SwitchOption &option)
Definition switches.cc:92
SwitchOption ByIndex(size_t switch_index)
Definition switches.cc:63
static SwitchOption Cycle(const SwitchOption &option)
Definition switches.cc:75
static StringSlice GetStateLabel(an< ConfigMap > the_switch, size_t state_index, bool abbreviated)
Definition switches.cc:138
SwitchOption FindOption(function< FindResult(SwitchOption option)> callback)
Definition switches.cc:40
static SwitchOption FindRadioGroupOption(an< ConfigMap > the_switch, function< FindResult(SwitchOption option)> callback)
Definition switches.cc:110
Switches(Config *config)
Definition switches.h:26
SwitchType
Definition switches.h:28
@ kRadioGroup
Definition switches.h:30
@ kToggleOption
Definition switches.h:29
SwitchOption OptionByName(const string &option_name)
Definition switches.cc:57
Definition switches.h:33
string option_name
Definition switches.h:36
int reset_value
Definition switches.h:38
size_t option_index
Definition switches.h:42
an< ConfigMap > the_switch
Definition switches.h:34
bool found() const
Definition switches.h:44
SwitchType type
Definition switches.h:35
size_t switch_index
Definition switches.h:40