The Pedigree Project  0.1
ConfigurationManager.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/kernel/config/ConfigurationManager.h"
21 #include "pedigree/kernel/config/ConfigurationBackend.h"
22 #include "pedigree/kernel/processor/types.h"
23 #include "pedigree/kernel/utilities/Result.h"
24 #include "pedigree/kernel/utilities/new"
25 
26 ConfigurationManager ConfigurationManager::m_Instance;
27 
28 ConfigurationManager::ConfigurationManager() : m_Backends()
29 {
30 }
31 
32 ConfigurationManager::~ConfigurationManager()
33 {
34 }
35 
36 ConfigurationManager &ConfigurationManager::instance()
37 {
38  return m_Instance;
39 }
40 
41 size_t ConfigurationManager::createTable(
42  const String &configStore, const String &table)
43 {
44  // Lookup the backend
46  m_Backends.lookup(configStore);
47  if (!result.hasValue())
48  return 0;
49  return result.value()->createTable(table);
50 }
51 
53  const String &configStore, const String &table, const String &key,
54  const ConfigValue &value)
55 {
56  // Lookup the backend
58  m_Backends.lookup(configStore);
59  if (!result.hasValue())
60  return;
61  return result.value()->insert(table, key, value);
62 }
63 
65  const String &configStore, const String &table, const String &key)
66 {
67  static ConfigValue v;
68 
69  // Lookup the backend
71  m_Backends.lookup(configStore);
72  if (!result.hasValue())
73  return v;
74  return result.value()->select(table, key);
75 }
76 
78  const String &configStore, const String &table, const String &key,
79  ConfigurationWatcher watcher)
80 {
81  // Lookup the backend
83  m_Backends.lookup(configStore);
84  if (!result.hasValue())
85  return;
86  return result.value()->watch(table, key, watcher);
87 }
88 
90  const String &configStore, const String &table, const String &key,
91  ConfigurationWatcher watcher)
92 {
93  // Lookup the backend
95  m_Backends.lookup(configStore);
96  if (!result.hasValue())
97  return;
98  return result.value()->unwatch(table, key, watcher);
99 }
100 
101 bool ConfigurationManager::installBackend(
102  ConfigurationBackend *pBackend, const String &configStore)
103 {
104  // Check for sanity
105  if (!pBackend)
106  return false;
107 
108  // Get the real config store to use
109  const String &realConfigStore =
110  configStore.length() ? configStore : pBackend->getConfigStore();
111 
112  // Install into the list
113  if (!backendExists(realConfigStore))
114  {
115  m_Backends.insert(realConfigStore, pBackend);
116  return true;
117  }
118  else
119  return false;
120 }
121 
122 void ConfigurationManager::removeBackend(const String &configStore)
123 {
124  // Lookup the backend
126  m_Backends.lookup(configStore);
127  if (!result.hasValue())
128  return;
129 
130  // Remove it from the list and free used memory
131  m_Backends.remove(configStore);
132  delete result.value();
133 }
134 
135 bool ConfigurationManager::backendExists(const String &configStore)
136 {
137  return m_Backends.lookup(configStore).hasValue();
138 }
void insert(const String &configStore, const String &table, const String &key, const ConfigValue &value)
A key/value dictionary for string keys.
Definition: RadixTree.h:45
void watch(const String &configStore, const String &table, const String &key, ConfigurationWatcher watcher)
void insert(const String &key, const T &value)
Definition: RadixTree.h:347
Definition: String.h:49
Result< T, bool > lookup(const String &key) const
Definition: RadixTree.h:462
void unwatch(const String &configStore, const String &table, const String &key, ConfigurationWatcher watcher)
ConfigValue & select(const String &configStore, const String &table, const String &key)