Commit 2c4b29b5c6fa8e6409f761fcb32f07276c6624c5

Authored by m-holger
1 parent 724658fe

Refactor `util::assertion` etc to accept forwarding references for improved flexibility

- Updated `assertion`, `internal_error_if`, and `no_ci_rt_error_if` to use templates and `std::forward`.
Showing 1 changed file with 12 additions and 7 deletions
libqpdf/qpdf/Util.hh
... ... @@ -18,28 +18,33 @@ namespace qpdf::util
18 18 // Throw a logic_error if 'cond' does not hold.
19 19 //
20 20 // DO NOT USE unless it is impractical or unnecessary to cover violations during CI Testing.
  21 + template <typename T>
21 22 inline void
22   - assertion(bool cond, std::string const& msg)
  23 + assertion(bool cond, T&& msg)
23 24 {
24 25 if (!cond) {
25   - throw std::logic_error(msg);
  26 + throw std::logic_error(std::forward<T>(msg));
26 27 }
27 28 }
28 29  
  30 + template <typename T>
29 31 inline void
30   - internal_error_if(bool cond, std::string const& msg)
  32 + internal_error_if(bool cond, T&& msg)
31 33 {
32 34 if (cond) {
33   - throw std::logic_error("INTERNAL ERROR: "s.append(msg).append(
34   - "\nThis is a qpdf bug. Please report at https://github.com/qpdf/qpdf/issues"));
  35 + throw std::logic_error("INTERNAL ERROR: "s.append(std::forward<T>(msg))
  36 + .append(
  37 + "\nThis is a qpdf bug. Please report at "
  38 + "https://github.com/qpdf/qpdf/issues"));
35 39 }
36 40 }
37 41  
  42 + template <typename T>
38 43 inline void
39   - no_ci_rt_error_if(bool cond, std::string const& msg)
  44 + no_ci_rt_error_if(bool cond, T&& msg)
40 45 {
41 46 if (cond) {
42   - throw std::runtime_error(msg);
  47 + throw std::runtime_error(std::forward<T>(msg));
43 48 }
44 49 }
45 50  
... ...