File indexing completed on 2021-02-14 13:29:19
0001 #ifndef FWCore_Utilities_Exception_h
0002 #define FWCore_Utilities_Exception_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 #include <list>
0036 #include <sstream>
0037 #include <string>
0038 #include <exception>
0039 #include <type_traits>
0040
0041 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0042 #include "FWCore/Utilities/interface/Likely.h"
0043 #include "FWCore/Utilities/interface/Visibility.h"
0044
0045 namespace cms {
0046
0047 namespace detail {
0048
0049
0050
0051
0052
0053 template <typename T, bool b>
0054 struct Desired;
0055 template <typename T>
0056 struct Desired<T, true> {
0057 typedef T type;
0058 };
0059
0060
0061
0062
0063 template <typename BASE, typename DERIVED>
0064 struct is_derived_or_same {
0065 static bool const value = std::is_base_of<BASE, DERIVED>::value || std::is_same<BASE, DERIVED>::value;
0066 };
0067
0068 }
0069
0070 class dso_export Exception : public std::exception {
0071 public:
0072 explicit Exception(std::string const& aCategory);
0073 explicit Exception(char const* aCategory);
0074
0075 Exception(std::string const& aCategory, std::string const& message);
0076 Exception(char const* aCategory, std::string const& message);
0077 Exception(std::string const& aCategory, char const* message);
0078 Exception(char const* aCategory, char const* message);
0079
0080 Exception(std::string const& aCategory, std::string const& message, Exception const& another);
0081
0082 Exception(Exception const& other);
0083 ~Exception() noexcept override;
0084
0085 void swap(Exception& other);
0086
0087 Exception& operator=(Exception const& other);
0088
0089
0090
0091 char const* what() const noexcept override;
0092
0093 virtual std::string explainSelf() const;
0094
0095 std::string const& category() const;
0096 std::string message() const;
0097 std::list<std::string> const& context() const;
0098 std::list<std::string> const& additionalInfo() const;
0099 int returnCode() const;
0100
0101 void raise() { rethrow(); }
0102
0103 void append(Exception const& another);
0104 void append(std::string const& more_information);
0105 void append(char const* more_information);
0106
0107 void clearMessage();
0108 void clearContext();
0109 void clearAdditionalInfo();
0110
0111 void addContext(std::string const& context);
0112 void addContext(char const* context);
0113
0114 void addAdditionalInfo(std::string const& info);
0115 void addAdditionalInfo(char const* info);
0116
0117 void setContext(std::list<std::string> const& context);
0118 void setAdditionalInfo(std::list<std::string> const& info);
0119
0120 bool alreadyPrinted() const;
0121 void setAlreadyPrinted(bool value);
0122
0123 virtual Exception* clone() const;
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145 template <typename E, typename T>
0146 friend typename detail::Desired<E, detail::is_derived_or_same<Exception, std::remove_reference_t<E>>::value>::type&
0147 operator<<(E&& e, T const& stuff);
0148
0149 template <typename E>
0150 friend typename detail::Desired<E, detail::is_derived_or_same<Exception, std::remove_reference_t<E>>::value>::type&
0151 operator<<(E&& e, std::ostream& (*f)(std::ostream&));
0152
0153 template <typename E>
0154 friend typename detail::Desired<E, detail::is_derived_or_same<Exception, std::remove_reference_t<E>>::value>::type&
0155 operator<<(E&& e, std::ios_base& (*f)(std::ios_base&));
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176 std::list<std::string> history() const;
0177
0178 private:
0179 void init(std::string const& message);
0180 virtual void rethrow();
0181 virtual int returnCode_() const;
0182
0183
0184 std::ostringstream ost_;
0185 std::string category_;
0186
0187 CMS_SA_ALLOW mutable std::string what_;
0188 std::list<std::string> context_;
0189 std::list<std::string> additionalInfo_;
0190 bool alreadyPrinted_;
0191 };
0192
0193 inline std::ostream& operator<<(std::ostream& ost, Exception const& e) {
0194 ost << e.explainSelf();
0195 return ost;
0196 }
0197
0198
0199
0200 template <typename E, typename T>
0201 inline typename detail::Desired<E, detail::is_derived_or_same<Exception, std::remove_reference_t<E>>::value>::type&
0202 operator<<(E&& e, T const& stuff) {
0203 e.ost_ << stuff;
0204 return e;
0205 }
0206
0207 template <typename E>
0208 inline typename detail::Desired<E, detail::is_derived_or_same<Exception, std::remove_reference_t<E>>::value>::type&
0209 operator<<(E&& e, std::ostream& (*f)(std::ostream&)) {
0210 f(e.ost_);
0211 return e;
0212 }
0213
0214 template <typename E>
0215 inline typename detail::Desired<E, detail::is_derived_or_same<Exception, std::remove_reference_t<E>>::value>::type&
0216 operator<<(E&& e, std::ios_base& (*f)(std::ios_base&)) {
0217 f(e.ost_);
0218 return e;
0219 }
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263 }
0264
0265 #endif