]> git.tdb.fi Git - builder.git/blobdiff - source/cache.h
Replace DependencyCache with a more generic cache
[builder.git] / source / cache.h
diff --git a/source/cache.h b/source/cache.h
new file mode 100644 (file)
index 0000000..86a5f47
--- /dev/null
@@ -0,0 +1,66 @@
+#ifndef CACHE_H_
+#define CACHE_H_
+
+#include <msp/time/timestamp.h>
+#include "misc.h"
+
+class SourcePackage;
+class Target;
+
+/**
+Stores data between build runs.  This can be used to avoid scanning files again
+every time builder is run, or to detect outside changes.
+
+Data is stored as lists of strings and keyed to target and an arbitrary
+identifier.  Any kind of strings can be stored, even ones that contain
+unprintable characters or nuls.
+*/
+class Cache
+{
+public:
+       typedef std::list<std::string> ValueList;
+private:
+       typedef std::pair<std::string, std::string> Key;
+       typedef std::map<Key, ValueList> DataMap;
+
+       SourcePackage &package;
+       Msp::FS::Path filename;
+       DataMap data;
+       Msp::Time::TimeStamp mtime;
+       bool changed;
+
+public:
+       Cache(SourcePackage &p);
+
+       /// Sets a key to a single value, replacing any existing values.
+       void set_value(const Target *, const std::string &, const std::string &);
+
+       /// Appends a value to a key.  If the key does not exist, it is created.
+       void append_value(const Target *, const std::string &, const std::string &);
+
+       /// Sets a key to a list of values, replacing any existing values.
+       void set_values(const Target *, const std::string &, const ValueList &);
+
+       /** Returns the first value from a key.  The key must exist and be
+       non-empty. */
+       const std::string &get_value(const Target *, const std::string &);
+
+       /// Returns the values from a key.  The key must exist.
+       const ValueList &get_values(const Target *, const std::string &);
+
+       /// Indicates whether a key exists.
+       bool has_key(const Target *, const std::string &);
+
+       /// Returns the last modification timestamp of the cache.
+       const Msp::Time::TimeStamp &get_mtime() const { return mtime; }
+
+       /** Loads the cache file and sets the last modification timestamp
+       accordingly. */
+       void load();
+
+       /** Saves the cache.  Does nothing if there is no data to save or nothing
+       has changed. */
+       void save() const;
+};
+
+#endif