LIBINT 2.9.0
util.h
1/*
2 * Copyright (C) 2004-2024 Edward F. Valeev
3 *
4 * This file is part of Libint compiler.
5 *
6 * Libint compiler 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 compiler 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 compiler. 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 <cxxabi.h>
25#include <smart_ptr.h>
26#include <util_types.h>
27
28#include <numeric>
29#include <stdexcept>
30#include <string>
31
32namespace libint2 {
33std::string to_string(FunctionPosition pos);
34
35template <class Target, class Source>
36std::shared_ptr<Target> require_dynamic_cast(const std::shared_ptr<Source>& s) {
37 const std::shared_ptr<Target> t =
38 std::dynamic_pointer_cast<Target, Source>(s);
39 if (t == 0)
40 throw std::runtime_error("require_dynamic_cast: dynamic case failed");
41 return t;
42}
43template <class Target, class Source>
44const Target* require_dynamic_cast(const Source* s) {
45 const Target* t = dynamic_cast<Target*>(s);
46 if (t == 0)
47 throw std::runtime_error("require_dynamic_cast: dynamic case failed");
48 return t;
49}
50
52template <typename T>
53std::string class_name(T* ptr = nullptr) {
54 int status = 1;
55 std::unique_ptr<char, void (*)(void*)> result{
56 abi::__cxa_demangle(
57 ptr == nullptr ? typeid(T).name() : typeid(ptr).name(), NULL, NULL,
58 &status),
59 std::free};
60 return status == 0 ? result.get() : "unknown";
61}
62
63} // namespace libint2
64
65#endif /* header guard */
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
std::string class_name(T *ptr=nullptr)
Definition util.h:53
std::string to_string(const T &x)
Converts x to its string representation.
Definition entity.h:121