exceptions.hpp

 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
#pragma once


#include <felspar/exceptions.hpp>


namespace felspar::parse::binary {


    class buffer_too_small : public stdexcept::logic_error {
      public:
        buffer_too_small(
                felspar::source_location const &loc =
                        felspar::source_location::current())
        : stdexcept::logic_error{"Buffer is too small", loc} {}
        buffer_too_small(
                std::size_t const wants,
                std::size_t const has,
                felspar::source_location const &loc =
                        felspar::source_location::current())
        : stdexcept::logic_error{
                "Buffer is too small\n"
                "Wants " + std::to_string(wants)
                        + " bytes, but only has " + std::to_string(has)
                        + " bytes",
                loc} {}

        static void
                check(std::size_t const wants,
                      std::size_t const has,
                      felspar::source_location const &loc =
                              felspar::source_location::current()) {
            if (wants > has) { throw buffer_too_small{wants, has, loc}; }
        }
    };


}