]> git.tdb.fi Git - builder.git/blob - source/config.h
Replace per-file copyright notices with a single file
[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::Loader
35         {
36         private:
37                 Config &conf;
38
39         public:
40                 Loader(Config &);
41         private:
42                 void option(const std::string &, const std::string &);
43         };
44
45         SourcePackage &package;
46         OptionMap options;
47         Msp::Time::TimeStamp mtime;
48         bool freeze_mtime;
49
50 public:
51         Config(SourcePackage &);
52
53         /** Adds a configuration option with name, default value and description. */
54         void add_option(const std::string &, const std::string &, const std::string &);
55
56         /** Gets a configuration option by name. */
57         const Option &get_option(const std::string &) const;
58
59         const OptionMap &get_options() const { return options; }
60         const Msp::Time::TimeStamp &get_mtime() const { return mtime; }
61
62         /** Checks whether an option exists. */
63         bool is_option(const std::string &) const;
64
65         /** Selects the last profile used.  If the profile cache file is not
66         present, the default profile is assumed. */
67         void select_last_profile();
68
69         /** Selects a profile.  The profile cache file is updated, unless doing a
70         dry run. */
71         void select_profile(const std::string &);
72
73         /** Processes options from the given raw option map.  Nonexistent options
74         are ignored.  If any options were changed, the mtime of the configuration is
75         updated to the current time.  Return value indicates whether any options
76         were changed. */
77         bool update(const StringMap &);
78
79         /** Expands any variable references in options. */
80         void finish();
81
82         void save() const;
83 private:
84         bool set_option(const std::string &, const std::string &);
85         void load();
86 };
87
88 #endif