This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
setting.h
159 lines (118 loc) · 3.45 KB
/
setting.h
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2009 Daniel Erat <[email protected]>
// All rights reserved.
#ifndef __XSETTINGSD_SETTING_H__
#define __XSETTINGSD_SETTING_H__
#include <map>
#include <stdint.h>
#include <string>
#ifdef __TESTING
#include <gtest/gtest_prod.h>
#endif
#include "common.h"
namespace xsettingsd {
class DataWriter;
// Base class for settings.
class Setting {
public:
enum Type {
TYPE_INTEGER = 0,
TYPE_STRING = 1,
TYPE_COLOR = 2,
};
explicit Setting(Type type)
: type_(type),
serial_(0) {
}
virtual ~Setting() {}
uint32_t serial() const { return serial_; }
bool operator==(const Setting& other) const;
// Write this setting (using the passed-in setting name) in the format
// described in the XSETTINGS spec.
bool Write(const std::string& name, DataWriter* writer) const;
// Update this setting's serial number based on the previous version of
// the setting. (If the setting changed, we use 'serial'; otherwise we
// use the same serial as 'prev'.)
void UpdateSerial(const Setting* prev, uint32_t serial);
private:
// Write type-specific data.
virtual bool WriteBody(DataWriter* writer) const = 0;
// Cast 'other' to this setting's type and compare it.
virtual bool EqualsImpl(const Setting& other) const = 0;
Type type_;
// Incremented when the setting's value changes.
uint32_t serial_;
DISALLOW_COPY_AND_ASSIGN(Setting);
};
class IntegerSetting : public Setting {
public:
explicit IntegerSetting(int32_t value)
: Setting(TYPE_INTEGER),
value_(value) {
}
int32_t value() const { return value_; }
private:
#ifdef __TESTING
friend class IntegerSettingTest;
FRIEND_TEST(IntegerSettingTest, WriteBody);
#endif
bool WriteBody(DataWriter* writer) const;
bool EqualsImpl(const Setting& other) const;
int32_t value_;
DISALLOW_COPY_AND_ASSIGN(IntegerSetting);
};
class StringSetting : public Setting {
public:
explicit StringSetting(const std::string& value)
: Setting(TYPE_STRING),
value_(value) {
}
const std::string& value() const { return value_; }
private:
bool WriteBody(DataWriter* writer) const;
bool EqualsImpl(const Setting& other) const;
std::string value_;
DISALLOW_COPY_AND_ASSIGN(StringSetting);
};
class ColorSetting : public Setting {
public:
ColorSetting(uint16_t red,
uint16_t green,
uint16_t blue,
uint16_t alpha)
: Setting(TYPE_COLOR),
red_(red),
green_(green),
blue_(blue),
alpha_(alpha) {
}
uint16_t red() const { return red_; }
uint16_t green() const { return green_; }
uint16_t blue() const { return blue_; }
uint16_t alpha() const { return alpha_; }
private:
bool WriteBody(DataWriter* writer) const;
bool EqualsImpl(const Setting& other) const;
uint16_t red_;
uint16_t green_;
uint16_t blue_;
uint16_t alpha_;
DISALLOW_COPY_AND_ASSIGN(ColorSetting);
};
// A simple wrapper around a string-to-Setting map.
// Handles deleting the Setting objects in its d'tor.
class SettingsMap {
public:
SettingsMap() {}
~SettingsMap();
typedef std::map<std::string, Setting*> Map;
const Map& map() const { return map_; }
Map* mutable_map() { return &map_; }
void swap(SettingsMap* other) { map_.swap(other->map_); }
// Get a pointer to a setting or NULL if it doesn't exist.
const Setting* GetSetting(const std::string& name) const;
private:
Map map_;
DISALLOW_COPY_AND_ASSIGN(SettingsMap);
};
} // namespace xsettingsd
#endif