-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoutput.h
More file actions
69 lines (55 loc) · 1.57 KB
/
output.h
File metadata and controls
69 lines (55 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2016 Dino Wernli. All Rights Reserved. See LICENSE for licensing terms.
#ifndef OUTPUT_H_
#define OUTPUT_H_
#include <memory>
#include "error.h"
#include "value.h"
namespace ccproducers {
class OutputBase {
};
// Represents the (immutable) result of running a producer. Contains either a
// value or an error which occurred during execution.
template<class T>
class Output : public OutputBase {
public:
Output(T&& content)
: value_(std::make_unique<Value<T>>(std::move(content))),
error_(nullptr) {}
Output(Error&& error)
: value_(nullptr),
error_(std::make_unique<Error>(std::move(error))) {}
Output(Output<T>&& other)
: value_(std::move(other.value_)),
error_(std::move(other.error_)) {}
~Output() {}
Output<T>& operator=(Output<T>&& other) {
value_ = std::move(other.value_);
error_ = std::move(other.error_);
return *this;
}
bool IsError() const {
return error_.get() != nullptr;
}
bool IsValue() const {
return value_.get() != nullptr;
}
// This must only be called if IsValue() returns true.
const T& get() const {
assert(IsValue());
return value_->get();
}
// Returns an Input instance which points to the result of this output.
Input<T> AsInput() const {
if (IsError()) {
return Input<T>(error_.get());
} else {
return Input<T>(value_.get());
}
}
private:
// Exactly one of these two fields is set for any given instance.
std::unique_ptr<Value<T>> value_;
std::unique_ptr<Error> error_;
};
} // namespace ccproducers
#endif // OUTPUT_H