librime: config_cow_ref.h Source File

| 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>
10#include <rime/config/config_data.h>
11#include <rime/config/config_types.h>
12
13namespace rime {
14
15template <class T>
16 class ConfigCowRef : public ConfigItemRef {
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
40 an<ConfigItemRef> parent_;
41 string key_;
42 bool copied_ = false;
43};
44
45template <class T>
46 inline an<T> ConfigCowRef<T>::CopyOnWrite(const an<T>& container,
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 <>
57 inline an<ConfigItem> ConfigCowRef<ConfigMap>::Read(const an<ConfigMap>& map,
58 const string& key) {
59 return map->Get(key);
60}
61
62template <>
63 inline void ConfigCowRef<ConfigMap>::Write(const an<ConfigMap>& map,
64 const string& key,
65 an<ConfigItem> value) {
66 map->Set(key, value);
67}
68
69template <>
70 inline an<ConfigItem> ConfigCowRef<ConfigList>::Read(const an<ConfigList>& list,
71 const string& key) {
72 return list->GetAt(ConfigData::ResolveListIndex(list, key, true));
73}
74
75template <>
76 inline void ConfigCowRef<ConfigList>::Write(const an<ConfigList>& list,
77 const string& key,
78 an<ConfigItem> value) {
79 list->SetAt(ConfigData::ResolveListIndex(list, key), value);
80}
81
82 inline an<ConfigItemRef> Cow(an<ConfigItemRef> parent, string key) {
83 if (ConfigData::IsListItemReference(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_
common.h
DLOG
#define DLOG(severity)
Definition no_logging.h:53
config_data.h
config_types.h
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::As
an< X > As(const an< Y > &ptr)
Definition common.h:67
rime::Cow
an< ConfigItemRef > Cow(an< ConfigItemRef > parent, string key)
Definition config_cow_ref.h:82
rime::New
an< T > New(Args &&... args)
Definition common.h:77
rime::ConfigCowRef::ConfigCowRef
ConfigCowRef(an< ConfigItemRef > parent, string key)
Definition config_cow_ref.h:18
rime::ConfigCowRef::parent_
an< ConfigItemRef > parent_
Definition config_cow_ref.h:40
rime::ConfigCowRef::GetItem
an< ConfigItem > GetItem() const override
Definition config_cow_ref.h:20
rime::ConfigCowRef::Write
static void Write(const an< T > &container, const string &key, an< ConfigItem > value)
rime::ConfigCowRef::Read
static an< ConfigItem > Read(const an< T > &container, const string &key)
rime::ConfigCowRef::SetItem
void SetItem(an< ConfigItem > item) override
Definition config_cow_ref.h:24
rime::ConfigCowRef::key_
string key_
Definition config_cow_ref.h:41
rime::ConfigCowRef::CopyOnWrite
static an< T > CopyOnWrite(const an< T > &container, const string &key)
Definition config_cow_ref.h:46
rime::ConfigCowRef::copied_
bool copied_
Definition config_cow_ref.h:42
rime::ConfigData::ResolveListIndex
static size_t ResolveListIndex(an< ConfigItem > list, const string &key, bool read_only=false)
Definition config_data.cc:113
rime::ConfigData::IsListItemReference
static bool IsListItemReference(const string &key)
Definition config_data.cc:98
rime::ConfigItemRef::ConfigItemRef
ConfigItemRef(ConfigData *data)
Definition config_types.h:128