aboutsummaryrefslogtreecommitdiff
path: root/build/diagnostics
blob: 98f481f56b314dabea6ecbcb2df0a0812397dea7 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// file      : build/diagnostics -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD_DIAGNOSTICS
#define BUILD_DIAGNOSTICS

#include <tuple>
#include <vector>
#include <utility>
#include <exception>

#include <build/trace>

namespace build
{
  // Throw this exception to terminate the build. The handler should
  // assume that the diagnostics has already been issued.
  //
  class error: public std::exception {};

  // Print process commmand line.
  //
  void
  print_process (const char* const* args);

  inline void
  print_process (const std::vector<const char*>& args)
  {
    print_process (args.data ());
  }

  // Call a function if there is an exception.
  //
  template <typename F, typename T>
  struct exception_guard;

  template <typename F, typename... A>
  inline exception_guard<F, std::tuple<A&&...>>
  make_exception_guard (F f, A&&... a)
  {
    return exception_guard<F, std::tuple<A&&...>> (
      std::move (f), std::forward_as_tuple (a...));
  }

  template <typename F, typename... A>
  struct exception_guard<F, std::tuple<A...>>
  {
    typedef std::tuple<A...> T;

    exception_guard (F f, T a): f_ (std::move (f)), a_ (std::move (a)) {}
    ~exception_guard ()
    {
      if (std::uncaught_exception ())
        call (std::index_sequence_for<A...> ());
    }

  private:
    template <std::size_t... I>
    void
    call (std::index_sequence<I...>) {f_ (std::get<I> (a_)...);}

    F f_;
    T a_;
  };
}

#endif // BUILD_DIAGNOSTICS