summaryrefslogtreecommitdiffhomepage
path: root/NewKit/ErrorOr.hpp
blob: 736340c7d35e14d8baa3b7a3c099a0c3c36be63b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
*	========================================================
*
*	hCore
* 	Copyright Mahrouss Logic, all rights reserved.
*
* 	========================================================
*/

#pragma once

#include <NewKit/Defines.hpp>
#include <NewKit/Ref.hpp>

namespace hCore
{
using ErrorT = UInt;

template <typename T> class ErrorOr final
{
 public:
   ErrorOr() = default;
   ~ErrorOr() = default;

 public:
   explicit ErrorOr(Int32 err)
       : mId(err)
   {}

   explicit ErrorOr(nullPtr Null)
   {}

   explicit ErrorOr(T Class)
       : mRef(Class)
   {}

   ErrorOr &operator=(const ErrorOr &) = default;
   ErrorOr(const ErrorOr &) = default;

   ErrorOr &operator=(const Ref<T> &refErr)
   {
       mRef = refErr;
       return *this;
   }

   Ref<T> Leak()
   {
       return mRef;
   }

   Int32  Error()
   {
       return mId;
   }

   operator bool()
   {
       return mRef;
   }

 private:
   Ref<T> mRef;
   Int32 mId{0};
};

using ErrorOrAny = ErrorOr<voidPtr>;

} // namespace hCore