underflow_error.hpp

1
2
3
4
5
6
7
8
9
#pragma once


#include <felspar/exceptions/source_annotation.hpp>
#include <optional>
#include <stdexcept>


namespace felspar::stdexcept {

Annotated mathematical underflow exception. Includes value and minimum annotations in addition to throw source location

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
    template<typename V>
    class underflow_error :
    public exceptions::source_annotation<std::underflow_error> {
        V v;
        std::optional<V> mx;

      public:
        explicit underflow_error(
                std::string m,
                V v = {},
                std::optional<V> mx = {},
                source_location loc = source_location::current())
        : source_annotation<
                std::underflow_error>{loc, annotate(std::move(m), loc, v, mx)},
          v{v},
          mx{mx} {}
        underflow_error(std::string m, V v, source_location loc)
        : source_annotation<
                std::underflow_error>{loc, annotate(std::move(m), loc, v, {})},
          v{v},
          mx{} {}

      protected:
        static std::string annotate(
                std::string m, source_location loc, V v, std::optional<V> mx) {
            if (mx) {
                return source_annotation<std::underflow_error>::annotate(
                               std::move(m), loc)
                        + "\nLimit " + std::to_string(*mx) + " and value is "
                        + std::to_string(v);
            } else {
                return source_annotation<std::underflow_error>::annotate(
                               std::move(m), loc)
                        + "\nValue is " + std::to_string(v);
            }
        }
    };

    template<>
    class underflow_error<void> :
    public exceptions::source_annotation<std::underflow_error> {
      public:

Standard constructors

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        explicit underflow_error(
                std::string m, source_location loc = source_location::current())
        : source_annotation<std::underflow_error>{
                loc, annotate(std::move(m), loc)} {}
    };


    underflow_error(std::string const &) -> underflow_error<void>;
    underflow_error(std::string const &, source_location)
            -> underflow_error<void>;
    template<typename V, typename... Args>
    underflow_error(std::string const &, V, Args...) -> underflow_error<V>;


}