MADNESS  0.10.1
tensorexcept.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 
36 #ifndef MADNESS_TENSOR_TENSOREXCPT_H__INCLUDED
37 #define MADNESS_TENSOR_TENSOREXCPT_H__INCLUDED
38 
39 /// \file tensorexcept.h
40 /// \brief Declares and implements TensorException
41 
42 /// \ingroup tensor
43 
44 #include <iosfwd>
45 #include <exception>
47 
48 namespace madness {
49 /// Tensor is intended to throw only TensorExceptions
50  class TensorException : public std::exception {
51  const char* msg;
52  const char* assertion;
53  int value;
55  const BaseTensor *tp;
56  int line;
57  const char *function;
58  const char *filename;
59 
60 public:
61  // Capturing the line/function/filename info is best done with the macros below
62  TensorException(const char* s, const char *a, int err, const BaseTensor* tp,
63  int lin, const char *func, const char *file)
64  : msg(s)
65  , assertion(a)
66  , value(err)
67  , tp(tp)
68  , line(lin)
69  , function(func)
70  , filename(file) {
71  // We must copy the pointed-to tensor because during unwinding it
72  // might go out of scope and dereferencing its pointer is invalid
73  if (tp != nullptr)
74  new(&t) BaseTensor(*tp);
75  }
76 
77  virtual const char* what() const throw() {
78  return msg;
79  }
80 
81  virtual ~TensorException() throw() {}
82 
83  /// Print a TensorException to the stream (for human consumption)
84  friend std::ostream& operator <<(std::ostream& out, const TensorException& e) {
85  out << "TensorException: msg='";
86  if (e.msg) out << e.msg;
87  out << "'\n";
88  if (e.assertion) out << " failed assertion='" <<
89  e.assertion << "'\n";
90  out << " value=" << e.value << "\n";
91  if (e.line) out << " line=" << e.line << "\n";
92  if (e.function) out << " function='" <<
93  e.function << "'\n";
94  if (e.filename) out << " filename='" <<
95  e.filename << "'\n";
96  if (e.tp != nullptr) {
97  out << " tensor=Tensor<";
98  if (e.t.id()>=0 && e.t.id()<=TENSOR_MAX_TYPE_ID) {
99  out << tensor_type_names[e.t.id()] << ">(";
100  }
101  else {
102  out << "invalid_type_id>(";
103  }
104  if (e.t.ndim()>=0 && e.t.ndim()<TENSOR_MAXDIM) {
105  for (int i=0; i<e.t.ndim(); ++i) {
106  out << e.t.dim(i);
107  if (i != (e.t.ndim()-1)) out << ",";
108  }
109  out << ")";
110  }
111  else {
112  out << "invalid_dimensions)";
113  }
114  out << " at 0x" << (void *)(e.tp) << "\n";
115  }
116 
117  return out;
118  }
119 
120  };
121 
122 
123 #define TENSOR_STRINGIZE(X) #X
124 #define TENSOR_EXCEPTION_AT(F, L) TENSOR_STRINGIZE(F) "(" TENSOR_STRINGIZE(L) ")"
125 
126 #define TENSOR_EXCEPTION(msg,value,t) \
127  throw ::madness::TensorException("TENSOR EXCEPTION: " TENSOR_EXCEPTION_AT( __FILE__, __LINE__ ) ": " msg , \
128  0,value,t,__LINE__,__FUNCTION__,__FILE__)
129 
130 #define TENSOR_ASSERT(condition,msg,value,t) \
131 do {if (!(condition)) \
132  throw ::madness::TensorException("TENSOR ASSERTION FAILED: " TENSOR_EXCEPTION_AT( __FILE__, __LINE__ ) ": " msg , \
133  #condition,value,t,__LINE__,__FUNCTION__,__FILE__); \
134  } while (0)
135 
136 }
137 
138 #endif // MADNESS_TENSOR_TENSOREXCPT_H__INCLUDED
Declares BaseTensor.
The base class for tensors defines generic capabilities.
Definition: basetensor.h:85
Tensor is intended to throw only TensorExceptions.
Definition: tensorexcept.h:50
virtual const char * what() const
Definition: tensorexcept.h:77
int line
Definition: tensorexcept.h:56
const char * function
Definition: tensorexcept.h:57
const char * filename
Definition: tensorexcept.h:58
const char * msg
Definition: tensorexcept.h:51
friend std::ostream & operator<<(std::ostream &out, const TensorException &e)
Print a TensorException to the stream (for human consumption)
Definition: tensorexcept.h:84
TensorException(const char *s, const char *a, int err, const BaseTensor *tp, int lin, const char *func, const char *file)
Definition: tensorexcept.h:62
virtual ~TensorException()
Definition: tensorexcept.h:81
const BaseTensor * tp
Definition: tensorexcept.h:55
BaseTensor t
Definition: tensorexcept.h:54
int value
Definition: tensorexcept.h:53
const char * assertion
Definition: tensorexcept.h:52
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
const char * tensor_type_names[]
std::shared_ptr< FunctionFunctorInterface< double, 3 > > func(new opT(g))
static const double a
Definition: nonlinschro.cc:118
#define TENSOR_MAXDIM
Definition: tensor_macros.h:194
void e()
Definition: test_sig.cc:75
#define TENSOR_MAX_TYPE_ID
Definition: type_data.h:115