Bladeren bron

Result DBO

develop
Robin Thoni 8 jaren geleden
bovenliggende
commit
9aa25b3ebf
4 gewijzigde bestanden met toevoegingen van 61 en 64 verwijderingen
  1. 23
    21
      src/DBO/Result.h
  2. 37
    35
      src/DBO/Result.hxx
  3. 0
    5
      src/DataAccess/Pcap.cpp
  4. 1
    3
      src/DataAccess/Pcap.h

+ 23
- 21
src/DBO/Result.h Bestand weergeven

@@ -7,45 +7,47 @@
7 7
 
8 8
 # include <string>
9 9
 # include <vector>
10
+# include <iostream>
10 11
 
11 12
 template <class T> class Result
12 13
 {
13 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 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 22
     const std::string& getError() const;
33 23
 
34 24
     bool operator !() const;
35
-
36 25
     operator bool() const;
37 26
 
38
-    const Result<T>& print() const;
27
+    template<class U>
28
+    friend std::ostream& operator<<(std::ostream& os, const Result<U>& res);
39 29
 
40 30
 private:
41
-    T _data;
31
+    Result();
42 32
 
43
-    std::vector<std::string> _warnings;
33
+    T _data;
44 34
 
45 35
     std::string _error;
36
+
37
+    bool _success;
46 38
 };
47 39
 
48
-typedef Result<bool> BResult;
40
+typedef Result<bool> ResultBool;
41
+
42
+typedef Result<int> ResultInt;
43
+
44
+typedef Result<long> ResultLong;
45
+
46
+typedef Result<float> ResultFloat;
47
+
48
+typedef Result<double> ResultDouble;
49
+
50
+typedef Result<char> ResultChar;
49 51
 
50 52
 # include "DBO/Result.hxx"
51 53
 

+ 37
- 35
src/DBO/Result.hxx Bestand weergeven

@@ -2,8 +2,9 @@
2 2
 // Created by robin on 8/9/15.
3 3
 //
4 4
 
5
-#include <iostream>
6 5
 #include "Result.h"
6
+#include <errno.h>
7
+#include <string.h>
7 8
 
8 9
 template<class T>
9 10
 Result<T>::Result()
@@ -11,46 +12,52 @@ Result<T>::Result()
11 12
 }
12 13
 
13 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 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 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 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 51
 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
52
+T &Result<T>::getData()
45 53
 {
46
-    return _warnings;
54
+    return _data;
47 55
 }
48 56
 
49 57
 template<class T>
50
-Result<T>& Result<T>::addWarning(const std::string &warning)
58
+const bool Result<T>::isSuccess() const
51 59
 {
52
-    _warnings.push_back(warning);
53
-    return *this;
60
+    return _success;
54 61
 }
55 62
 
56 63
 template<class T>
@@ -65,25 +72,20 @@ Result<T>::operator bool() const
65 72
     return isSuccess();
66 73
 }
67 74
 
68
-template<class T>
69
-Result<T> &Result<T>::error(const std::string &error)
70
-{
71
-    _error = error;
72
-    return *this;
73
-}
74
-
75 75
 template<class T>
76 76
 const std::string &Result<T>::getError() const
77 77
 {
78 78
     return _error;
79 79
 }
80 80
 
81
-template<class T>
82
-const Result<T>& Result<T>::print() const
81
+template<class U>
82
+std::ostream& operator<<(std::ostream& os, const Result<U>& res)
83 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
-}
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;
91
+}

+ 0
- 5
src/DataAccess/Pcap.cpp Bestand weergeven

@@ -7,8 +7,3 @@
7 7
 Pcap::Pcap(const std::string &device)
8 8
 {
9 9
 }
10
-
11
-void Pcap::test()
12
-{
13
-
14
-}

+ 1
- 3
src/DataAccess/Pcap.h Bestand weergeven

@@ -5,15 +5,13 @@
5 5
 #ifndef ROZITM_PCAP_H
6 6
 #define ROZITM_PCAP_H
7 7
 
8
-
9 8
 #include <string>
9
+#include "DBO/Result.h"
10 10
 
11 11
 class Pcap
12 12
 {
13 13
 public:
14 14
     Pcap(const std::string& device);
15
-
16
-    void test();
17 15
 };
18 16
 
19 17
 

Laden…
Annuleren
Opslaan