2012-07-18 21:59:03 +03:00
|
|
|
|
#pragma once
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
2012-09-11 21:45:42 +03:00
|
|
|
|
|
2016-02-23 14:53:31 +02:00
|
|
|
|
#include "ref.hh"
|
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <list>
|
|
|
|
|
#include <set>
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
|
#include <memory>
|
2017-04-13 16:55:38 +03:00
|
|
|
|
#include <map>
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
|
2014-10-20 19:15:50 +03:00
|
|
|
|
/* 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
|
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
2017-05-11 18:06:07 +03:00
|
|
|
|
/* 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...) {} };
|
|
|
|
|
|
|
|
|
|
|
2014-03-28 17:59:26 +02:00
|
|
|
|
struct FormatOrString
|
|
|
|
|
{
|
|
|
|
|
string s;
|
|
|
|
|
FormatOrString(const string & s) : s(s) { };
|
2020-04-03 17:48:20 +03:00
|
|
|
|
template<class F>
|
|
|
|
|
FormatOrString(const F & f) : s(f.str()) { };
|
2014-03-28 17:59:26 +02:00
|
|
|
|
FormatOrString(const char * s) : s(s) { };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-11-26 01:37:43 +02:00
|
|
|
|
/* 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
|
2016-09-21 17:00:03 +03:00
|
|
|
|
takes place). */
|
|
|
|
|
|
2020-04-03 17:48:20 +03:00
|
|
|
|
template<class F>
|
|
|
|
|
inline void formatHelper(F & f)
|
2019-12-05 18:48:16 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 17:48:20 +03:00
|
|
|
|
template<class F, typename T, typename... Args>
|
|
|
|
|
inline void formatHelper(F & f, const T & x, const Args & ... args)
|
2019-12-05 18:48:16 +02:00
|
|
|
|
{
|
|
|
|
|
formatHelper(f % x, args...);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 17:00:03 +03:00
|
|
|
|
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>
|
2019-12-05 18:39:11 +02:00
|
|
|
|
inline std::string fmt(const std::string & fs, const Args & ... args)
|
2016-09-21 17:00:03 +03:00
|
|
|
|
{
|
|
|
|
|
boost::format f(fs);
|
2017-08-14 23:42:17 +03:00
|
|
|
|
f.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
|
2019-12-05 18:48:16 +02:00
|
|
|
|
formatHelper(f, args...);
|
2016-09-21 17:00:03 +03:00
|
|
|
|
return f.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-08-12 03:29:28 +03:00
|
|
|
|
/* BaseError should generally not be caught, as it has Interrupted as
|
|
|
|
|
a subclass. Catch Error instead. */
|
2014-03-28 17:59:26 +02:00
|
|
|
|
class BaseError : public std::exception
|
2006-09-05 00:06:23 +03:00
|
|
|
|
{
|
|
|
|
|
protected:
|
2009-06-30 16:28:29 +03:00
|
|
|
|
string prefix_; // used for location traces etc.
|
2006-09-05 00:06:23 +03:00
|
|
|
|
string err;
|
|
|
|
|
public:
|
2016-09-21 17:00:03 +03:00
|
|
|
|
unsigned int status = 1; // exit status
|
|
|
|
|
|
|
|
|
|
template<typename... Args>
|
2019-12-05 19:23:32 +02:00
|
|
|
|
BaseError(unsigned int status, const Args & ... args)
|
2016-09-21 17:00:03 +03:00
|
|
|
|
: err(fmt(args...))
|
|
|
|
|
, status(status)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename... Args>
|
2019-12-05 19:23:32 +02:00
|
|
|
|
BaseError(const Args & ... args)
|
2016-09-21 17:00:03 +03:00
|
|
|
|
: err(fmt(args...))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 19:15:50 +03:00
|
|
|
|
#ifdef EXCEPTION_NEEDS_THROW_SPEC
|
2014-10-20 18:33:48 +03:00
|
|
|
|
~BaseError() throw () { };
|
|
|
|
|
const char * what() const throw () { return err.c_str(); }
|
2014-10-20 19:15:50 +03:00
|
|
|
|
#else
|
|
|
|
|
const char * what() const noexcept { return err.c_str(); }
|
|
|
|
|
#endif
|
2016-09-21 17:00:03 +03:00
|
|
|
|
|
2014-07-09 13:14:40 +03:00
|
|
|
|
const string & msg() const { return err; }
|
|
|
|
|
const string & prefix() const { return prefix_; }
|
2014-03-28 17:59:26 +02:00
|
|
|
|
BaseError & addPrefix(const FormatOrString & fs);
|
2006-09-05 00:06:23 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define MakeError(newClass, superClass) \
|
|
|
|
|
class newClass : public superClass \
|
|
|
|
|
{ \
|
|
|
|
|
public: \
|
2016-09-21 17:00:03 +03:00
|
|
|
|
using superClass::superClass; \
|
2019-11-10 18:14:26 +02:00
|
|
|
|
}
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
2019-11-10 18:14:26 +02:00
|
|
|
|
MakeError(Error, BaseError);
|
2007-08-12 03:29:28 +03:00
|
|
|
|
|
|
|
|
|
class SysError : public Error
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
int errNo;
|
2016-09-21 17:00:03 +03:00
|
|
|
|
|
|
|
|
|
template<typename... Args>
|
2019-12-05 19:23:32 +02:00
|
|
|
|
SysError(const Args & ... args)
|
2016-09-21 17:00:03 +03:00
|
|
|
|
: Error(addErrno(fmt(args...)))
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
std::string addErrno(const std::string & s);
|
2007-08-12 03:29:28 +03:00
|
|
|
|
};
|
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
|
|
typedef list<string> Strings;
|
|
|
|
|
typedef set<string> StringSet;
|
2017-04-13 16:55:38 +03:00
|
|
|
|
typedef std::map<std::string, std::string> StringMap;
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Paths are just strings. */
|
|
|
|
|
typedef string Path;
|
|
|
|
|
typedef list<Path> Paths;
|
|
|
|
|
typedef set<Path> PathSet;
|
|
|
|
|
|
2014-03-28 17:59:26 +02:00
|
|
|
|
|
2020-03-30 17:04:18 +03:00
|
|
|
|
/* Helper class to run code at startup. */
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct OnStartup
|
|
|
|
|
{
|
|
|
|
|
OnStartup(T && t) { t(); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
|
}
|