Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
mrtUtils.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef HEPUTILS_H_INCLUDED
24 #define HEPUTILS_H_INCLUDED
25 
26 #include <csl.h>
27 #include "mrtError.h"
28 
29 namespace mty{
30 
31 template<typename T, typename U>
32 bool IsOfType(std::shared_ptr<const U> const& expr)
33 {
34  auto const &object = *expr;
35  return typeid(object) == typeid(T);
36 }
37 
38 template<typename T, typename U>
39 bool IsOfType(U const* expr)
40 {
41  auto const &object = *expr;
42  return typeid(object) == typeid(T);
43 }
44 
45 template<typename T, typename U>
46 bool IsOfType(U const& expr)
47 {
48  auto const &object = *expr;
49  return typeid(object) == typeid(T);
50 }
51 
52 template<typename T, typename U>
53 std::shared_ptr<T> ConvertToShared(std::shared_ptr<U> const& expr)
54 {
55  auto ptr = std::dynamic_pointer_cast<T>(expr);
56  HEPAssert(ptr,
57  mty::error::TypeError,
58  "Converting an expr to the wrong type in"+
59  std::string("ConvertToShared<T>(const csl::Expr&): type =")+
60  typeid(T).name()+
61  ", expr = "+
62  toString(expr));
63  return ptr;
64 }
65 
66 template<typename T, typename U>
67 T* ConvertToPtr(std::shared_ptr<U> const& expr)
68 {
69  return ConvertToShared<T>(expr).get();
70 }
71 
72 template<typename T, typename U>
73 const T* ConvertToPtr(const U* expr)
74 {
75  auto ptr = dynamic_cast<const T*>(expr);
76  HEPAssert(ptr,
77  mty::error::TypeError,
78  "Converting an expr to the wrong type in"+
79  std::string("ConvertToShared<T>(const csl::Expr&): type =")+
80  typeid(T).name()+
81  ", expr = "+
82  toString(expr));
83 
84  return ptr;
85 }
86 
87 
88 template<typename T, typename U>
89 T ConvertTo(std::shared_ptr<U> const& expr)
90 {
91  return *ConvertToPtr<T>(expr);
92 }
93 
94 template<typename T, typename U>
95 T& ConvertTo(std::shared_ptr<U>& expr)
96 {
97  return *ConvertToPtr<T>(expr);
98 }
99 
100 template<typename T, typename U>
101 T* ConvertToPtr(U* parent)
102 {
103  return dynamic_cast<T*>(parent);
104 }
105 
106 } // End of namespace mty
107 
108 #endif
Namespace of MARTY.
Definition: 2HDM.h:31