tempoch-cpp
Header-only C++ wrapper for tempoch
Loading...
Searching...
No Matches
ffi_core.hpp
Go to the documentation of this file.
1#pragma once
2
11#include <stdexcept>
12#include <string>
13
14extern "C" {
15#include "tempoch_ffi.h"
16}
17
18namespace tempoch {
19
20// ============================================================================
21// Exception Hierarchy
22// ============================================================================
23
27class TempochException : public std::runtime_error {
28public:
29 explicit TempochException(const std::string& msg) : std::runtime_error(msg) {}
30};
31
36public:
37 explicit NullPointerError(const std::string& msg) : TempochException(msg) {}
38};
39
44public:
45 explicit UtcConversionError(const std::string& msg) : TempochException(msg) {}
46};
47
52public:
53 explicit InvalidPeriodError(const std::string& msg) : TempochException(msg) {}
54};
55
60public:
61 explicit NoIntersectionError(const std::string& msg) : TempochException(msg) {}
62};
63
64// ============================================================================
65// Error Translation
66// ============================================================================
67
71inline void check_status(tempoch_status_t status, const char* operation) {
72 if (status == TEMPOCH_STATUS_T_OK) return;
73
74 std::string msg = std::string(operation) + " failed: ";
75 switch (status) {
76 case TEMPOCH_STATUS_T_NULL_POINTER:
77 throw NullPointerError(msg + "null output pointer");
78 case TEMPOCH_STATUS_T_UTC_CONVERSION_FAILED:
79 throw UtcConversionError(msg + "UTC conversion failed");
80 case TEMPOCH_STATUS_T_INVALID_PERIOD:
81 throw InvalidPeriodError(msg + "invalid period (start > end)");
82 case TEMPOCH_STATUS_T_NO_INTERSECTION:
83 throw NoIntersectionError(msg + "periods do not intersect");
84 default:
85 throw TempochException(msg + "unknown error (" + std::to_string(status) + ")");
86 }
87}
88
89} // namespace tempoch
The period is invalid (start > end).
Definition ffi_core.hpp:51
InvalidPeriodError(const std::string &msg)
Definition ffi_core.hpp:53
The two periods do not intersect.
Definition ffi_core.hpp:59
NoIntersectionError(const std::string &msg)
Definition ffi_core.hpp:61
A required output pointer was null.
Definition ffi_core.hpp:35
NullPointerError(const std::string &msg)
Definition ffi_core.hpp:37
Base exception for all tempoch errors.
Definition ffi_core.hpp:27
TempochException(const std::string &msg)
Definition ffi_core.hpp:29
UTC conversion failed (date out of range or invalid).
Definition ffi_core.hpp:43
UtcConversionError(const std::string &msg)
Definition ffi_core.hpp:45
void check_status(tempoch_status_t status, const char *operation)
Check a tempoch_status_t and throw the appropriate exception on error.
Definition ffi_core.hpp:71