CppADCodeGen  HEAD
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  * Copyright (C) 2019 Joao Leal
7  *
8  * CppADCodeGen is distributed under multiple licenses:
9  *
10  * - Eclipse Public License Version 1.0 (EPL1), and
11  * - GNU General Public License Version 3 (GPL3).
12  *
13  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
14  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
15  * ----------------------------------------------------------------------------
16  * Author: Joao Leal
17  */
18 
19 namespace CppAD {
20 namespace cg {
21 
27 class CGException : public std::exception {
28 protected:
29  std::string _message;
30 
31 public:
32 
33  inline explicit CGException(std::string message) noexcept :
34  _message(std::move(message)) {
35  }
36 
37  inline CGException(const CGException& e) = default;
38 
39  inline CGException(CGException&& e) = default;
40 
41  template<typename... Ts>
42  explicit CGException(const Ts&... ts) noexcept {
43  std::ostringstream s;
44  createMessage(s, ts...);
45  _message = s.str();
46  }
47 
48  CGException() noexcept = delete;
49 
50  const char* what() const noexcept override {
51  return _message.c_str();
52  }
53 
54  ~CGException() noexcept override = default;
55 
56 private:
57 
58  template <typename T, typename... Ts>
59  inline void createMessage(std::ostringstream& s, const T& t, const Ts&... ts) noexcept {
60  s << t;
61  createMessage(s, ts...);
62  }
63 
64  template <typename T>
65  inline void createMessage(std::ostringstream& s, const T& t) noexcept {
66  s << t;
67  }
68 
69 };
70 
71 inline std::ostream& operator<<(std::ostream& out, const CGException& rhs) {
72  out << rhs.what();
73  return out;
74 }
75 
76 } // END cg namespace
77 } // END CppAD namespace
78 
79 #endif