Lumiera  0.pre.03
»edityourfreedom«
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 
44 #ifndef LIB_DIAGNOSTIC_CONTEXT_H
45 #define LIB_DIAGNOSTIC_CONTEXT_H
46 
47 
48 #include "lib/error.hpp"
49 #include "lib/nocopy.hpp"
50 #include "lib/thread-local.hpp"
51 
52 #include <nobug.h>
53 
54 
55 
56 namespace lib {
57 
58 
59 
60 
69  template<typename VAL>
72  {
74  typedef std::vector<VAL> ValSequence;
75 
76  const VAL value_;
78 
81  static ThreadLocalAccess&
83  {
84  static ThreadLocalAccess accessPoint;
85  return accessPoint;
86  }
87 
88 
89  public:
90  DiagnosticContext(VAL const& value_to_log = VAL())
91  : value_(value_to_log)
92  , prev_(current().get())
93  {
94  current().set (this);
95  }
96 
98  {
99  ASSERT (this == current().get());
100  current().set (prev_);
101  }
102 
103 
104  operator VAL const&()
105  {
106  return value_;
107  }
108 
110  static DiagnosticContext&
112  {
113  DiagnosticContext* innermost = current().get();
114  if (!innermost)
115  throw lumiera::error::Logic ("Accessing Diagnostic context out of order; "
116  "an instance should have been created within "
117  "an enclosing scope");
118  return *innermost;
119  }
120 
121 
132  static ValSequence
134  {
135  ValSequence loggedValues;
136  DiagnosticContext* next = current().get();
137  while (next)
138  {
139  loggedValues.push_back (*next);
140  next = next->prev_;
141  }
142  return loggedValues;
143  }
144  };
145 
146 
147 
148 
149 
150 } // namespace lib
151 #endif
TAR * get() const
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Thread local pointer without ownership management.
Helpers for working with thread local data.
Implementation namespace for support and library code.
static ThreadLocalAccess & current()
embedded thread local pointer to the innermost context encountered
DiagnosticContext(VAL const &value_to_log=VAL())
Mix-Ins to allow or prohibit various degrees of copying and cloning.
ThreadLocalPtr< DiagnosticContext > ThreadLocalAccess
LumieraError< LERR_(LOGIC)> Logic
Definition: error.hpp:212
Diagnostic data frame to collect specific information concerning a scope.
Lumiera error handling (C++ interface).
void set(TAR *pointee)
DiagnosticContext *const prev_
static DiagnosticContext & access()
accessing the innermost diagnostic context created
static ValSequence extractStack()
snapshot of the current stack of diagnostic frames
std::vector< VAL > ValSequence