daemon: Fix possible use-after-free.
[jackhill/guix/guix.git] / nix / libutil / types.hh
1 #pragma once
2
3 #include "config.h"
4
5 #include <string>
6 #include <list>
7 #include <set>
8
9 #include <boost/format.hpp>
10
11
12 namespace nix {
13
14
15 /* Inherit some names from other namespaces for convenience. */
16 using std::string;
17 using std::list;
18 using std::set;
19 using std::vector;
20 using boost::format;
21
22
23 struct FormatOrString
24 {
25 string s;
26 FormatOrString(const string & s) : s(s) { };
27 FormatOrString(const format & f) : s(f.str()) { };
28 FormatOrString(const char * s) : s(s) { };
29 };
30
31
32 /* BaseError should generally not be caught, as it has Interrupted as
33 a subclass. Catch Error instead. */
34 class BaseError : public std::exception
35 {
36 protected:
37 string prefix_; // used for location traces etc.
38 string err;
39 public:
40 unsigned int status; // exit status
41 BaseError(const FormatOrString & fs, unsigned int status = 1);
42 ~BaseError() throw () { };
43 const char * what() const throw () { return err.c_str(); }
44 const string & msg() const throw () { return err; }
45 const string & prefix() const throw () { return prefix_; }
46 BaseError & addPrefix(const FormatOrString & fs);
47 };
48
49 #define MakeError(newClass, superClass) \
50 class newClass : public superClass \
51 { \
52 public: \
53 newClass(const FormatOrString & fs, unsigned int status = 1) : superClass(fs, status) { }; \
54 };
55
56 MakeError(Error, BaseError)
57
58 class SysError : public Error
59 {
60 public:
61 int errNo;
62 SysError(const FormatOrString & fs);
63 };
64
65
66 typedef list<string> Strings;
67 typedef set<string> StringSet;
68
69
70 /* Paths are just strings. */
71 typedef string Path;
72 typedef list<Path> Paths;
73 typedef set<Path> PathSet;
74
75
76 typedef enum {
77 lvlError = 0,
78 lvlInfo,
79 lvlTalkative,
80 lvlChatty,
81 lvlDebug,
82 lvlVomit
83 } Verbosity;
84
85
86 }