LIBINT  2.1.0-stable
util.h
1 /*
2  * This file is a part of Libint.
3  * Copyright (C) 2004-2014 Edward F. Valeev
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see http://www.gnu.org/licenses/.
17  *
18  */
19 
20 #ifndef _libint2_src_bin_libint_util_h_
21 #define _libint2_src_bin_libint_util_h_
22 
23 #include <numeric>
24 #include <string>
25 #include <stdexcept>
26 #include <smart_ptr.h>
27 #include <util_types.h>
28 #include <cxxabi.h>
29 
30 namespace libint2 {
31  std::string to_string(FunctionPosition pos);
32 
33  template <class Target, class Source> SafePtr<Target> require_dynamic_cast(const SafePtr<Source>& s) {
34  const SafePtr<Target> t = dynamic_pointer_cast<Target,Source>(s);
35  if (t == 0)
36  throw std::runtime_error("require_dynamic_cast: dynamic case failed");
37  return t;
38  }
39  template <class Target, class Source> const Target* require_dynamic_cast(const Source* s) {
40  const Target* t = dynamic_cast<Target*>(s);
41  if (t == 0)
42  throw std::runtime_error("require_dynamic_cast: dynamic case failed");
43  return t;
44  }
45 
47  template <typename T> std::string class_name(T* ptr=nullptr) {
48  int status = 1;
49  std::unique_ptr<char, void (*)(void*)>
50  result { abi::__cxa_demangle(ptr==nullptr?typeid(T).name():typeid(ptr).name(),NULL,NULL,&status),
51  std::free };
52  return status == 0 ? result.get() : "unknown";
53  }
54 
55 } // namespace libint2
56 
57 #endif /* header guard */
std::string class_name(T *ptr=nullptr)
Definition: util.h:47
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:23
std::string to_string(const T &x)
Converts x to its string representation.
Definition: entity.h:71