programName as static member var

This commit is contained in:
Ben Burdette 2020-03-25 10:52:03 -06:00
parent fc310eda3a
commit 3582dc3c88
3 changed files with 18 additions and 5 deletions

View file

@ -1,11 +1,15 @@
#include "error.hh" #include "error.hh"
#include <iostream> #include <iostream>
#include <optional>
namespace nix { namespace nix {
using std::cout; using std::cout;
using std::endl; using std::endl;
using std::nullopt;
optional<string> ErrorInfo::programName = nullopt;
// return basic_format? // return basic_format?
string showErrLine(ErrLine &errLine) string showErrLine(ErrLine &errLine)
@ -98,10 +102,11 @@ void print_error(ErrorInfo &einfo)
{ {
level_string = "wat:"; level_string = "wat:";
break; break;
}} }
}
int ndl = level_string.length() + 3 + einfo.name.length() + einfo.program.length(); int ndl = prefix.length() + level_string.length() + 3 + einfo.name.length() + einfo.programName.value_or("").length();
int dashwidth = errwidth - 3 ? 3 : 80 - ndl; int dashwidth = ndl > (errwidth - 3) ? 3 : 80 - ndl;
string dashes; string dashes;
for (int i = 0; i < dashwidth; ++i) for (int i = 0; i < dashwidth; ++i)
@ -114,7 +119,7 @@ void print_error(ErrorInfo &einfo)
% "---" % "---"
% einfo.name % einfo.name
% dashes % dashes
% einfo.program % einfo.programName.value_or("")
<< endl; << endl;
// filename. // filename.

View file

@ -80,11 +80,12 @@ class ErrorInfo {
ErrLevel level; ErrLevel level;
string name; string name;
string description; string description;
string program;
optional<NixCode> nixCode; optional<NixCode> nixCode;
string hint; string hint;
ErrorInfo& GetEI() { return *this; } ErrorInfo& GetEI() { return *this; }
static optional<string> programName;
// give these access to the private constructor, // give these access to the private constructor,
// when they are direct descendants. // when they are direct descendants.
friend AddName<ErrorInfo>; friend AddName<ErrorInfo>;
@ -109,18 +110,21 @@ class ErrorInfo {
}; };
// Init as error
class EIError : public ErrorInfo class EIError : public ErrorInfo
{ {
protected: protected:
EIError() : ErrorInfo(elError) {} EIError() : ErrorInfo(elError) {}
}; };
// Init as warning
class EIWarning : public ErrorInfo class EIWarning : public ErrorInfo
{ {
protected: protected:
EIWarning() : ErrorInfo(elWarning) {} EIWarning() : ErrorInfo(elWarning) {}
}; };
// Builder class definitions.
template <class T> template <class T>
class AddName : private T class AddName : private T
{ {

View file

@ -5,11 +5,15 @@
using std::optional; using std::optional;
using std::nullopt; using std::nullopt;
using std::cout;
using std::endl;
int main() { int main() {
using namespace nix; using namespace nix;
ErrorInfo::programName = optional("errorTest");
/* /*
ColumnRange columnRange; ColumnRange columnRange;
columnRange.start = 24; columnRange.start = 24;