Sierra Toolkit  Version of the Day
Exception.hpp
Go to the documentation of this file.
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2003 - 2008 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 
9 #ifndef STK_UTIL_PARALLEL_Exception_hpp
10 #define STK_UTIL_PARALLEL_Exception_hpp
11 
12 /*--------------------------------------------------------------------*/
63 #include <stdexcept>
64 #include <exception>
65 #include <new>
66 #include <typeinfo>
67 #include <string>
68 #include <vector>
69 // #include <iostream> // for std::cerr
70 #include <sstream>
71 
72 #include <stk_util/stk_config.h>
73 #if defined( STK_HAS_MPI )
74 #include <mpi.h>
75 #endif
76 
77 #include <stk_util/environment/ReportHandler.hpp>
78 
79 #include <stk_util/util/FeatureTest.hpp>
80 #include <stk_util/diag/String.hpp>
81 #include <stk_util/diag/StringUtil.hpp>
82 #include <stk_util/diag/Trace.hpp>
83 
84 namespace sierra {
85 
90 
91 class ExParallel;
92 
99 
106 void set_exception();
107 
115 void set_exception(std::exception &x);
116 
125 void set_exception(ExParallel &x);
126 
134 
151 void throw_copy(const std::exception &x, const std::string &message);
152 
162 void parallel_throw(MPI_Comm mpi_comm);
163 
164 
183 {
184 public:
192 
200  static bool areExceptionsRegistered() {
201  return !ParallelThrowRegistry::instance().m_registry.empty();
202  }
203 
213  template<class T>
215  T *x = new T();
216  register_exception_a(typeid(T), x);
217  register_exception_a(typeid(typename T::BaseExceptionType), x);
218  return *x;
219  }
220 
230  ExParallel *findException(const std::type_info &exception_type);
231 
232 private:
244  ExParallel &register_exception_a(const std::type_info &parallel_exception_type, ExParallel *exception);
245 
246 private:
252  class Registry : public std::vector<std::pair<const std::type_info *, ExParallel *> >
253  {
254  public:
255  Registry();
256 
257  ~Registry();
258  };
259 
260  Registry m_registry;
261 };
262 
263 
272 {
273 protected:
280  : m_descriptionStream(),
281  m_whatBuffer(),
282  m_traceback(Diag::Trace::printTraceback(Diag::Trace::Traceback::snapshot())),
283  m_parallel(-1)
284  {
287  }
288 
299  explicit ExParallel(const std::string & message, int parallel = -1)
300  : m_descriptionStream(),
301  m_whatBuffer(),
302  m_traceback(Diag::Trace::printTraceback(Diag::Trace::Traceback::snapshot())),
303  m_parallel(parallel)
304  {
305  m_descriptionStream << message;
308  }
309 
317  : m_descriptionStream(),
318  m_whatBuffer(),
319  m_traceback(x.m_traceback),
320  m_parallel(x.m_parallel)
321  {
322  m_descriptionStream << x.m_descriptionStream.str();
323  }
324 
325 public:
330  virtual ~ExParallel()
331  {}
332 
339  virtual const char *what() const throw() {
340  try {
341  m_whatBuffer = m_descriptionStream.str();
342  return m_whatBuffer.c_str();
343  }
344  catch(...) {
345  return NULL;
346  }
347  }
348 
355  m_descriptionStream.str("");
356  m_traceback.clear();
357  m_parallel = -1;
358  return *this;
359  }
360 
370  ExParallel &setDescription(const std::string &description) {
371  clear();
372  m_descriptionStream << description;
373  return *this;
374  }
375 
383  std::string getDescription() const{
384  return m_descriptionStream.str();
385  }
386 
394  std::ostringstream &getDescriptionStream() {
395  return m_descriptionStream;
396  }
397 
405  const std::ostringstream &getDescriptionStream() const {
406  return m_descriptionStream;
407  }
408 
418  ExParallel &setTraceback(const std::string &traceback) {
419  m_traceback = traceback;
420  return *this;
421  }
422 
429  const std::string &getTraceback() const {
430  return m_traceback;
431  }
432 
443  ExParallel &setParallel(int parallel) {
444  m_parallel = parallel;
445  return *this;
446  }
447 
455  int getParallel() const {
456  return m_parallel;
457  }
458 
459 
467  bool isParallel() const {
468  return m_parallel != -1;
469  }
470 
479  ExParallel &operator<<(std::ostream& (*f)(std::ostream&)) {
480  f(m_descriptionStream);
481  return *this;
482  }
483 
493  template<class U>
494  ExParallel &operator<<(const U &t) {
495  m_descriptionStream << t;
496  return *this;
497  }
498 
509  virtual void throw_copy() const = 0;
510 
518  virtual void parallel_handler();
519 
520 private:
521  std::ostringstream m_descriptionStream;
522  mutable std::string m_whatBuffer;
523  std::string m_traceback;
524  int m_parallel;
525 };
526 
527 
535 template<class T>
536 class ExTemp : public ExParallel, public T
537 {
538 public:
540  typedef T BaseExceptionType;
541 
547  : ExParallel(),
548  T()
549  {}
550 
557  explicit ExTemp(const std::string &message)
558  : ExParallel(message),
559  T()
560  {}
561 
567  ExTemp(const ExTemp & x)
568  : ExParallel(static_cast<const ExParallel &>(x)),
569  T(static_cast<const T &>(x))
570  {}
571 
576  virtual ~ExTemp() throw()
577  {}
578 
585  virtual const char *what() const throw() {
586  return ExParallel::what();
587  }
588 
597  ExTemp &operator<<(std::ostream& (*f)(std::ostream&)) {
599  return *this;
600  }
601 
611  template<class U>
612  ExTemp &operator<<(const U &t) {
613  getDescriptionStream() << t;
614  return *this;
615  }
616 
623  virtual void throw_copy() const {
624  ParallelExceptionType t(*this);
625 
626 // std::cerr << "throwing " << this->what() << std::endl
627 // << " as " << t.what();
628  throw t;
629  }
630 
638  static void registerException() {
639 #ifdef SIERRA_TEMPLATE_CALL_BUG
640  ParallelThrowRegistry::instance().template registerException<ExTemp>();
641 #else
643 #endif
644  }
645 };
646 
647 
648 template<class T>
649 class ExTemp1 : public ExParallel, public T
650 {
651 public:
652  typedef ExTemp1<T> ParallelExceptionType;
653  typedef T BaseExceptionType;
654 
659  ExTemp1()
660  : ExParallel(),
661  T(std::string())
662  {}
663 
670  explicit ExTemp1(const std::string & message)
671  : ExParallel(message),
672  T(message)
673  {}
674 
680  ExTemp1(const ExTemp1 & x)
681  : ExParallel(static_cast<const ExParallel &>(x)),
682  T(static_cast<const T &>(x))
683  {}
684 
689  virtual ~ExTemp1() throw()
690  {}
691 
698  virtual const char *what() const throw() {
699  return ExParallel::what();
700  }
701 
710  ExTemp1 &operator<<(std::ostream& (*f)(std::ostream&)) {
712  return *this;
713  }
714 
715 
725  template<class U>
726  ExTemp1 &operator<<(const U &t) {
727  getDescriptionStream() << t;
728  return *this;
729  }
730 
740  virtual void throw_copy() const {
741  ParallelExceptionType t(*this);
742 
743 // std::cerr << "throwing " << this->what() << std::endl
744 // << " as " << t.what();
745  throw t;
746  }
747 
755  static ExParallel &registerException() {
756 #ifdef SIERRA_TEMPLATE_CALL_BUG
757  return ParallelThrowRegistry::instance().template registerException<ExTemp1>();
758 #else
760 #endif
761  }
762 };
763 
769 typedef ExTemp1<std::ios_base::failure> IosBaseFailure;
770 typedef ExTemp1<std::logic_error> LogicError;
771 typedef ExTemp1<std::domain_error> DomainError;
772 typedef ExTemp1<std::invalid_argument> InvalidArgument;
773 typedef ExTemp1<std::length_error> LengthError;
774 typedef ExTemp1<std::out_of_range> OutOfRange;
775 typedef ExTemp1<std::runtime_error> RuntimeError;
776 typedef ExTemp1<std::range_error> RangeError;
777 typedef ExTemp1<std::overflow_error> OverflowError;
778 typedef ExTemp1<std::underflow_error> UnderflowError;
779 
780 //----------------------------------------------------------------------
781 
786 class runtime_user_error : public std::runtime_error
787 {
788 public:
789  explicit runtime_user_error(const std::string &message) throw()
790  : std::runtime_error(message)
791  {}
792 
793  runtime_user_error(const runtime_user_error &x) throw()
794  : std::runtime_error(x)
795  {}
796 
797  virtual ~runtime_user_error() throw ()
798  {}
799 };
800 
801 typedef ExTemp1<runtime_user_error> RuntimeUserError;
802 
806 
807 } // namepace sierra
808 
809 // DO NOT USE ParallelStackTrace, unless you know that the exception will be thrown in
810 // parallel. This is a magic string used by the parallel_throw routine that makes
811 // sure the printout will only be done on processor 0. Since the exception is
812 // parallel there is no reason to print the same message many times.
813 #define StackTraceMessage " exception thrown from "
814 #define ParallelStackTraceMessage " parallel exception thrown from "
815 #define ParallelStackTrace std::string(std::string(ParallelStackTraceMessage) + stk_classic::source_relative_path(STR_TRACE))
816 
817 #endif // STK_UTIL_PARALLEL_Exception_hpp
ExTemp1< std::invalid_argument > InvalidArgument
Defined in <stdexcept>
Definition: Exception.hpp:772
ExParallel & clear()
Member function clear clears the contents of the exception.
Definition: Exception.hpp:354
void sierra_exception_throw()
Member function sierra_exception_throw is called whenever a parallel exception is constructed...
Definition: Exception.cpp:34
ExTemp< std::bad_alloc > BadAlloc
Defined in <new>
Definition: Exception.hpp:766
virtual ~ExTemp()
Definition: Exception.hpp:576
void parallel_throw(MPI_Comm mpi_comm)
Function parallel_throw throws a consistant exception in parallel. parallel_throw is called after the...
Definition: Exception.cpp:180
virtual void throw_copy() const
Member function copy throws a copy of the original exception. It copies the original message...
Definition: Exception.hpp:623
ExParallel & operator<<(std::ostream &(*f)(std::ostream &))
Member function operator<< passes the std manipilator functions to the ExParallel object...
Definition: Exception.hpp:479
Definition: Env.cpp:53
virtual const char * what() const
Member function what returns the exception&#39;s description.
Definition: Exception.hpp:585
void throw_copy(const std::exception &x, const std::string &append_message)
Function throw_copy throws a copy of the exception. The exception is located in the parallel exceptio...
Definition: Exception.cpp:91
virtual const char * what() const
Member function what returns the exception&#39;s description.
Definition: Exception.hpp:339
ExParallel & registerException()
Member template function registerException registers an exception of the specified type with the para...
Definition: Exception.hpp:214
std::ostringstream & getDescriptionStream()
Member function getDescriptionStream returns the stream used to assemble the description.
Definition: Exception.hpp:394
ExParallel * findException(const std::type_info &exception_type)
Member function findException returns a pointer to the matching exception in parallel exception regis...
Definition: Exception.cpp:74
std::string getDescription() const
Member function getDescription returns the exception&#39;s description.
Definition: Exception.hpp:383
Template ExTemp takes a zero argument exception and makes it into a parallel throwable and put-to-abl...
Definition: Exception.hpp:536
ExParallel & setTraceback(const std::string &traceback)
Member function setTraceback sets the exception&#39;s traceback to the caller generating the exception...
Definition: Exception.hpp:418
ExParallel(const std::string &message, int parallel=-1)
Definition: Exception.hpp:299
ExTemp< std::bad_exception > BadException
Defined in
Definition: Exception.hpp:765
void set_exception()
Function set_exception is called on a single processor when an exception is caught. The next collective communication will propogate the exception to all processors. This flavor is called when an unknown exception (...) is caught.
Definition: Exception.cpp:107
ExTemp & operator<<(std::ostream &(*f)(std::ostream &))
Member function operator<< passes the std manipilator functions to the ExTemp object. This allows the manipulators to modify the description. (Currently in a limited fashion).
Definition: Exception.hpp:597
static ParallelThrowRegistry & instance()
Member function instance returns the singleton instance for the parallel exception registry...
Definition: Exception.cpp:39
Class ParallelThrowRegistry is a registry of known parallel exceptions. For the negotiation of parall...
Definition: Exception.hpp:182
ExParallel & setParallel(int parallel)
Member function setParallel sets the originating processor for an exception that is being thrown in p...
Definition: Exception.hpp:443
bool isParallel() const
Member function isParallel returns true if the exception is being thrown in parallel.
Definition: Exception.hpp:467
ExTemp1< std::runtime_error > RuntimeError
Defined in <stdexcept>
Definition: Exception.hpp:775
static bool areExceptionsRegistered()
Static member function setExceptionsRegistered sets the exceptions have been registered flag...
Definition: Exception.hpp:200
std::ostream & operator<<(std::ostream &s, const Bucket &k)
Print the part names for which this bucket is a subset.
Definition: Bucket.cpp:239
ExTemp1< std::logic_error > LogicError
Defined in <stdexcept>
Definition: Exception.hpp:770
ExParallel(const ExParallel &x)
Definition: Exception.hpp:316
ExTemp< std::bad_typeid > BadTypeid
Defined in <typeinfo>
Definition: Exception.hpp:767
ExTemp & operator<<(const U &t)
Definition: Exception.hpp:612
virtual void parallel_handler()
Member function parallel_handler is called just before a parallel exception is thrown. It is guaranteed to be called in parallel on all processors, so collective communication is allowed inside Parallel_Handler. This function might be used to copy information to all processors. The default is to do nothing.
Definition: Exception.cpp:86
virtual ~ExParallel()
Definition: Exception.hpp:330
ExTemp1< std::out_of_range > OutOfRange
Defined in <stdexcept>
Definition: Exception.hpp:774
static void registerException()
Member function registerException registers the exception with the parallel exception registry...
Definition: Exception.hpp:638
void register_stl_parallel_exceptions()
Member function register_stl_parallel_exceptions registers the stl exceptions with the parallel excep...
Definition: Exception.cpp:156
ExTemp< T > ParallelExceptionType
Parallel exception type.
Definition: Exception.hpp:539
ExTemp1< std::ios_base::failure > IosBaseFailure
Defined in <ios>
Definition: Exception.hpp:769
ExTemp1< std::domain_error > DomainError
Defined in <stdexcept>
Definition: Exception.hpp:771
ExParallel & operator<<(const U &t)
Definition: Exception.hpp:494
virtual void throw_copy() const =0
Member function throw_copy is a pure virtual function which is allows the copying and throwing of the...
int getParallel() const
Member function getParallel returns the originating processor for a parallel exception or -1 if the e...
Definition: Exception.hpp:455
ExTemp1< std::length_error > LengthError
Defined in <stdexcept>
Definition: Exception.hpp:773
ExTemp1< std::overflow_error > OverflowError
Defined in <stdexcept>
Definition: Exception.hpp:777
const std::string & getTraceback() const
Member function getTraceback returns the exception&#39;s traceback string.
Definition: Exception.hpp:429
Class ExParallel implements the features of a parallel exception. It is a std::string which stores th...
Definition: Exception.hpp:271
ExTemp(const ExTemp &x)
Definition: Exception.hpp:567
ExTemp1< std::range_error > RangeError
Defined in <stdexcept>
Definition: Exception.hpp:776
ExTemp< std::exception > Exception
Defined in
Definition: Exception.hpp:764
T BaseExceptionType
Base exception type.
Definition: Exception.hpp:540
ExTemp1< std::underflow_error > UnderflowError
Defined in <stdexcept>
Definition: Exception.hpp:778
ExTemp(const std::string &message)
Definition: Exception.hpp:557
ExTemp< std::bad_cast > BadCast
Defined in <typeinfo>
Definition: Exception.hpp:768
ExParallel & setDescription(const std::string &description)
Member function setDescription sets the value of the exception&#39;s description.
Definition: Exception.hpp:370
const std::ostringstream & getDescriptionStream() const
Member function getDescriptionStream returns the stream used to assemble the description.
Definition: Exception.hpp:405
Class runtime_user_error ...
Definition: Exception.hpp:786