source: trunk/src/core/RpOutcome.cc @ 1036

Last change on this file since 1036 was 1018, checked in by gah, 16 years ago

Massive changes: New directory/file layout

File size: 3.3 KB
Line 
1/*
2 * ======================================================================
3 *  Rappture::Outcome
4 *
5 *  AUTHOR:  Michael McLennan, Purdue University
6 *  Copyright (c) 2004-2007  Purdue Research Foundation
7 * ----------------------------------------------------------------------
8 *  See the file "license.terms" for information on usage and
9 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 * ======================================================================
11 */
12#include "RpOutcome.h"
13#include <stdarg.h>
14using namespace Rappture;
15
16/**
17 *  Create a negative outcome, with the given error message.
18 */
19Outcome::Outcome(const char *errmsg) :
20    _status(0),
21    _remarkPtr(NULL),
22    _contextPtr(NULL)
23{
24    if (errmsg) {
25        error(errmsg);
26    }
27}
28
29/// Copy constructor
30Outcome::Outcome(const Outcome& oc) :
31    _status(oc._status),
32    _remarkPtr(oc._remarkPtr),
33    _contextPtr(oc._contextPtr)
34{
35}
36
37/// Assignment operator
38Outcome&
39Outcome::operator=(const Outcome& oc)
40{
41    _status = oc._status;
42    _remarkPtr = oc._remarkPtr;
43    _contextPtr = oc._contextPtr;
44    return *this;
45}
46
47/// Destructor
48Outcome::~Outcome()
49{}
50
51/**
52 *  Assign an error condition to the outcome.
53 */
54Outcome&
55Outcome::error(const char* errmsg, int status)
56{
57    _status = status;
58    _remarkPtr = Ptr<std::string>(new std::string(errmsg));
59    _contextPtr.clear();
60    return *this;
61}
62
63Outcome&
64Outcome::AddError(const char* format, ...)
65{
66    char stackSpace[1024];
67    va_list lst;
68    size_t n;
69    char *bufPtr;
70
71    va_start(lst, format);
72    bufPtr = stackSpace;
73    n = vsnprintf(bufPtr, 1024, format, lst);
74    if (n >= 1024) {
75        bufPtr = (char *)malloc(n);
76        vsnprintf(bufPtr, n, format, lst);
77    }
78    if (_remarkPtr.isNull()) {
79        _remarkPtr = Ptr<std::string>(new std::string(bufPtr));
80    } else {
81        _remarkPtr->append("\n");
82        _remarkPtr->append(bufPtr);
83    }
84    _contextPtr.clear();
85    _status = 1;                /* Set to error */
86    if (bufPtr != stackSpace) {
87        free(bufPtr);
88    }
89    return *this;
90}
91
92/**
93 *  Clear the status of this outcome.
94 */
95Outcome&
96Outcome::clear()
97{
98    _status = 0;
99    _remarkPtr.clear();
100    _contextPtr.clear();
101    return *this;
102}
103
104/**
105 *  Returns the status of this outcome as an integer.
106 *  As in Unix systems, 0 = okay.
107 */
108Outcome::operator int() const
109{
110    return _status;
111}
112
113/**
114 *  For !error tests.
115 */
116int
117Outcome::operator!() const
118{
119    return (_status == 0);
120}
121
122/**
123 *  Use this to concatenate many different outcomes.
124 */
125Outcome&
126Outcome::operator&=(Outcome oc)
127{
128    _status &= oc._status;
129    if (!oc._contextPtr.isNull()) {
130        _remarkPtr = oc._remarkPtr;
131        _contextPtr = oc._contextPtr;
132    }
133    return *this;
134}
135
136/**
137 *  Query the error remark from an outcome.
138 */
139std::string
140Outcome::remark() const
141{
142    if (!_remarkPtr.isNull()) {
143        return _remarkPtr->data();
144    }
145    return "";
146}
147
148/**
149 *  Add information to the context stack for an outcome.
150 */
151Outcome&
152Outcome::addContext(const char *rem)
153{
154    if (_contextPtr.isNull()) {
155        _contextPtr = new std::string();
156    }
157    _contextPtr->append(rem);
158    _contextPtr->append("\n");
159    return *this;
160}
161
162/**
163 *  Query the context stack from an outcome.
164 */
165std::string
166Outcome::context() const
167{
168    if (!_contextPtr.isNull()) {
169        return _contextPtr->data();
170    }
171    return "";
172}
Note: See TracBrowser for help on using the repository browser.