Merge branch 'master' into core-updates
[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 /* Before 4.7, gcc's std::exception uses empty throw() specifiers for
12 * its (virtual) destructor and what() in c++11 mode, in violation of spec
13 */
14 #ifdef __GNUC__
15 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7)
16 #define EXCEPTION_NEEDS_THROW_SPEC
17 #endif
18 #endif
19
20
21 namespace nix {
22
23
24 /* Inherit some names from other namespaces for convenience. */
25 using std::string;
26 using std::list;
27 using std::set;
28 using std::vector;
29 using boost::format;
30
31
32 struct FormatOrString
33 {
34 string s;
35 FormatOrString(const string & s) : s(s) { };
36 FormatOrString(const format & f) : s(f.str()) { };
37 FormatOrString(const char * s) : s(s) { };
38 };
39
40
41 /* BaseError should generally not be caught, as it has Interrupted as
42 a subclass. Catch Error instead. */
43 class BaseError : public std::exception
44 {
45 protected:
46 string prefix_; // used for location traces etc.
47 string err;
48 public:
49 unsigned int status; // exit status
50 BaseError(const FormatOrString & fs, unsigned int status = 1);
51 #ifdef EXCEPTION_NEEDS_THROW_SPEC
52 ~BaseError() throw () { };
53 const char * what() const throw () { return err.c_str(); }
54 #else
55 const char * what() const noexcept { return err.c_str(); }
56 #endif
57 const string & msg() const { return err; }
58 const string & prefix() const { return prefix_; }
59 BaseError & addPrefix(const FormatOrString & fs);
60 };
61
62 #define MakeError(newClass, superClass) \
63 class newClass : public superClass \
64 { \
65 public: \
66 newClass(const FormatOrString & fs, unsigned int status = 1) : superClass(fs, status) { }; \
67 };
68
69 MakeError(Error, BaseError)
70
71 class SysError : public Error
72 {
73 public:
74 int errNo;
75 SysError(const FormatOrString & fs);
76 };
77
78
79 typedef list<string> Strings;
80 typedef set<string> StringSet;
81
82
83 /* Paths are just strings. */
84 typedef string Path;
85 typedef list<Path> Paths;
86 typedef set<Path> PathSet;
87
88
89 typedef enum {
90 lvlError = 0,
91 lvlInfo,
92 lvlTalkative,
93 lvlChatty,
94 lvlDebug,
95 lvlVomit
96 } Verbosity;
97
98
99 }