librime: switches.h Source File

| 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
13 struct StringSlice {
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
24 class Switches {
25 public:
26 explicit Switches(Config* config) : config_(config) {}
27
28 enum SwitchType {
29 kToggleOption,
30 kRadioGroup,
31 };
32
33 struct SwitchOption {
34 an<ConfigMap> the_switch = nullptr;
35 SwitchType type = kToggleOption;
36 string option_name;
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
47 enum FindResult {
48 kContinue,
49 kFound,
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
64 static SwitchOption FindRadioGroupOption(
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_
common.h
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::SwitchOption
Switches::SwitchOption SwitchOption
Definition switch_translator.cc:24
rime::Config
Definition config_component.h:21
rime::ConfigValue
Definition config_types.h:34
rime::ConfigMap
Definition config_types.h:87
rime::ConfigItemRef
Definition config_types.h:126
rime::StringSlice
Definition switches.h:13
rime::StringSlice::length
size_t length
Definition switches.h:15
rime::StringSlice::str
const char * str
Definition switches.h:14
rime::Switches::FindResult
FindResult
Definition switches.h:47
rime::Switches::kFound
@ kFound
Definition switches.h:49
rime::Switches::kContinue
@ kContinue
Definition switches.h:48
rime::Switches::Reset
static SwitchOption Reset(const SwitchOption &option)
Definition switches.cc:92
rime::Switches::ByIndex
SwitchOption ByIndex(size_t switch_index)
Definition switches.cc:63
rime::Switches::Cycle
static SwitchOption Cycle(const SwitchOption &option)
Definition switches.cc:75
rime::Switches::GetStateLabel
static StringSlice GetStateLabel(an< ConfigMap > the_switch, size_t state_index, bool abbreviated)
Definition switches.cc:138
rime::Switches::FindOption
SwitchOption FindOption(function< FindResult(SwitchOption option)> callback)
Definition switches.cc:40
rime::Switches::FindRadioGroupOption
static SwitchOption FindRadioGroupOption(an< ConfigMap > the_switch, function< FindResult(SwitchOption option)> callback)
Definition switches.cc:110
rime::Switches::Switches
Switches(Config *config)
Definition switches.h:26
rime::Switches::SwitchType
SwitchType
Definition switches.h:28
rime::Switches::kRadioGroup
@ kRadioGroup
Definition switches.h:30
rime::Switches::kToggleOption
@ kToggleOption
Definition switches.h:29
rime::Switches::OptionByName
SwitchOption OptionByName(const string &option_name)
Definition switches.cc:57
rime::Switches::SwitchOption
Definition switches.h:33
rime::Switches::SwitchOption::option_name
string option_name
Definition switches.h:36
rime::Switches::SwitchOption::reset_value
int reset_value
Definition switches.h:38
rime::Switches::SwitchOption::option_index
size_t option_index
Definition switches.h:42
rime::Switches::SwitchOption::the_switch
an< ConfigMap > the_switch
Definition switches.h:34
rime::Switches::SwitchOption::found
bool found() const
Definition switches.h:44
rime::Switches::SwitchOption::type
SwitchType type
Definition switches.h:35
rime::Switches::SwitchOption::switch_index
size_t switch_index
Definition switches.h:40