Browse Source

Result

master
Robin Thoni 8 years ago
parent
commit
8ac1ebba6e
2 changed files with 69 additions and 54 deletions
  1. 29
    23
      src/DBO/Result.h
  2. 40
    31
      src/DBO/Result.hxx

+ 29
- 23
src/DBO/Result.h View File

2
 // Created by robin on 8/9/15.
2
 // Created by robin on 8/9/15.
3
 //
3
 //
4
 
4
 
5
-#ifndef PDNS_SLAVE_RESULT_H
6
-#define PDNS_SLAVE_RESULT_H
5
+#ifndef RESULT_H
6
+#define RESULT_H
7
 
7
 
8
 # include <string>
8
 # include <string>
9
 # include <vector>
9
 # include <vector>
10
+# include <iostream>
10
 
11
 
11
 template <class T> class Result
12
 template <class T> class Result
12
 {
13
 {
13
 public:
14
 public:
14
-    Result();
15
-
16
-    Result(const T& data);
17
-
18
-    virtual ~Result();
19
-
20
-    Result<T>& ok(const T& data);
15
+    static const Result<T> ok(const T& data);
16
+    static const Result<T> strerror();
17
+    static const Result<T> error(const std::string& error);
18
+    template <class U> static const Result<T> error(const Result<U>& other);
21
 
19
 
22
     T& getData();
20
     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
-
21
+    const bool isSuccess() const;
32
     const std::string& getError() const;
22
     const std::string& getError() const;
33
 
23
 
34
     bool operator !() const;
24
     bool operator !() const;
35
-
36
     operator bool() const;
25
     operator bool() const;
37
 
26
 
27
+    template<class U>
28
+    friend std::ostream& operator<<(std::ostream& os, const Result<U>& res);
29
+
38
     const Result<T>& print() const;
30
     const Result<T>& print() const;
39
 
31
 
40
 private:
32
 private:
41
-    T _data;
33
+    Result();
42
 
34
 
43
-    std::vector<std::string> _warnings;
35
+    T _data;
44
 
36
 
45
     std::string _error;
37
     std::string _error;
38
+
39
+    bool _success;
46
 };
40
 };
47
 
41
 
48
-typedef Result<bool> BResult;
42
+typedef Result<bool> ResultBool;
43
+
44
+typedef Result<int> ResultInt;
45
+
46
+typedef Result<long> ResultLong;
47
+
48
+typedef Result<float> ResultFloat;
49
+
50
+typedef Result<double> ResultDouble;
51
+
52
+typedef Result<char> ResultChar;
53
+
54
+typedef Result<std::string> ResultString;
49
 
55
 
50
 # include "DBO/Result.hxx"
56
 # include "DBO/Result.hxx"
51
 
57
 
52
-#endif //PDNS_SLAVE_RESULT_H
58
+#endif //RESULT_H

+ 40
- 31
src/DBO/Result.hxx View File

2
 // Created by robin on 8/9/15.
2
 // Created by robin on 8/9/15.
3
 //
3
 //
4
 
4
 
5
-#include <iostream>
6
 #include "Result.h"
5
 #include "Result.h"
6
+#include <errno.h>
7
+#include <string.h>
7
 
8
 
8
 template<class T>
9
 template<class T>
9
 Result<T>::Result()
10
 Result<T>::Result()
11
 }
12
 }
12
 
13
 
13
 template<class T>
14
 template<class T>
14
-Result<T>::Result(const T &data)
15
-        : _data(data)
15
+const Result<T> Result<T>::ok(const T& data)
16
 {
16
 {
17
+    Result<T> r;
18
+    r._success = true;
19
+    r._data = data;
20
+    return r;
17
 }
21
 }
18
 
22
 
19
 template<class T>
23
 template<class T>
20
-Result<T>::~Result()
24
+const Result<T> Result<T>::strerror()
21
 {
25
 {
26
+    Result<T> r;
27
+    r._success = false;
28
+    r._error = ::strerror(errno);
29
+    return r;
22
 }
30
 }
23
 
31
 
24
 template<class T>
32
 template<class T>
25
-Result<T> &Result<T>::ok(const T &data)
33
+const Result<T> Result<T>::error(const std::string& error)
26
 {
34
 {
27
-    _data = data;
28
-    return *this;
35
+    Result<T> r;
36
+    r._success = false;
37
+    r._error = error;
38
+    return r;
29
 }
39
 }
30
 
40
 
31
 template<class T>
41
 template<class T>
32
-T &Result<T>::getData()
42
+template<class U>
43
+const Result<T> Result<T>::error(const Result<U>& other)
33
 {
44
 {
34
-    return _data;
45
+    Result<T> r;
46
+    r._success = false;
47
+    r.error(other.getError());
48
+    return r;
35
 }
49
 }
36
 
50
 
37
 template<class T>
51
 template<class T>
38
-bool Result<T>::isSuccess() const
52
+T &Result<T>::getData()
39
 {
53
 {
40
-    return _error.empty();
54
+    return _data;
41
 }
55
 }
42
 
56
 
43
 template<class T>
57
 template<class T>
44
-const std::vector<std::string>& Result<T>::getWarnings() const
58
+const bool Result<T>::isSuccess() const
45
 {
59
 {
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;
60
+    return _success;
54
 }
61
 }
55
 
62
 
56
 template<class T>
63
 template<class T>
66
 }
73
 }
67
 
74
 
68
 template<class T>
75
 template<class T>
69
-Result<T> &Result<T>::error(const std::string &error)
76
+const std::string &Result<T>::getError() const
70
 {
77
 {
71
-    _error = error;
72
-    return *this;
78
+    return _error;
73
 }
79
 }
74
 
80
 
75
-template<class T>
76
-const std::string &Result<T>::getError() const
81
+template<class U>
82
+std::ostream& operator<<(std::ostream& os, const Result<U>& res)
77
 {
83
 {
78
-    return _error;
84
+    if (res._success) {
85
+        os << "Success";// << res._data;
86
+    }
87
+    else {
88
+        os << "Error: " << (res._error.empty() ? "Unknown error" : res._error);
89
+    }
90
+    return os;
79
 }
91
 }
80
 
92
 
81
 template<class T>
93
 template<class T>
82
 const Result<T>& Result<T>::print() const
94
 const Result<T>& Result<T>::print() const
83
 {
95
 {
84
-    for (auto warning : _warnings)
85
-        std::cerr << "WARNING: " << warning << std::endl;
86
-    if (!isSuccess())
87
-        std::cerr << "ERROR: " << _error << std::endl;
96
+    (_success ? std::cout : std::cerr) << *this << std::endl;
88
     return *this;
97
     return *this;
89
-}
98
+}

Loading…
Cancel
Save