C++ Run-time Library
Loading...
Searching...
No Matches
status.h
Go to the documentation of this file.
1// Copyright 2018 Delft University of Technology
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <fletcher/fletcher.h>
18#include <cstdlib>
19#include <string>
20#include <iostream>
21
22namespace fletcher {
23
25struct Status {
27 fstatus_t val = FLETCHER_STATUS_ERROR;
29 std::string message;
30
32 explicit Status(const fstatus_t val, std::string msg) : val(val), message(std::move(msg)) {}
33 explicit Status(const fstatus_t val = FLETCHER_STATUS_ERROR) : val(val) {}
34
36 [[nodiscard]] bool ok() const { return val == FLETCHER_STATUS_OK; }
37
39 void ewf(const std::string &msg = "") const {
40 if (!ok()) {
41 if (!msg.empty()) {
42 std::cerr << msg << std::endl;
43 } else {
44 std::cerr << message << std::endl;
45 }
46 exit(EXIT_FAILURE);
47 }
48 }
49
51 bool operator==(const Status &rhs) const {
52 return val == rhs.val;
53 }
54
56 static Status OK() { return Status(FLETCHER_STATUS_OK); }
57
59 static Status ERROR(std::string msg = "") {
60 return Status(FLETCHER_STATUS_ERROR, std::move(msg));
61 }
62
63 // Helper macro for error states.
64#define STATUS_FACTORY(RAW_ID, MESSAGE) \
65 static Status RAW_ID() { \
66 return Status(FLETCHER_STATUS_##RAW_ID, MESSAGE); \
67 }
68
69 // Other error states:
70 STATUS_FACTORY(NO_PLATFORM, "Could not detect platform.")
71 STATUS_FACTORY(DEVICE_OUT_OF_MEMORY, "Device out of memory.")
72};
73
74} // namespace fletcher
Contains all Fletcher classes and functions for use in run-time applications.
Definition api.h:34
#define STATUS_FACTORY(RAW_ID, MESSAGE)
Definition status.h:64
Status return value of all Fletcher run-time functions.
Definition status.h:25
Status(const fstatus_t val=FLETCHER_STATUS_ERROR)
Definition status.h:33
fstatus_t val
The raw status value.
Definition status.h:27
bool operator==(const Status &rhs) const
Compare raw status values for equality.
Definition status.h:51
static Status ERROR(std::string msg="")
Return an ERROR status with some message.
Definition status.h:59
bool ok() const
Return true if the status is OK.
Definition status.h:36
void ewf(const std::string &msg="") const
Exit the program on a failure status, with some message. If no message is supplied,...
Definition status.h:39
static Status OK()
Return an OK status.
Definition status.h:56
std::string message
Optional message.
Definition status.h:29
Status(const fstatus_t val, std::string msg)
Construct a new status.
Definition status.h:32