nx_server_plugin_sdk  1.0
Server Plugin 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  needMoreData = -102, //< Operation won't be performed until more data has been provided
22  inProgress = -103, //< Async operation has started. No additional data will be accepted until it finishes
23 };
24 
25 class Error
26 {
27 public:
28  Error(const Error&) = default;
29 
30  Error(ErrorCode errorCode, const IString* errorMessage):
31  m_errorCode(errorCode), m_errorMessage(errorMessage)
32  {
33  }
34 
35  bool isOk() const
36  {
37  return m_errorCode == ErrorCode::noError && m_errorMessage == nullptr;
38  }
39 
40  ErrorCode errorCode() const { return m_errorCode; }
41  const IString* errorMessage() const { return m_errorMessage; }
42 
43  Error(Error&&) = default;
44  Error& operator=(const Error&) = default;
45 
46 private:
47  ErrorCode m_errorCode;
48  const IString* m_errorMessage;
49 };
50 
51 template<typename Value>
52 class Result
53 {
54 public:
55  Result(): m_error(ErrorCode::noError, nullptr) {}
56 
57  Result(Value value): m_error(ErrorCode::noError, nullptr), m_value(std::move(value)) {}
58 
59  Result(Error&& error): m_error(std::move(error)) {}
60 
61  Result& operator=(Error&& error)
62  {
63  m_error = std::move(error);
64  m_value = Value{};
65  return *this;
66  }
67 
68  Result& operator=(Value value)
69  {
70  m_error = Error{ErrorCode::noError, nullptr};
71  m_value = value;
72  return *this;
73  }
74 
75  bool isOk() const { return m_error.isOk(); }
76 
77  const Error& error() const { return m_error; }
78  Value value() const { return m_value; }
79 
80 private:
81  Error m_error;
82  Value m_value{};
83 };
84 
85 template<>
86 class Result<void>
87 {
88 public:
89  Result(): m_error(ErrorCode::noError, nullptr) {}
90 
91  Result(Error&& error): m_error(std::move(error)) {}
92 
93  Result& operator=(Error&& error)
94  {
95  m_error = std::move(error);
96  return *this;
97  }
98 
99  bool isOk() const { return m_error.isOk(); }
100 
101  const Error& error() const { return m_error; }
102 
103 private:
104  Error m_error;
105 };
106 
107 } // namespace nx::sdk
Definition: i_string.h:9
Definition: result.h:52
Definition: device_agent.h:12
Definition: result.h:25