WIP:afs-service-commit
[jackhill/guix/guix.git] / nix / libutil / types.hh
CommitLineData
36457566
LC
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
2bb04905
LC
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
36457566
LC
20
21namespace nix {
22
23
24/* Inherit some names from other namespaces for convenience. */
25using std::string;
26using std::list;
27using std::set;
28using std::vector;
29using boost::format;
30
31
32struct 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. */
43class BaseError : public std::exception
44{
45protected:
46 string prefix_; // used for location traces etc.
47 string err;
48public:
49 unsigned int status; // exit status
50 BaseError(const FormatOrString & fs, unsigned int status = 1);
2bb04905 51#ifdef EXCEPTION_NEEDS_THROW_SPEC
36457566
LC
52 ~BaseError() throw () { };
53 const char * what() const throw () { return err.c_str(); }
2bb04905
LC
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_; }
36457566
LC
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
69MakeError(Error, BaseError)
70
71class SysError : public Error
72{
73public:
74 int errNo;
75 SysError(const FormatOrString & fs);
76};
77
78
79typedef list<string> Strings;
80typedef set<string> StringSet;
81
82
83/* Paths are just strings. */
84typedef string Path;
85typedef list<Path> Paths;
86typedef set<Path> PathSet;
87
88
89typedef enum {
90 lvlError = 0,
91 lvlInfo,
92 lvlTalkative,
93 lvlChatty,
94 lvlDebug,
95 lvlVomit
96} Verbosity;
97
98
99}