From 582ce267769b93d03d8cae241abd86a6dfbffaf0 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 30 Jul 2022 12:54:25 +0000 Subject: [PATCH] Add Result class inspired by Rust --- CMakeLists.txt | 1 + src/Result.h | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/Result.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0da7ec7..315f932 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ add_executable( src/sapphirepch.h src/IRBuilder.cpp src/IRBuilder.h + src/Result.h ) target_include_directories(sapphirec PUBLIC src) diff --git a/src/Result.h b/src/Result.h new file mode 100644 index 0000000..21f4204 --- /dev/null +++ b/src/Result.h @@ -0,0 +1,36 @@ +#pragma once +#include "Token.h" + +// Kinda copying Rust here, but it's a great idea. + +template +class Result +{ +public: + Result() = default; + bool is_error() const { return m_is_error; } + void ethrow(); + std::shared_ptr get() { return m_result; } +protected: + std::shared_ptr m_token; + std::shared_ptr 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 +class Ok : public Result +{ +public: + Ok(T* result, Token* token) : Result(result,token) {} +}; + +template +class Err : public Result +{ +public: + Err(const std::string& error, Token* token) : Result(std::move(error),token) {} +}; \ No newline at end of file