Block-Structured AMR Software Framework
Error.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017, UChicago Argonne, LLC
3  * All Rights Reserved
4  *
5  * Hardware/Hybrid Cosmology Code (HACC), Version 1.0
6  *
7  * Salman Habib, Adrian Pope, Hal Finkel, Nicholas Frontiere, Katrin Heitmann,
8  * Vitali Morozov, Jeffrey Emberson, Thomas Uram, Esteban Rangel
9  * (Argonne National Laboratory)
10  *
11  * David Daniel, Patricia Fasel, Chung-Hsing Hsu, Zarija Lukic, James Ahrens
12  * (Los Alamos National Laboratory)
13  *
14  * George Zagaris
15  * (Kitware)
16  *
17  * OPEN SOURCE LICENSE
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions are met:
21  *
22  * 1. Redistributions of source code must retain the above copyright notice,
23  * this list of conditions and the following disclaimer. Software changes,
24  * modifications, or derivative works, should be noted with comments and
25  * the author and organization's name.
26  *
27  * 2. Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  *
31  * 3. Neither the names of UChicago Argonne, LLC or the Department of Energy
32  * nor the names of its contributors may be used to endorse or promote
33  * products derived from this software without specific prior written
34  * permission.
35  *
36  * 4. The software and the end-user documentation included with the
37  * redistribution, if any, must include the following acknowledgment:
38  *
39  * "This product includes software produced by UChicago Argonne, LLC under
40  * Contract No. DE-AC02-06CH11357 with the Department of Energy."
41  *
42  * *****************************************************************************
43  * DISCLAIMER
44  * THE SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND. NEITHER THE
45  * UNITED STATES GOVERNMENT, NOR THE UNITED STATES DEPARTMENT OF ENERGY, NOR
46  * UCHICAGO ARGONNE, LLC, NOR ANY OF THEIR EMPLOYEES, MAKES ANY WARRANTY,
47  * EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE
48  * ACCURARY, COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, DATA, APPARATUS,
49  * PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT INFRINGE
50  * PRIVATELY OWNED RIGHTS.
51  *
52  * *****************************************************************************
53  */
54 
55 #ifndef HACC_ERROR_H
56 #define HACC_ERROR_H
57 
58 #include <iostream>
59 #include <sstream>
60 #include <stdexcept>
61 #include <string>
62 
63 #include <errno.h>
64 #include <string.h>
65 
66 namespace hacc {
67 // Use this class to generate a fatal error, like this:
68 // Error() << "Something bad happened: " << 5 << " is not " << 6;
69 // which will format a string and throw a std::runtime_error.
70 class Error {
71 public:
72  Error(bool UseErrno = false) : SysError(0) {
73  if (UseErrno)
74  SysError = strerror(errno);
75  }
76 
77  ~Error() {
78  if (SysError)
79  SS << ": " << SysError;
80 
81  throw std::runtime_error(SS.str());
82  }
83 
84  template <typename T>
85  Error &operator << (const T &Value) {
86  SS << Value;
87  return *this;
88  }
89 
90 protected:
91  std::stringstream SS;
92  const char *SysError;
93 };
94 } // namespace hacc
95 
96 #endif // HACC_ERROR_H
97 
Definition: Error.h:70
Error & operator<<(const T &Value)
Definition: Error.h:85
const char * SysError
Definition: Error.h:92
~Error()
Definition: Error.h:77
Error(bool UseErrno=false)
Definition: Error.h:72
std::stringstream SS
Definition: Error.h:91
Definition: AlignedAllocator.h:63