Add Result class inspired by Rust
This commit is contained in:
parent
1827b27996
commit
582ce26776
@ -56,6 +56,7 @@ add_executable(
|
|||||||
src/sapphirepch.h
|
src/sapphirepch.h
|
||||||
src/IRBuilder.cpp
|
src/IRBuilder.cpp
|
||||||
src/IRBuilder.h
|
src/IRBuilder.h
|
||||||
|
src/Result.h
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(sapphirec PUBLIC src)
|
target_include_directories(sapphirec PUBLIC src)
|
||||||
|
36
src/Result.h
Normal file
36
src/Result.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Token.h"
|
||||||
|
|
||||||
|
// Kinda copying Rust here, but it's a great idea.
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class Result
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Result() = default;
|
||||||
|
bool is_error() const { return m_is_error; }
|
||||||
|
void ethrow();
|
||||||
|
std::shared_ptr<T> get() { return m_result; }
|
||||||
|
protected:
|
||||||
|
std::shared_ptr<Token> m_token;
|
||||||
|
std::shared_ptr<T> m_result;
|
||||||
|
bool m_is_error;
|
||||||
|
std::string m_error;
|
||||||
|
|
||||||
|
Result(T* result, Token* token) : m_result(result), m_token(token) {}
|
||||||
|
Result(const std::string& error, Token* token) : m_error(std::move(error)), m_token(token) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class Ok : public Result<T>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Ok(T* result, Token* token) : Result<T>(result,token) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class Err : public Result<T>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Err(const std::string& error, Token* token) : Result<T>(std::move(error),token) {}
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user