1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * Rappture::Outcome |
---|
4 | * This object represents the result of any Rappture call. It acts |
---|
5 | * like a boolean, so it can be tested for success/failure. But |
---|
6 | * it can also contain information about failure, including a trace |
---|
7 | * back of messages indicating the cause. |
---|
8 | * |
---|
9 | * ====================================================================== |
---|
10 | * AUTHOR: Michael McLennan, Purdue University |
---|
11 | * Copyright (c) 2004-2006 Purdue Research Foundation |
---|
12 | * |
---|
13 | * See the file "license.terms" for information on usage and |
---|
14 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
15 | * ====================================================================== |
---|
16 | */ |
---|
17 | #include "RpOutcome.h" |
---|
18 | |
---|
19 | using namespace Rappture; |
---|
20 | |
---|
21 | Outcome::Outcome(const char *errmsg) |
---|
22 | : _status(0), |
---|
23 | _remarkPtr(NULL), |
---|
24 | _contextPtr(NULL) |
---|
25 | { |
---|
26 | if (errmsg) { |
---|
27 | error(errmsg); |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | Outcome::Outcome(const Outcome& oc) |
---|
32 | : _status(oc._status), |
---|
33 | _remarkPtr(oc._remarkPtr), |
---|
34 | _contextPtr(oc._contextPtr) |
---|
35 | { |
---|
36 | } |
---|
37 | |
---|
38 | Outcome& |
---|
39 | Outcome::operator=(const Outcome& oc) |
---|
40 | { |
---|
41 | _status = oc._status; |
---|
42 | _remarkPtr = oc._remarkPtr; |
---|
43 | _contextPtr = oc._contextPtr; |
---|
44 | return *this; |
---|
45 | } |
---|
46 | |
---|
47 | Outcome& |
---|
48 | Outcome::error(const char* errmsg, int status) |
---|
49 | { |
---|
50 | _status = status; |
---|
51 | _remarkPtr = Ptr<std::string>(new std::string(errmsg)); |
---|
52 | _contextPtr.clear(); |
---|
53 | return *this; |
---|
54 | } |
---|
55 | |
---|
56 | Outcome& |
---|
57 | Outcome::clear() |
---|
58 | { |
---|
59 | _status = 0; |
---|
60 | _remarkPtr.clear(); |
---|
61 | _contextPtr.clear(); |
---|
62 | return *this; |
---|
63 | } |
---|
64 | |
---|
65 | Outcome::operator int() const |
---|
66 | { |
---|
67 | return _status; |
---|
68 | } |
---|
69 | |
---|
70 | int |
---|
71 | Outcome::operator!() const |
---|
72 | { |
---|
73 | return (_status == 0); |
---|
74 | } |
---|
75 | |
---|
76 | Outcome& |
---|
77 | Outcome::operator&=(Outcome oc) |
---|
78 | { |
---|
79 | _status &= oc._status; |
---|
80 | if (!oc._contextPtr.isNull()) { |
---|
81 | _remarkPtr = oc._remarkPtr; |
---|
82 | _contextPtr = oc._contextPtr; |
---|
83 | } |
---|
84 | return *this; |
---|
85 | } |
---|
86 | |
---|
87 | std::string |
---|
88 | Outcome::remark() const |
---|
89 | { |
---|
90 | if (!_remarkPtr.isNull()) { |
---|
91 | return _remarkPtr->data(); |
---|
92 | } |
---|
93 | return ""; |
---|
94 | } |
---|
95 | |
---|
96 | Outcome& |
---|
97 | Outcome::addContext(const char *rem) |
---|
98 | { |
---|
99 | if (_contextPtr.isNull()) { |
---|
100 | _contextPtr = new std::string(); |
---|
101 | } |
---|
102 | _contextPtr->append(rem); |
---|
103 | _contextPtr->append("\n"); |
---|
104 | return *this; |
---|
105 | } |
---|
106 | |
---|
107 | std::string |
---|
108 | Outcome::context() const |
---|
109 | { |
---|
110 | if (!_contextPtr.isNull()) { |
---|
111 | return _contextPtr->data(); |
---|
112 | } |
---|
113 | return ""; |
---|
114 | } |
---|