GDCM: gdcmException.h Source File

GDCM 3.2.5

gdcmException.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: GDCM (Grassroots DICOM). A DICOM library
4
5 Copyright (c) 2006-2011 Mathieu Malaterre
6 All rights reserved.
7 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notice for more information.
12
13=========================================================================*/
14#ifndef GDCMEXCEPTION_H
15#define GDCMEXCEPTION_H
16
17#include <cassert>
18#include <cstdlib> // NULL
19#include <exception>
20#include <sstream> // ostringstream
21#include <stdexcept> // logic_error
22#include <string>
23
24// Disable clang warning "dynamic exception specifications are deprecated".
25// We need to be C++03 and C++11 compatible, and if we remove the 'throw()'
26// specifier we'll get an error in C++03 by not matching the superclass.
27#if defined(__clang__) && defined(__has_warning)
28# if __has_warning("-Wdeprecated")
29# pragma clang diagnostic push
30# pragma clang diagnostic ignored "-Wdeprecated"
31# endif
32#endif
33
34#define gdcm_forced_assert(cond) assert(cond)
35
36namespace gdcm
37{
38
45 class Exception : public std::exception
46{
51 typedef std::logic_error StringHolder;
52
54 static StringHolder CreateWhat(const char* const desc,
55 const char* const file,
56 const unsigned int lineNumber,
57 const char* const func)
58 {
59 gdcm_forced_assert(desc != nullptr);
60 gdcm_forced_assert(file != nullptr);
61 gdcm_forced_assert(func != nullptr);
62 std::ostringstream oswhat;
63 oswhat << file << ":" << lineNumber << " (" << func << "):\n";
64 oswhat << desc;
65 return StringHolder( oswhat.str() );
66 }
67
68public:
74 explicit Exception(const char *desc = "None",
75 const char *file = __FILE__,
76 unsigned int lineNumber = __LINE__,
77 // FIXME: __PRETTY_FUNCTION__ is the non-mangled version for __GNUC__ compiler
78 const char *func = "" /*__FUNCTION__*/)
79 :
80 What( CreateWhat(desc, file, lineNumber, func) ),
81 Description(desc)
82 {
83 }
84
85 ~Exception() throw() override {}
86
88 const char* what() const throw() override
89 {
90 return What.what();
91 }
92
94 const char * GetDescription() const { return Description.what(); }
95
96private:
97 StringHolder What;
98 StringHolder Description;
99};
100
101} // end namespace gdcm
102
103// Always defined
104#define gdcm_assert(cond) \
105 if (!(cond)) throw gdcm::Exception("An invalid logic behavior occurred" #cond, __FILE__ , __LINE__)
106
107/* Asserts that should only exist in debug builds. */
108#ifndef NDEBUG // checks in debug builds and elision in release builds (like assert)
109#define gdcm_debug_assert(cond) gdcm_assert(cond)
110#else
111#define gdcm_debug_assert(cond) ((void)0)
112#endif
113
114// Undo warning suppression.
115#if defined(__clang__) && defined(__has_warning)
116# if __has_warning("-Wdeprecated")
117# pragma clang diagnostic pop
118# endif
119#endif
120
121#endif
gdcm::Exception::~Exception
~Exception() override
Definition gdcmException.h:85
gdcm::Exception::GetDescription
const char * GetDescription() const
Return the Description.
Definition gdcmException.h:94
gdcm::Exception::Exception
Exception(const char *desc="None", const char *file=__FILE__, unsigned int lineNumber=__LINE__, const char *func="")
Definition gdcmException.h:74
gdcm::Exception::what
const char * what() const override
what implementation
Definition gdcmException.h:88
gdcm_forced_assert
#define gdcm_forced_assert(cond)
Definition gdcmException.h:34
gdcm
Definition gdcmASN1.h:21