diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2020-06-17 10:26:52 +0200 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2020-06-17 10:26:52 +0200 |
commit | 1524752c17ee8753467f068c23fbe1d994aa8f75 (patch) | |
tree | c802ca482e2461765dcbdcef5cc6fa38c9d1c42b /src/libutil/types.hh | |
parent | 7db879e65e83b1c65206b490d36a69e97c5a877a (diff) | |
parent | 29542865cee37ab22efe1bd142900b69f6c59f0d (diff) |
Merge remote-tracking branch 'origin/master' into flakes
Diffstat (limited to 'src/libutil/types.hh')
-rw-r--r-- | src/libutil/types.hh | 145 |
1 files changed, 4 insertions, 141 deletions
diff --git a/src/libutil/types.hh b/src/libutil/types.hh index f70590b44..3af485fa0 100644 --- a/src/libutil/types.hh +++ b/src/libutil/types.hh @@ -1,165 +1,29 @@ #pragma once - #include "ref.hh" -#include <string> #include <list> #include <set> -#include <memory> #include <map> - -#include <boost/format.hpp> - -/* Before 4.7, gcc's std::exception uses empty throw() specifiers for - * its (virtual) destructor and what() in c++11 mode, in violation of spec - */ -#ifdef __GNUC__ -#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7) -#define EXCEPTION_NEEDS_THROW_SPEC -#endif -#endif - +#include <vector> namespace nix { - -/* Inherit some names from other namespaces for convenience. */ -using std::string; using std::list; using std::set; using std::vector; -using boost::format; - - -/* A variadic template that does nothing. Useful to call a function - for all variadic arguments but ignoring the result. */ -struct nop { template<typename... T> nop(T...) {} }; - - -struct FormatOrString -{ - string s; - FormatOrString(const string & s) : s(s) { }; - template<class F> - FormatOrString(const F & f) : s(f.str()) { }; - FormatOrString(const char * s) : s(s) { }; -}; - - -/* A helper for formatting strings. ‘fmt(format, a_0, ..., a_n)’ is - equivalent to ‘boost::format(format) % a_0 % ... % - ... a_n’. However, ‘fmt(s)’ is equivalent to ‘s’ (so no %-expansion - takes place). */ - -template<class F> -inline void formatHelper(F & f) -{ -} - -template<class F, typename T, typename... Args> -inline void formatHelper(F & f, const T & x, const Args & ... args) -{ - formatHelper(f % x, args...); -} - -inline std::string fmt(const std::string & s) -{ - return s; -} - -inline std::string fmt(const char * s) -{ - return s; -} - -inline std::string fmt(const FormatOrString & fs) -{ - return fs.s; -} - -template<typename... Args> -inline std::string fmt(const std::string & fs, const Args & ... args) -{ - boost::format f(fs); - f.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit); - formatHelper(f, args...); - return f.str(); -} - - -/* BaseError should generally not be caught, as it has Interrupted as - a subclass. Catch Error instead. */ -class BaseError : public std::exception -{ -protected: - string prefix_; // used for location traces etc. - string err; -public: - unsigned int status = 1; // exit status - - template<typename... Args> - BaseError(unsigned int status, const Args & ... args) - : err(fmt(args...)) - , status(status) - { - } - - template<typename... Args> - BaseError(const Args & ... args) - : err(fmt(args...)) - { - } - -#ifdef EXCEPTION_NEEDS_THROW_SPEC - ~BaseError() throw () { }; - const char * what() const throw () { return err.c_str(); } -#else - const char * what() const noexcept { return err.c_str(); } -#endif - - const string & msg() const { return err; } - const string & prefix() const { return prefix_; } - BaseError & addPrefix(const FormatOrString & fs); -}; - -#define MakeError(newClass, superClass) \ - class newClass : public superClass \ - { \ - public: \ - using superClass::superClass; \ - } - -MakeError(Error, BaseError); -MakeError(UsageError, Error); - -class SysError : public Error -{ -public: - int errNo; - - template<typename... Args> - SysError(const Args & ... args) - : Error(addErrno(fmt(args...))) - { } - -private: - - std::string addErrno(const std::string & s); -}; - +using std::string; typedef list<string> Strings; typedef set<string> StringSet; -typedef std::map<std::string, std::string> StringMap; - +typedef std::map<string, string> StringMap; /* Paths are just strings. */ + typedef string Path; typedef list<Path> Paths; typedef set<Path> PathSet; - /* Helper class to run code at startup. */ template<typename T> struct OnStartup @@ -167,5 +31,4 @@ struct OnStartup OnStartup(T && t) { t(); } }; - } |