FluxioResult.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <stdexcept>
5
6namespace armarx
7{
8 namespace skills
9 {
10
11 template <typename T, typename E = std::exception>
12 class Result
13 {
14 private:
15 const bool success;
16 const std::shared_ptr<const T> result;
17 const std::shared_ptr<const E> error;
18
19 public:
20 Result(const T& res) :
21 success(true), result(std::make_shared<const T>(res)), error(nullptr)
22 {
23 }
24
25 Result(const E& err) :
26 success(false), result(nullptr), error(std::make_shared<const E>(err))
27 {
28 }
29
30 bool
31 isSuccess() const
32 {
33 return success;
34 }
35
36 T
37 getResult() const
38 {
39 if (!success)
40 {
41 throw std::logic_error("Result is not successful");
42 }
43 return *result;
44 }
45
46 E
47 getError() const
48 {
49 if (success)
50 {
51 throw std::logic_error("Result does not contain an error");
52 }
53 return *error;
54 }
55 };
56 } // namespace skills
57} // namespace armarx
This file is part of ArmarX.
This file offers overloads of toIce() and fromIce() functions for STL container types.
auto make_shared(Args &&... args)