Lumiera  0.pre.03
»edit your freedom«
diagnostic-context.hpp
Go to the documentation of this file.
1 /*
2  DIAGNOSTIC-CONTEXT.hpp - thread local stack for explicitly collecting diagnostic context info
3 
4  Copyright (C)
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
28 #ifndef LIB_DIAGNOSTIC_CONTEXT_H
29 #define LIB_DIAGNOSTIC_CONTEXT_H
30 
31 
32 #include "lib/error.hpp"
33 #include "lib/nocopy.hpp"
34 
35 
36 
37 namespace lib {
38 
39 
40 
41 
50  template<typename VAL>
53  {
54  using ValSequence = std::vector<VAL>;
55 
56  const VAL value_;
57  DiagnosticContext * const prev_;
58 
61  static DiagnosticContext* &
63  {
64  thread_local DiagnosticContext* accessPoint;
65  return accessPoint;
66  }
67 
68 
69  public:
70  DiagnosticContext(VAL const& value_to_log = VAL())
71  : value_{value_to_log}
72  , prev_{current()}
73  {
74  current() = this;
75  }
76 
78  {
79  ASSERT (this == current());
80  current() = prev_;
81  }
82 
83 
84  operator VAL const&()
85  {
86  return value_;
87  }
88 
90  static DiagnosticContext&
91  access ()
92  {
93  DiagnosticContext* innermost = current();
94  if (!innermost)
95  throw lumiera::error::Logic ("Accessing Diagnostic context out of order; "
96  "an instance should have been created within "
97  "an enclosing scope");
98  return *innermost;
99  }
100 
101 
109  static ValSequence
111  {
112  ValSequence loggedValues;
113  DiagnosticContext* next = current();
114  while (next)
115  {
116  loggedValues.push_back (*next);
117  next = next->prev_;
118  }
119  return loggedValues;
120  }
121  };
122 
123 
124 
125 
126 
127 } // namespace lib
128 #endif
static DiagnosticContext *& current()
embedded thread local pointer to the innermost context encountered
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
Implementation namespace for support and library code.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Diagnostic data frame to collect specific information concerning a scope.
Lumiera error handling (C++ interface).
static DiagnosticContext & access()
accessing the innermost diagnostic context created
static ValSequence extractStack()
snapshot of the current stack of diagnostic frames