]> git.tdb.fi Git - builder.git/blob - source/config.h
Evaluate conditions at load time to allow more flexibility
[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         typedef std::map<std::string, std::string> InputOptions;
33
34 private:
35         class Loader: public Msp::DataFile::ObjectLoader<Config>
36         {
37         public:
38                 Loader(Config &);
39         private:
40                 void option(const std::string &, const std::string &);
41         };
42
43         SourcePackage &package;
44         OptionMap options;
45         InputOptions pending_options;
46         Msp::Time::TimeStamp mtime;
47         bool changed;
48
49 public:
50         Config(SourcePackage &);
51
52         /** Adds a configuration option with name, default value and description. */
53         void add_option(const std::string &, const std::string &, const std::string &);
54
55         /** Gets a configuration option by name. */
56         const Option &get_option(const std::string &) const;
57
58         const OptionMap &get_options() const { return options; }
59         const Msp::Time::TimeStamp &get_mtime() const { return mtime; }
60
61         /** Checks whether an option exists. */
62         bool is_option(const std::string &) const;
63
64         /** Processes options from the given raw option map.  Nonexistent options
65         are ignored.  If any options were changed, the mtime of the configuration is
66         updated to the current time.  Return value indicates whether any options
67         were changed. */
68         bool update(const StringMap &);
69
70         /** Expands any variable references in options. */
71         void finish();
72
73         void save() const;
74         bool set_option(const std::string &, const std::string &);
75         void load();
76 };
77
78 #endif