Skip to content

Neko-Box-Coder/DSResult

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dead Simple Result With Error Tracing

A single header only simple C++11 struct for allowing golang/rust like error handling and tracing with expected like container

Supports tl::expected, expected lite, std expected or custom expected like container.

Just add Include to your project include path and do #include "DSResult/DSResult.hpp".

By default it uses tl::expected, if nothing is instructed. Define the following macro to choose a different backend

#define DS_USE_TL_EXPECTED 1
#define DS_USE_EXPECTED_LITE 1
#define DS_USE_STD_EXPECTED 1
#define DS_USE_CUSTOM_EXPECTED 1

If you are using a custom expected like container, you need to define the macros DS_EXPECTED_TYPE and DS_UNEXPECTED_TYPE. For example,

#define DS_EXPECTED_TYPE MyNamespace::MyExpected        //MyNamespace::MyExpected<T, E>
#define DS_UNEXPECTED_TYPE MyNamespace::MyUnexpected    //MyNamespace::MyUnexpected<E>

Usage

Type Definitions

template<typename T>
using Result = DS_EXPECTED_TYPE<T, DS::ErrorTrace>;
using Error = DS_UNEXPECTED_TYPE<DS::ErrorTrace>;

See the respective expected and unexpected type on how to use them, but mainly the following

  • bool DS::Result<T>::has_value()
  • T& DS::Result<T>::value()
  • DS::Error DS::Result<T>::error()

Function Declaration that uses DS::Result

DS::Result<int> MyFunction(...);

Returning an error message

int myValue;
return DS::Error(DS_ERROR_MSG("Something wrong: " + DS_STR(myValue)));

Return error if assertion fails

std::vector<int> myData;
DS_ASSERT_RETURN(!myData.empty());  //Returns DS::Error if `myData.empty()` is true

Check result, append to trace and return if there's an error

DS::Result<void> MyVoidFunction()
{
    DS::Result<int> functionResult = MyFunction();
    DS_CHECKED_RETURN(functionResult);
    int myInt = functioonResult.value();
    return {};
}

Get the error trace if a function failed

#include <iostream>
int main()
{
    DS::Result<void> result = MyFunction();
    if(!result.has_value())
    {
        DS::ErrorTrace errorTrace = DS_APPEND_TRACE(result.error());
        //Error:
        //  ...
        //
        //Stack trace
        //  at ...
        //  at ...
        //  ...
        std::cout << errorTrace.ToString() << std::endl;
        return 1;
    }
    return 0;
}

About

A simple struct for allowing golang or rust like error handling

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published