LIBINT  2.6.0
util.h
1 /*
2  * Copyright (C) 2004-2019 Edward F. Valeev
3  *
4  * This file is part of Libint.
5  *
6  * Libint 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 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Libint 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 Libint. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef _libint2_src_bin_libint_util_h_
22 #define _libint2_src_bin_libint_util_h_
23 
24 #include <numeric>
25 #include <string>
26 #include <stdexcept>
27 #include <smart_ptr.h>
28 #include <util_types.h>
29 #include <cxxabi.h>
30 
31 namespace libint2 {
32  std::string to_string(FunctionPosition pos);
33 
34  template <class Target, class Source> SafePtr<Target> require_dynamic_cast(const SafePtr<Source>& s) {
35  const SafePtr<Target> t = dynamic_pointer_cast<Target,Source>(s);
36  if (t == 0)
37  throw std::runtime_error("require_dynamic_cast: dynamic case failed");
38  return t;
39  }
40  template <class Target, class Source> const Target* require_dynamic_cast(const Source* s) {
41  const Target* t = dynamic_cast<Target*>(s);
42  if (t == 0)
43  throw std::runtime_error("require_dynamic_cast: dynamic case failed");
44  return t;
45  }
46 
48  template <typename T> std::string class_name(T* ptr=nullptr) {
49  int status = 1;
50  std::unique_ptr<char, void (*)(void*)>
51  result { abi::__cxa_demangle(ptr==nullptr?typeid(T).name():typeid(ptr).name(),NULL,NULL,&status),
52  std::free };
53  return status == 0 ? result.get() : "unknown";
54  }
55 
56 } // namespace libint2
57 
58 #endif /* header guard */
std::string class_name(T *ptr=nullptr)
Definition: util.h:48
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
std::string to_string(const T &x)
Converts x to its string representation.
Definition: entity.h:74