~aleteoryx/muditaos

ref: d558e60a4d5bc26a982f0ba85f20631d7efac5df muditaos/module-services/service-db/agents/settings/SettingsCache.cpp -rw-r--r-- 1.8 KiB
d558e60a — Lefucjusz [CP-2013] Add time sync endpoint 2 years ago
                                                                                
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <service-db/SettingsCache.hpp>
#include <mutex.hpp>

namespace settings
{

    namespace
    {
        class SettingsCacheImpl : public SettingsCache
        {
          public:
            static SettingsCacheImpl &get()
            {
                static SettingsCacheImpl instance;
                return instance;
            }

            const std::string &getValue(const EntryPath &path) const;
            void setValue(const EntryPath &path, const std::string &value);

          private:
            std::map<EntryPath, std::string> settingsMap;
            mutable cpp_freertos::MutexStandard settingsMutex;
        };

        const std::string &SettingsCacheImpl::getValue(const EntryPath &path) const
        {
            static const std::string empty;
            cpp_freertos::LockGuard lock(settingsMutex);
            auto pathIt = settingsMap.find(path);
            if (settingsMap.end() != pathIt) {
                return pathIt->second;
            }
            return empty;
        }

        void SettingsCacheImpl::setValue(const EntryPath &path, const std::string &value)
        {
            cpp_freertos::LockGuard lock(settingsMutex);
            settingsMap[path] = value;
        }
    } // namespace

    SettingsCache *SettingsCache::getInstance()
    {
        return &SettingsCacheImpl::get();
    }

    const std::string &SettingsCache::getValue(const EntryPath &path) const
    {
        return SettingsCacheImpl::get().getValue(path);
    }

    void SettingsCache::setValue(const EntryPath &path, const std::string &value)
    {
        return SettingsCacheImpl::get().setValue(path, value);
    }
} // namespace settings