]> git.tdb.fi Git - builder.git/blob - source/config.h
Replace the overly generic configuration profiles with something more purposeful
[builder.git] / source / config.h
1 #ifndef CONFIG_H_
2 #define CONFIG_H_
3
4 #include <map>
5 #include <string>
6 #include <msp/datafile/loader.h>
7 #include <msp/fs/path.h>
8 #include <msp/time/timestamp.h>
9 #include "misc.h"
10
11 class SourcePackage;
12
13 /**
14 Manages configuration for a package.  A configuration may have an arbitary
15 amount of options, as well as a modification time (mtime).
16 */
17 class Config
18 {
19 public:
20         /** A single configuration option. */
21         struct Option
22         {
23                 std::string name;
24                 std::string defv;
25                 std::string descr;
26                 std::string value;
27
28                 Option(const std::string &, const std::string &, const std::string &);
29         };
30
31         typedef std::map<std::string, Option> OptionMap;
32
33 private:
34         class Loader: public Msp::DataFile::ObjectLoader<Config>
35         {
36         public:
37                 Loader(Config &);
38         private:
39                 void option(const std::string &, const std::string &);
40         };
41
42         SourcePackage &package;
43         OptionMap options;
44         Msp::Time::TimeStamp mtime;
45         bool changed;
46
47 public:
48         Config(SourcePackage &);
49
50         /** Adds a configuration option with name, default value and description. */
51         void add_option(const std::string &, const std::string &, const std::string &);
52
53         /** Gets a configuration option by name. */
54         const Option &get_option(const std::string &) const;
55
56         const OptionMap &get_options() const { return options; }
57         const Msp::Time::TimeStamp &get_mtime() const { return mtime; }
58
59         /** Checks whether an option exists. */
60         bool is_option(const std::string &) const;
61
62         /** Processes options from the given raw option map.  Nonexistent options
63         are ignored.  If any options were changed, the mtime of the configuration is
64         updated to the current time.  Return value indicates whether any options
65         were changed. */
66         bool update(const StringMap &);
67
68         /** Expands any variable references in options. */
69         void finish();
70
71         void save() const;
72 private:
73         bool set_option(const std::string &, const std::string &);
74 public:
75         void load();
76 };
77
78 #endif