types.hh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include "config.h"
  3. #include <string>
  4. #include <list>
  5. #include <set>
  6. #include <boost/format.hpp>
  7. /* Before 4.7, gcc's std::exception uses empty throw() specifiers for
  8. * its (virtual) destructor and what() in c++11 mode, in violation of spec
  9. */
  10. #ifdef __GNUC__
  11. #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7)
  12. #define EXCEPTION_NEEDS_THROW_SPEC
  13. #endif
  14. #endif
  15. namespace nix {
  16. /* Inherit some names from other namespaces for convenience. */
  17. using std::string;
  18. using std::list;
  19. using std::set;
  20. using std::vector;
  21. using boost::format;
  22. struct FormatOrString
  23. {
  24. string s;
  25. FormatOrString(const string & s) : s(s) { };
  26. FormatOrString(const format & f) : s(f.str()) { };
  27. FormatOrString(const char * s) : s(s) { };
  28. };
  29. /* BaseError should generally not be caught, as it has Interrupted as
  30. a subclass. Catch Error instead. */
  31. class BaseError : public std::exception
  32. {
  33. protected:
  34. string prefix_; // used for location traces etc.
  35. string err;
  36. public:
  37. unsigned int status; // exit status
  38. BaseError(const FormatOrString & fs, unsigned int status = 1);
  39. #ifdef EXCEPTION_NEEDS_THROW_SPEC
  40. ~BaseError() throw () { };
  41. const char * what() const throw () { return err.c_str(); }
  42. #else
  43. const char * what() const noexcept { return err.c_str(); }
  44. #endif
  45. const string & msg() const { return err; }
  46. const string & prefix() const { return prefix_; }
  47. BaseError & addPrefix(const FormatOrString & fs);
  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. MakeError(Error, BaseError)
  56. class SysError : public Error
  57. {
  58. public:
  59. int errNo;
  60. SysError(const FormatOrString & fs);
  61. };
  62. typedef list<string> Strings;
  63. typedef set<string> StringSet;
  64. /* Paths are just strings. */
  65. typedef string Path;
  66. typedef list<Path> Paths;
  67. typedef set<Path> PathSet;
  68. typedef enum {
  69. lvlError = 0,
  70. lvlInfo,
  71. lvlTalkative,
  72. lvlChatty,
  73. lvlDebug,
  74. lvlVomit
  75. } Verbosity;
  76. }