]> git.tdb.fi Git - builder.git/blob - source/cache.h
Save caches before starting the build
[builder.git] / source / cache.h
1 #ifndef CACHE_H_
2 #define CACHE_H_
3
4 #include <list>
5 #include <map>
6 #include <msp/fs/path.h>
7 #include <msp/time/timestamp.h>
8
9 class SourcePackage;
10 class Target;
11
12 /**
13 Stores data between build runs.  This can be used to avoid scanning files again
14 every time builder is run, or to detect outside changes.
15
16 Data is stored as lists of strings and keyed to target and an arbitrary
17 identifier.  Any kind of strings can be stored, even ones that contain
18 unprintable characters or nuls.
19 */
20 class Cache
21 {
22 public:
23         typedef std::list<std::string> ValueList;
24 private:
25         typedef std::pair<std::string, std::string> Key;
26         typedef std::map<Key, ValueList> DataMap;
27
28         SourcePackage &package;
29         Msp::FS::Path filename;
30         DataMap data;
31         Msp::Time::TimeStamp mtime;
32         mutable bool changed;
33
34 public:
35         Cache(SourcePackage &p);
36
37         /// Sets a key to a single value, replacing any existing values.
38         void set_value(const Target *, const std::string &, const std::string &);
39
40         /// Appends a value to a key.  If the key does not exist, it is created.
41         void append_value(const Target *, const std::string &, const std::string &);
42
43         /// Sets a key to a list of values, replacing any existing values.
44         void set_values(const Target *, const std::string &, const ValueList &);
45
46         /** Returns the first value from a key.  The key must exist and be
47         non-empty. */
48         const std::string &get_value(const Target *, const std::string &);
49
50         /// Returns the values from a key.  The key must exist.
51         const ValueList &get_values(const Target *, const std::string &);
52
53         /// Indicates whether a key exists.
54         bool has_key(const Target *, const std::string &);
55
56         /// Returns the last modification timestamp of the cache.
57         const Msp::Time::TimeStamp &get_mtime() const { return mtime; }
58
59         /** Loads the cache file and sets the last modification timestamp
60         accordingly. */
61         void load();
62
63         /** Saves the cache.  Does nothing if there is no data to save or nothing
64         has changed. */
65         void save() const;
66 };
67
68 #endif