CppADCodeGen  2.3.0
A C++ Algorithmic Differentiation Package with Source Code Generation
exception.hpp
1 #ifndef CPPAD_CG_EXCEPTION_INCLUDED
2 #define CPPAD_CG_EXCEPTION_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2012 Ciengis
6  *
7  * CppADCodeGen is distributed under multiple licenses:
8  *
9  * - Eclipse Public License Version 1.0 (EPL1), and
10  * - GNU General Public License Version 3 (GPL3).
11  *
12  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
13  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
14  * ----------------------------------------------------------------------------
15  * Author: Joao Leal
16  */
17 
18 namespace CppAD {
19 namespace cg {
20 
26 class CGException : public std::exception {
27 protected:
28  std::string _message;
29 
30 public:
31 
32  inline CGException(const std::string& message) throw () :
33  _message(message) {
34  }
35 
36  template<typename... Ts>
37  explicit CGException(const Ts&... ts) throw () {
38  std::ostringstream s;
39  createMessage(s, ts...);
40  _message = s.str();
41  }
42 
43  CGException() throw () = delete;
44 
45  const char* what() const throw () {
46  return _message.c_str();
47  }
48 
49  virtual ~CGException() throw () {
50  }
51 
52 private:
53 
54  template <typename T, typename... Ts>
55  inline void createMessage(std::ostringstream& s, const T& t, const Ts&... ts) throw () {
56  s << t;
57  createMessage(s, ts...);
58  }
59 
60  template <typename T>
61  inline void createMessage(std::ostringstream& s, const T& t) throw () {
62  s << t;
63  }
64 
65 };
66 
67 inline std::ostream& operator<<(std::ostream& out, const CGException& rhs) {
68  out << rhs.what();
69  return out;
70 }
71 
72 } // END cg namespace
73 } // END CppAD namespace
74 
75 #endif