#include <map>
#include <string>
-#include "option.h"
+#include <msp/datafile/loader.h>
+#include <msp/fs/path.h>
+#include <msp/time/timestamp.h>
+#include "misc.h"
-typedef std::map<std::string, std::string> RawOptionMap;
+class SourcePackage;
+/**
+Manages configuration for a package. A configuration may have an arbitary
+amount of options, as well as a modification time (mtime).
+*/
class Config
{
public:
+ /** A single configuration option. */
struct Option
{
std::string name;
Option(const std::string &, const std::string &, const std::string &);
};
+
typedef std::map<std::string, Option> OptionMap;
+private:
+ class Loader: public Msp::DataFile::ObjectLoader<Config>
+ {
+ public:
+ Loader(Config &);
+ private:
+ void option(const std::string &, const std::string &);
+ };
+
+ SourcePackage &package;
+ OptionMap options;
+ Msp::Time::TimeStamp mtime;
+ bool freeze_mtime;
+
+public:
+ Config(SourcePackage &);
+
+ /** Adds a configuration option with name, default value and description. */
void add_option(const std::string &, const std::string &, const std::string &);
+
+ /** Gets a configuration option by name. */
const Option &get_option(const std::string &) const;
+
+ const OptionMap &get_options() const { return options; }
+ const Msp::Time::TimeStamp &get_mtime() const { return mtime; }
+
+ /** Checks whether an option exists. */
bool is_option(const std::string &) const;
- bool process(const RawOptionMap &);
- void load(const std::string &);
+
+ /** Selects the last profile used. If the profile cache file is not
+ present, the default profile is assumed. */
+ void select_last_profile();
+
+ /** Selects a profile. The profile cache file is updated, unless doing a
+ dry run. */
+ void select_profile(const std::string &);
+
+ /** Processes options from the given raw option map. Nonexistent options
+ are ignored. If any options were changed, the mtime of the configuration is
+ updated to the current time. Return value indicates whether any options
+ were changed. */
+ bool update(const StringMap &);
+
+ /** Expands any variable references in options. */
+ void finish();
+
+ void save() const;
private:
- OptionMap options;
+ bool set_option(const std::string &, const std::string &);
+ void load();
};
#endif