aboutsummaryrefslogtreecommitdiff
path: root/lib/settings/configuration.h
blob: db93760abfa05b4b56bf58452106f71bf64d7170 (plain)
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
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#ifndef CONFIGURATION_H
#define CONFIGURATION_H

#include <libconfig.h++>
#include <optional>
#include <string>
#include <vector>

class Configuration
{
public:
    explicit Configuration(const std::string &path, const std::string &home);
    ~Configuration();

    bool read();
    bool parse(const std::string &contents);
    bool writeIfNeeded(const std::string &path = std::string());

    bool parseDefaultConfiguration(const std::string &contents);

    std::vector<std::string> childrenSettings(const char *name = "");
    std::vector<std::string> childrenGroups(const char *name = "");

    void resetValue(const char *path);

    template <typename T>
    std::optional<T> value(const char *path) const
    {
        // if setting doesn't exist, give back a nullopt
        if(!m_userCfg->exists(path)) {
            const_cast<Configuration *>(this)->resetValue(path);
            return value<T>(path);
        }

        const libconfig::Setting &v = m_userCfg->lookup(path);
        if constexpr(std::is_same_v<T, std::string>)
            return std::optional<std::string>(castToString(v));
        else
            return std::optional<T>(static_cast<T>(v));
    }

    template <typename T>
    bool setValue(std::string path, const T &val);

private:
    std::string castToString(const libconfig::Setting &v) const;

    bool changed = false;
    std::string m_homePath;
    std::string m_userCfgPath;
    libconfig::Config *m_userCfg, *m_defaultCfg;
};

// replace ~ with home
std::string patchHome(const std::string &path, const std::string &home);

// instantiate functions
// this needs to be done because the implementation is in the cpp file

// Settings::setValue<>
extern template bool Configuration::setValue<int>(std::string path, const int &val);
extern template bool Configuration::setValue<unsigned int>(std::string path, const unsigned int &val);
extern template bool Configuration::setValue<long>(std::string path, const long &val);
extern template bool Configuration::setValue<unsigned long>(std::string path, const unsigned long &val);

extern template bool Configuration::setValue<long long>(std::string path, const long long &val);
extern template bool Configuration::setValue<unsigned long long>(std::string path, const unsigned long long &val);

extern template bool Configuration::setValue<float>(std::string path, const float &val);
extern template bool Configuration::setValue<double>(std::string path, const double &val);

extern template bool Configuration::setValue<bool>(std::string path, const bool &val);

extern template bool Configuration::setValue<std::string>(std::string path, const std::string &val);

#endif // CONFIGURATION_H