nx_cloud_storage_sdk  1.0
Cloud Storage 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::sdk {
8 
10 enum class ErrorCode: int
11 {
12  noError = 0,
13  networkError = -22,
14  unauthorized = -1,
15  internalError = -1000, //< Assertion-failure-like error.
16  invalidParams = -1001, //< Method arguments are invalid.
17  notImplemented = -21,
18  otherError = -100,
19  ioError = -31,
20  noData = -101, //< Call succeeded, but no valid data can be returned (EoF for example)
21 };
22 
23 class Error
24 {
25 public:
26  Error(ErrorCode errorCode, const IString* errorMessage):
27  m_errorCode(errorCode), m_errorMessage(errorMessage)
28  {
29  }
30 
31  bool isOk() const
32  {
33  return m_errorCode == ErrorCode::noError && m_errorMessage == nullptr;
34  }
35 
36  ErrorCode errorCode() const { return m_errorCode; }
37  const IString* errorMessage() const { return m_errorMessage; }
38 
39  Error(Error&&) = default;
40  Error& operator=(const Error&) = default;
41 
42 private:
43  ErrorCode m_errorCode;
44  const IString* m_errorMessage;
45 };
46 
47 template<typename Value>
48 class Result
49 {
50 public:
51  Result(): m_error(ErrorCode::noError, nullptr) {}
52 
53  Result(Value value): m_error(ErrorCode::noError, nullptr), m_value(std::move(value)) {}
54 
55  Result(Error&& error): m_error(std::move(error)) {}
56 
57  Result& operator=(Error&& error)
58  {
59  m_error = std::move(error);
60  m_value = Value{};
61  return *this;
62  }
63 
64  Result& operator=(Value value)
65  {
66  m_error = Error{ErrorCode::noError, nullptr};
67  m_value = value;
68  return *this;
69  }
70 
71  bool isOk() const { return m_error.isOk(); }
72 
73  const Error& error() const { return m_error; }
74  Value value() const { return m_value; }
75 
76 private:
77  Error m_error;
78  Value m_value{};
79 };
80 
81 template<>
82 class Result<void>
83 {
84 public:
85  Result(): m_error(ErrorCode::noError, nullptr) {}
86 
87  Result(Error&& error): m_error(std::move(error)) {}
88 
89  Result& operator=(Error&& error)
90  {
91  m_error = std::move(error);
92  return *this;
93  }
94 
95  bool isOk() const { return m_error.isOk(); }
96 
97  const Error& error() const { return m_error; }
98 
99 private:
100  Error m_error;
101 };
102 
103 } // namespace nx::sdk
Definition: i_string.h:9
Definition: result.h:48
Definition: result.h:23