| chigraph master Systems programming language written for beginners in LLVM

Result.hpp
Go to the documentation of this file.
1
4 #ifndef CHI_RESULT_HPP
5 #define CHI_RESULT_HPP
6
7 #pragma once
8
9 #include "chi/Support/json.hpp"
10
11 #include <boost/container/flat_map.hpp>
12
13 namespace chi {
72 struct Result {
74 struct ScopedContext {
78 ScopedContext(Result& res, int ctxId) : result{res}, contextId{ctxId} {}
79
81 ~ScopedContext() { result.removeContext(contextId); }
82
84 Result& result;
85
87 const int contextId;
88 };
89
91 Result() : result_json(nlohmann::json::array()) {}
100 void addEntry(const char* ec, const char* overview, nlohmann::json data);
101
107 int addContext(const nlohmann::json& data);
108
123 ScopedContext addScopedContext(const nlohmann::json& data) {
124 return ScopedContext{*this, addContext(data)};
125 }
126
129 void removeContext(int id);
130
133 nlohmann::json contextJson() const;
134
137 explicit operator bool() const { return success(); }
138
141 bool success() const { return mSuccess; }
142
145 bool operator!() const { return !success(); }
146
149 std::string dump() const;
150
152 nlohmann::json result_json;
153
155 bool mSuccess = true;
156
157 private:
158 boost::container::flat_map<int, nlohmann::json> mContexts;
159 };
160
162
165
172 Result operator+(const Result& lhs, const Result& rhs);
173
180 Result& operator+=(Result& lhs, const Result& rhs);
181
187 inline std::ostream& operator<<(std::ostream& lhs, const Result& rhs) {
188 lhs << rhs.dump();
189
190 return lhs;
191 }
192
194
195 } // namespace chi
196
197 #endif // CHI_RESULT_HPP
chi::Result::dump
std::string dump() const
Dump to a pretty-printed error message.
Definition: Result.cpp:65
chi::Result::addEntry
void addEntry(const char *ec, const char *overview, nlohmann::json data)
Add a entry to the result, either a warning or an error.
Definition: Result.cpp:52
chi::Result::ScopedContext::~ScopedContext
~ScopedContext()
Destructor–does the magic of remving itsself from the result.
Definition: Result.hpp:81
chi::Result::contextJson
nlohmann::json contextJson() const
Get the JSON associated with the context that&#39;s been added.
Definition: Result.cpp:95
chi::Result::mSuccess
bool mSuccess
If it&#39;s successful.
Definition: Result.hpp:155
chi::Result::result_json
nlohmann::json result_json
The result JSON.
Definition: Result.hpp:152
chi::Result::ScopedContext::contextId
const int contextId
The ID for this context.
Definition: Result.hpp:87
chi::Result::ScopedContext::result
Result & result
The result object.
Definition: Result.hpp:84
chi::Result::ScopedContext::ScopedContext
ScopedContext(Result &res, int ctxId)
Create a scoped context object.
Definition: Result.hpp:78
chi::Result::addScopedContext
ScopedContext addScopedContext(const nlohmann::json &data)
Add a context with a scope Example usage: chi::Result res; res.contextJson(); // returns {} { aut...
Definition: Result.hpp:123
chi::Result::operator+
Result operator+(const Result &lhs, const Result &rhs)
Append two Result objects.
Definition: Result.cpp:106
chi::Result::addContext
int addContext(const nlohmann::json &data)
Add some json that will ALWAYS be merged with entries that are added and when results are added to th...
Definition: Result.cpp:84
chi::Result::success
bool success() const
Success test.
Definition: Result.hpp:141
chi::Result::operator<<
std::ostream & operator<<(std::ostream &lhs, const Result &rhs)
Stream operator.
Definition: Result.hpp:187
chi::Result::ScopedContext
A helper object for contexts that should be removed at the end of a scope.
Definition: Result.hpp:74
chi::Result::operator+=
Result & operator+=(Result &lhs, const Result &rhs)
Append one result to an existing one.
Definition: Result.cpp:127
chi
The namespace where chigraph lives.
Definition: BitcodeParser.hpp:12
chi::Result::operator!
bool operator!() const
!Success test
Definition: Result.hpp:145
chi::Result::Result
Result()
Default constructor; defaults to success.
Definition: Result.hpp:91
chi::Result
The result object, used for identifiying errors with good diagnostics.
Definition: Result.hpp:72
chi::Result::removeContext
void removeContext(int id)
Removes a previously added context.
Definition: Result.cpp:93