nx_metadata_sdk 1.0 Metadata SDK

result.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #pragma once
4
5 #include <nx/sdk/i_string.h>
6
7 namespace nx {
8 namespace sdk {
9
11 enum class ErrorCode: int
12 {
13 noError = 0,
14 networkError = -22,
15 unauthorized = -1,
16 internalError = -1000, //< Assertion-failure-like error.
17 invalidParams = -1001, //< Method arguments are invalid.
18 notImplemented = -21,
19 otherError = -100,
20 };
21
22 class Error
23 {
24 public:
25 Error(ErrorCode errorCode, const IString* errorMessage):
26 m_errorCode(errorCode), m_errorMessage(errorMessage)
27 {
28 }
29
30 bool isOk() const
31 {
32 return m_errorCode == ErrorCode::noError && m_errorMessage == nullptr;
33 }
34
35 ErrorCode errorCode() const { return m_errorCode; }
36 const IString* errorMessage() const { return m_errorMessage; }
37
38 Error(Error&&) = default;
39 Error& operator=(const Error&) = default;
40
41 private:
42 ErrorCode m_errorCode;
43 const IString* m_errorMessage;
44 };
45
46 template<typename Value>
47 class Result
48 {
49 public:
50 Result(): m_error(ErrorCode::noError, nullptr) {}
51
52 Result(Value value): m_error(ErrorCode::noError, nullptr), m_value(std::move(value)) {}
53
54 Result(Error&& error): m_error(std::move(error)) {}
55
56 Result& operator=(Error&& error)
57 {
58 m_error = std::move(error);
59 m_value = Value{};
60 return *this;
61 }
62
63 Result& operator=(Value value)
64 {
65 m_error = Error{ErrorCode::noError, nullptr};
66 m_value = value;
67 return *this;
68 }
69
70 bool isOk() const { return m_error.isOk(); }
71
72 const Error& error() const { return m_error; }
73 Value value() const { return m_value; }
74
75 private:
76 Error m_error;
77 Value m_value{};
78 };
79
80 template<>
81 class Result<void>
82 {
83 public:
84 Result(): m_error(ErrorCode::noError, nullptr) {}
85
86 Result(Error&& error): m_error(std::move(error)) {}
87
88 Result& operator=(Error&& error)
89 {
90 m_error = std::move(error);
91 return *this;
92 }
93
94 bool isOk() const { return m_error.isOk(); }
95
96 const Error& error() const { return m_error; }
97
98 private:
99 Error m_error;
100 };
101
102 } // namespace sdk
103 } // namespace nx
nx::sdk::IString
Definition: i_string.h:10
nx::sdk::Result
Definition: result.h:47
nx
Definition: apple_utils.h:6
nx::sdk::Error
Definition: result.h:22