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) Lumiera.org
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
37 #ifndef LIB_DIAGNOSTIC_CONTEXT_H
38 #define LIB_DIAGNOSTIC_CONTEXT_H
39 
40 
41 #include "lib/error.hpp"
42 #include "lib/nocopy.hpp"
43 
44 
45 
46 namespace lib {
47 
48 
49 
50 
59  template<typename VAL>
62  {
63  using ValSequence = std::vector<VAL>;
64 
65  const VAL value_;
66  DiagnosticContext * const prev_;
67 
70  static DiagnosticContext* &
72  {
73  thread_local DiagnosticContext* accessPoint;
74  return accessPoint;
75  }
76 
77 
78  public:
79  DiagnosticContext(VAL const& value_to_log = VAL())
80  : value_{value_to_log}
81  , prev_{current()}
82  {
83  current() = this;
84  }
85 
87  {
88  ASSERT (this == current());
89  current() = prev_;
90  }
91 
92 
93  operator VAL const&()
94  {
95  return value_;
96  }
97 
99  static DiagnosticContext&
101  {
102  DiagnosticContext* innermost = current();
103  if (!innermost)
104  throw lumiera::error::Logic ("Accessing Diagnostic context out of order; "
105  "an instance should have been created within "
106  "an enclosing scope");
107  return *innermost;
108  }
109 
110 
118  static ValSequence
120  {
121  ValSequence loggedValues;
122  DiagnosticContext* next = current();
123  while (next)
124  {
125  loggedValues.push_back (*next);
126  next = next->prev_;
127  }
128  return loggedValues;
129  }
130  };
131 
132 
133 
134 
135 
136 } // namespace lib
137 #endif
static DiagnosticContext *& current()
embedded thread local pointer to the innermost context encountered
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Implementation namespace for support and library code.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
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