librime 1.2
Rime Input Method Engine, the core library
config_cow_ref.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5
6#ifndef RIME_CONFIG_COW_REF_H_
7#define RIME_CONFIG_COW_REF_H_
8
9#include <rime/common.h>
12
13namespace rime {
14
15template <class T>
17 public:
18 ConfigCowRef(an<ConfigItemRef> parent, string key)
19 : ConfigItemRef(nullptr), parent_(parent), key_(key) {}
20 an<ConfigItem> GetItem() const override {
21 auto container = As<T>(**parent_);
22 return container ? Read(container, key_) : nullptr;
23 }
24 void SetItem(an<ConfigItem> item) override {
25 auto container = As<T>(**parent_);
26 if (!copied_) {
27 *parent_ = container = CopyOnWrite(container, key_);
28 copied_ = true;
29 }
30 Write(container, key_, item);
31 }
32
33 protected:
34 static an<T> CopyOnWrite(const an<T>& container, const string& key);
35 static an<ConfigItem> Read(const an<T>& container, const string& key);
36 static void Write(const an<T>& container,
37 const string& key,
38 an<ConfigItem> value);
39
41 string key_;
42 bool copied_ = false;
43};
44
45template <class T>
47 const string& key) {
48 if (!container) {
49 DLOG(INFO) << "creating node: " << key;
50 return New<T>();
51 }
52 DLOG(INFO) << "copy on write: " << key;
53 return New<T>(*container);
54}
55
56template <>
58 const string& key) {
59 return map->Get(key);
60}
61
62template <>
64 const string& key,
65 an<ConfigItem> value) {
66 map->Set(key, value);
67}
68
69template <>
71 const string& key) {
72 return list->GetAt(ConfigData::ResolveListIndex(list, key, true));
73}
74
75template <>
77 const string& key,
78 an<ConfigItem> value) {
79 list->SetAt(ConfigData::ResolveListIndex(list, key), value);
80}
81
82inline an<ConfigItemRef> Cow(an<ConfigItemRef> parent, string key) {
84 return New<ConfigCowRef<ConfigList>>(parent, key);
85 else
86 return New<ConfigCowRef<ConfigMap>>(parent, key);
87}
88
89} // namespace rime
90
91#endif // RIME_CONFIG_COW_REF_H_
#define DLOG(severity)
Definition no_logging.h:53
Definition algebra.cc:12
std::shared_ptr< T > an
Definition common.h:60
an< X > As(const an< Y > &ptr)
Definition common.h:67
an< ConfigItemRef > Cow(an< ConfigItemRef > parent, string key)
Definition config_cow_ref.h:82
an< T > New(Args &&... args)
Definition common.h:77
ConfigCowRef(an< ConfigItemRef > parent, string key)
Definition config_cow_ref.h:18
an< ConfigItemRef > parent_
Definition config_cow_ref.h:40
an< ConfigItem > GetItem() const override
Definition config_cow_ref.h:20
static void Write(const an< T > &container, const string &key, an< ConfigItem > value)
static an< ConfigItem > Read(const an< T > &container, const string &key)
void SetItem(an< ConfigItem > item) override
Definition config_cow_ref.h:24
string key_
Definition config_cow_ref.h:41
static an< T > CopyOnWrite(const an< T > &container, const string &key)
Definition config_cow_ref.h:46
bool copied_
Definition config_cow_ref.h:42
static size_t ResolveListIndex(an< ConfigItem > list, const string &key, bool read_only=false)
Definition config_data.cc:113
static bool IsListItemReference(const string &key)
Definition config_data.cc:98
ConfigItemRef(ConfigData *data)
Definition config_types.h:128