source: trunk/src2/core/Outcome.cpp @ 1016

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