escript  Revision_
Assert.h
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2016 by The University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Apache License, version 2.0
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development 2012-2013 by School of Earth Sciences
13 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 *
15 *****************************************************************************/
16 
17 #ifndef __ESCRIPT_ASSERT_H__
18 #define __ESCRIPT_ASSERT_H__
19 
31 #if DOASSERT
32 
33 //
34 // DOASSERT is defined, evaluate assertions and abort on failure.
35 //
36 
37 #include <escript/EsysException.h>
38 #include <iostream>
39 #include <sstream>
40 
41 #if ESYS_MPI
42 
43 #include <mpi.h>
44 
45 #define ESYS_ASSERT(assert_test, assert_msg)\
46  do {\
47  const bool result = (assert_test);\
48  if (!result) {\
49  std::ostringstream message;\
50  message << assert_msg << "\n\n"\
51  << __FILE__ << ":" << __LINE__ << ": " << #assert_test << "\n";\
52  std::cerr << message.str();\
53  MPI_Abort(MPI_COMM_WORLD, 455347);\
54  }\
55  } while (0)
56 
57 #else
58 
59 #define ESYS_ASSERT(assert_test, assert_msg)\
60  do {\
61  const bool result = (assert_test);\
62  if (!result) {\
63  std::ostringstream message;\
64  message << assert_msg << "\n\n"\
65  << __FILE__ << ":" << __LINE__ << ": " << #assert_test << "\n";\
66  throw escript::AssertException(message.str());\
67  }\
68  } while (0)
69 
70 #endif // ESYS_MPI
71 
72 #else // !DOASSERT
73 
74 //
75 // DOASSERT is not defined, replace ESYS_ASSERT macro with no-op
76 //
77 
78 #define ESYS_ASSERT(a,b)
79 
80 #endif
81 
82 #endif // __ESCRIPT_ASSERT_H__
83