switch to one level of builder function, not subobject functions

This commit is contained in:
Ben Burdette 2020-03-24 14:24:57 -06:00
parent 657c08c852
commit fc310eda3a
2 changed files with 112 additions and 132 deletions

View file

@ -23,17 +23,6 @@ class ColumnRange {
class ErrorInfo; class ErrorInfo;
// -------------------------------------------------
// forward declarations before ErrLine.
template <class T>
class AddLineNumber;
template <class T>
class AddColumnRange;
template <class T>
class AddLOC;
class ErrLine { class ErrLine {
public: public:
int lineNumber; int lineNumber;
@ -42,107 +31,25 @@ class ErrLine {
string errLineOfCode; string errLineOfCode;
optional<string> nextLineOfCode; optional<string> nextLineOfCode;
friend AddLineNumber<ErrLine>;
friend AddColumnRange<ErrLine>;
friend AddLOC<ErrLine>;
ErrLine& GetEL() { return *this; }
private:
ErrLine() {}
}; };
template <class T>
class AddLineNumber : public T
{
public:
T& lineNumber(int lineNumber) {
GetEL().lineNumber = lineNumber;
return *this;
}
protected:
ErrLine& GetEL() { return T::GetEL(); }
};
template <class T>
class AddColumnRange : public T
{
public:
T& columnRange(unsigned int start, unsigned int len) {
GetEL().columnRange = { start, len };
return *this;
}
protected:
ErrLine& GetEL() { return T::GetEL(); }
};
template <class T>
class AddLOC : public T
{
public:
T& linesOfCode(optional<string> prevloc, string loc, optional<string> nextloc) {
GetEL().prevLineOfCode = prevloc;
GetEL().errLineOfCode = loc;
GetEL().nextLineOfCode = nextloc;
return *this;
}
protected:
ErrLine& GetEL() { return T::GetEL(); }
};
typedef AddLineNumber<AddColumnRange<AddLOC<ErrLine>>> MkErrLine;
MkErrLine mkErrLine;
// -------------------------------------------------
// NixCode.
template <class T>
class AddNixFile;
template <class T>
class AddErrLine;
class NixCode { class NixCode {
public: public:
optional<string> nixFile; optional<string> nixFile;
optional<ErrLine> errLine; optional<ErrLine> errLine;
friend AddNixFile<NixCode>; ErrLine& ensureErrLine()
friend AddErrLine<NixCode>; {
friend ErrorInfo; if (!this->errLine.has_value())
NixCode& GetNC() { return *this; } this->errLine = optional(ErrLine());
private: return *this->errLine;
NixCode() {}
};
template <class T>
class AddNixFile : public T
{
public:
T& nixFile(string filename) {
GetNC().nixFile = filename;
return *this;
} }
protected:
NixCode& GetNC() { return T::GetNC(); }
}; };
template <class T>
class AddErrLine : public T
{
public:
T& errLine(ErrLine errline) {
GetNC().errLine = errline;
return *this;
}
protected:
NixCode& GetNC() { return T::GetNC(); }
};
typedef AddNixFile<AddErrLine<NixCode>> MkNixCode;
// ------------------------------------------------- // -------------------------------------------------
// ErrorInfo. // ErrorInfo.
// Forward friend class declarations. "builder classes"
template <class T> template <class T>
class AddName; class AddName;
@ -152,6 +59,22 @@ class AddDescription;
template <class T> template <class T>
class AddNixCode; class AddNixCode;
template <class T>
class AddNixFile;
template <class T>
class AddErrLine;
template <class T>
class AddLineNumber;
template <class T>
class AddColumnRange;
template <class T>
class AddLOC;
// The error info class itself.
class ErrorInfo { class ErrorInfo {
public: public:
ErrLevel level; ErrLevel level;
@ -167,9 +90,23 @@ class ErrorInfo {
friend AddName<ErrorInfo>; friend AddName<ErrorInfo>;
friend AddDescription<ErrorInfo>; friend AddDescription<ErrorInfo>;
friend AddNixCode<ErrorInfo>; friend AddNixCode<ErrorInfo>;
friend AddNixFile<ErrorInfo>;
friend AddErrLine<ErrorInfo>;
friend AddLineNumber<ErrorInfo>;
friend AddColumnRange<ErrorInfo>;
friend AddLOC<ErrorInfo>;
NixCode& ensureNixCode()
{
if (!this->nixCode.has_value())
this->nixCode = optional(NixCode());
return *this->nixCode;
}
protected: protected:
// constructor is protected, so only the builder classes can create an ErrorInfo.
ErrorInfo(ErrLevel level) { this->level = level; } ErrorInfo(ErrLevel level) { this->level = level; }
}; };
class EIError : public ErrorInfo class EIError : public ErrorInfo
@ -208,23 +145,73 @@ class AddDescription : private T
ErrorInfo& GetEI() { return T::GetEI(); } ErrorInfo& GetEI() { return T::GetEI(); }
}; };
template <class T> template <class T>
class AddNixCode : private T class AddNixFile : public T
{ {
public: public:
T& nixcode(const NixCode &nixcode){ T& nixFile(string filename) {
GetEI().nixCode = nixcode; GetEI().ensureNixCode().nixFile = filename;
return *this;
}
protected:
ErrorInfo& GetEI() { return T::GetEI(); }
};
template <class T>
class AddLineNumber : public T
{
public:
T& lineNumber(int lineNumber) {
GetEI().ensureNixCode().ensureErrLine().lineNumber = lineNumber;
return *this; return *this;
} }
protected: protected:
ErrorInfo& GetEI() { return T::GetEI(); } ErrorInfo& GetEI() { return T::GetEI(); }
}; };
template <class T>
class AddColumnRange : public T
{
public:
T& columnRange(unsigned int start, unsigned int len) {
GetEI().ensureNixCode().ensureErrLine().columnRange = { start, len };
return *this;
}
protected:
ErrorInfo& GetEI() { return T::GetEI(); }
};
template <class T>
class AddLOC : public T
{
public:
T& linesOfCode(optional<string> prevloc, string loc, optional<string> nextloc) {
GetEI().ensureNixCode().ensureErrLine().prevLineOfCode = prevloc;
GetEI().ensureNixCode().ensureErrLine().errLineOfCode = loc;
GetEI().ensureNixCode().ensureErrLine().nextLineOfCode = nextloc;
return *this;
}
protected:
ErrorInfo& GetEI() { return T::GetEI(); }
};
typedef AddNixFile<AddErrLine<NixCode>> MkNixCode;
typedef AddName<AddDescription<EIError>> StandardError; typedef AddName<AddDescription<EIError>> StandardError;
typedef AddName<AddDescription<EIWarning>> StandardWarning; typedef AddName<AddDescription<EIWarning>> StandardWarning;
typedef AddName<AddDescription<AddNixCode<EIError>>> MkNixError; typedef AddName<
typedef AddName<AddDescription<AddNixCode<EIWarning>>> MkNixWarning; AddDescription<
AddNixFile<
AddLineNumber<
AddColumnRange<
AddLOC<EIError>>>>>> MkNixError;
typedef AddName<
AddDescription<
AddNixFile<
AddLineNumber<
AddColumnRange<
AddLOC<EIWarning>>>>>> MkNixWarning;
string showErrLine(ErrLine &errLine); string showErrLine(ErrLine &errLine);

View file

@ -34,41 +34,34 @@ using namespace nix;
print_error(generic); print_error(generic);
*/ */
StandardError standardError; print_error(StandardError()
print_error(standardError
.name("name") .name("name")
.description("description")); .description("description"));
StandardWarning standardWarning; print_error(StandardWarning()
print_error(standardWarning
.name("warning name") .name("warning name")
.description("warning description")); .description("warning description"));
print_error(MkNixError()
.name("name")
.description("description")
.nixcode(
MkNixCode()
.nixFile("myfile.nix")
.errLine(MkErrLine().lineNumber(40)
.columnRange(13,7)
.linesOfCode(nullopt
,"this is the problem line of code"
,nullopt))));
print_error(MkNixWarning() print_error(MkNixWarning()
.name("name") .name("warning name")
.description("description") .description("warning description")
.nixcode( .nixFile("myfile.nix")
MkNixCode() .lineNumber(40)
.nixFile("myfile.nix") .columnRange(13,7)
.errLine(MkErrLine().lineNumber(40) .linesOfCode(nullopt
.columnRange(13,7) ,"this is the problem line of code"
.linesOfCode(nullopt ,nullopt));
,"this is the problem line of code"
,nullopt)))); print_error(MkNixError()
.name("error name")
.description("warning description")
.nixFile("myfile.nix")
.lineNumber(40)
.columnRange(13,7)
.linesOfCode(nullopt
,"this is the problem line of code"
,nullopt));
return 0; return 0;
} }