浏览代码

result dbo

develop
Robin Thoni 9 年前
父节点
当前提交
c950781c20
共有 2 个文件被更改,包括 141 次插入0 次删除
  1. 52
    0
      src/DBO/Result.h
  2. 89
    0
      src/DBO/Result.hxx

+ 52
- 0
src/DBO/Result.h 查看文件

@@ -0,0 +1,52 @@
1
+//
2
+// Created by robin on 8/9/15.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_RESULT_H
6
+#define PDNS_SLAVE_RESULT_H
7
+
8
+# include <string>
9
+# include <vector>
10
+
11
+template <class T> class Result
12
+{
13
+public:
14
+    Result();
15
+
16
+    Result(const T& data);
17
+
18
+    virtual ~Result();
19
+
20
+    Result<T>& ok(const T& data);
21
+
22
+    T& getData();
23
+
24
+    bool isSuccess() const;
25
+
26
+    Result<T>& addWarning(const std::string& warning);
27
+
28
+    const std::vector<std::string>& getWarnings() const;
29
+
30
+    Result<T>& error(const std::string& error);
31
+
32
+    const std::string& getError() const;
33
+
34
+    bool operator !() const;
35
+
36
+    operator bool() const;
37
+
38
+    const Result<T>& print() const;
39
+
40
+private:
41
+    T _data;
42
+
43
+    std::vector<std::string> _warnings;
44
+
45
+    std::string _error;
46
+};
47
+
48
+typedef Result<bool> BResult;
49
+
50
+# include "DBO/Result.hxx"
51
+
52
+#endif //PDNS_SLAVE_RESULT_H

+ 89
- 0
src/DBO/Result.hxx 查看文件

@@ -0,0 +1,89 @@
1
+//
2
+// Created by robin on 8/9/15.
3
+//
4
+
5
+#include <iostream>
6
+#include "Result.h"
7
+
8
+template<class T>
9
+Result<T>::Result()
10
+{
11
+}
12
+
13
+template<class T>
14
+Result<T>::Result(const T &data)
15
+        : _data(data)
16
+{
17
+}
18
+
19
+template<class T>
20
+Result<T>::~Result()
21
+{
22
+}
23
+
24
+template<class T>
25
+Result<T> &Result<T>::ok(const T &data)
26
+{
27
+    _data = data;
28
+    return *this;
29
+}
30
+
31
+template<class T>
32
+T &Result<T>::getData()
33
+{
34
+    return _data;
35
+}
36
+
37
+template<class T>
38
+bool Result<T>::isSuccess() const
39
+{
40
+    return _error.empty();
41
+}
42
+
43
+template<class T>
44
+const std::vector<std::string>& Result<T>::getWarnings() const
45
+{
46
+    return _warnings;
47
+}
48
+
49
+template<class T>
50
+Result<T>& Result<T>::addWarning(const std::string &warning)
51
+{
52
+    _warnings.push_back(warning);
53
+    return *this;
54
+}
55
+
56
+template<class T>
57
+bool Result<T>::operator!() const
58
+{
59
+    return !isSuccess();
60
+}
61
+
62
+template<class T>
63
+Result<T>::operator bool() const
64
+{
65
+    return isSuccess();
66
+}
67
+
68
+template<class T>
69
+Result<T> &Result<T>::error(const std::string &error)
70
+{
71
+    _error = error;
72
+    return *this;
73
+}
74
+
75
+template<class T>
76
+const std::string &Result<T>::getError() const
77
+{
78
+    return _error;
79
+}
80
+
81
+template<class T>
82
+const Result<T>& Result<T>::print() const
83
+{
84
+    for (auto warning : _warnings)
85
+        std::cerr << "WARNING: " << warning << std::endl;
86
+    if (!isSuccess())
87
+        std::cerr << "ERROR: " << _error << std::endl;
88
+    return *this;
89
+}

正在加载...
取消
保存