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

Last change on this file since 1051 was 1051, checked in by dkearney, 15 years ago

templatized the rappture simple buffer object to work with ints, floats, chars, doubles.
adjusted rappture buffer and encode to reflect the changes to the simple buffer.
created a basic dx writer object, currently it handles some uniform rectangular grids with data.
added some c header files to rappture outcome object to stop it from complaining about function headers.
adjusted Makefile to reflect changes to the simple buffer and dx writer object.

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