OpenMS
GlobalExceptionHandler.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg$
6 // $Authors: Stephan Aiche, Marc Sturm $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/CONCEPT/Types.h>
13 
14 #include <string>
15 
16 namespace OpenMS
17 {
18 
19  namespace Exception
20  {
21 
27  class OPENMS_DLLAPI GlobalExceptionHandler
28  {
29 private:
33 
51  throw();
52 
54 
57 
59 public:
60 
63  {
64  static GlobalExceptionHandler * globalExceptionHandler_;
65 
66  if (globalExceptionHandler_ == nullptr)
67  {
68  globalExceptionHandler_ = new GlobalExceptionHandler;
69  }
70  return *globalExceptionHandler_;
71  }
72 
76 
79  static void setName(const std::string & name)
80  throw();
81 
84  static void setMessage(const std::string & message)
85  throw();
86 
89  static void setLine(int line)
90  throw();
91 
94  static void setFile(const std::string & file)
95  throw();
96 
99  static void setFunction(const std::string & function)
100  throw();
101 
104  static void set(const std::string & file, int line, const std::string & function,
105  const std::string & name, const std::string & message)
106  throw();
108 protected:
109 
111  static void terminate()
112  throw();
113 
115  static void newHandler();
116 
125 
127  static std::string & file_()
128  {
129  static std::string * file_ = nullptr;
130  if (file_ == nullptr)
131  {
132  file_ = new std::string;
133  *file_ = "unknown";
134  }
135  return *file_;
136  }
137 
139  static int & line_()
140  {
141  static int * line_ = nullptr;
142  if (line_ == nullptr)
143  {
144  line_ = new int;
145  *line_ = -1;
146  }
147  return *line_;
148  }
149 
151  static std::string & function_()
152  {
153  static std::string * function_ = nullptr;
154  if (function_ == nullptr)
155  {
156  function_ = new std::string;
157  *function_ = "unknown";
158  }
159  return *function_;
160  }
161 
163  static std::string & name_()
164  {
165  static std::string * name_ = nullptr;
166  if (name_ == nullptr)
167  {
168  name_ = new std::string;
169  *name_ = "unknown exception";
170  }
171  return *name_;
172  }
173 
175  static std::string & what_()
176  {
177  static std::string * what_ = nullptr;
178  if (what_ == nullptr)
179  {
180  what_ = new std::string;
181  *what_ = " - ";
182  }
183  return *what_;
184  }
185 
187  };
188 
189  } // namespace Exception
190 
191 } // namespace OpenMS
192 
OpenMS global exception handler.
Definition: GlobalExceptionHandler.h:28
static GlobalExceptionHandler & getInstance()
The accessor for the singleton. It also serves as a replacement for the constructor.
Definition: GlobalExceptionHandler.h:62
static void setFunction(const std::string &function)
static void setName(const std::string &name)
static void terminate()
The OPENMS replacement for terminate.
GlobalExceptionHandler()
Default constructor.
static void setFile(const std::string &file)
~GlobalExceptionHandler()
Definition: GlobalExceptionHandler.h:58
static int & line_()
wrapper for static member line_
Definition: GlobalExceptionHandler.h:139
static void set(const std::string &file, int line, const std::string &function, const std::string &name, const std::string &message)
static std::string & what_()
wrapper for static member what_
Definition: GlobalExceptionHandler.h:175
static std::string & function_()
wrapper for static member function_
Definition: GlobalExceptionHandler.h:151
static void setMessage(const std::string &message)
static std::string & name_()
wrapper for static member name_
Definition: GlobalExceptionHandler.h:163
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19