Documentation of CSL
cast.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 CAST_H_INCLUDED
24 #define CAST_H_INCLUDED
25 
26 #include <typeinfo>
27 #include <type_traits>
28 #include "abstract.h"
29 #include "utils.h"
30 #include "error.h"
31 
32 namespace csl {
33 
34 template<class T>
35 T pointer_to_object(const Expr& expr)
36 {
37  auto const &pointed = *expr;
38  if (typeid(T) != typeid(pointed)) {
39  std::cout << typeid(T).name() << "\n"
40  << typeid(pointed).name() << "\n";
41  callError(cslError::UndefinedBehaviour,
42  "pointer_cast(const Expr&).");
43  }
44 
45  return *static_cast<T*>(expr.get());
46 }
47 
48 template<class T>
49 T& pointer_to_object_ref(const Expr& expr)
50 {
51  auto const &pointed = *expr;
52  if (typeid(T) != typeid(pointed))
53  callError(cslError::UndefinedBehaviour,
54  "pointer_cast_ref(const Expr&).");
55 
56  return *static_cast<T*>(expr.get());
57 }
58 
59 template<typename T>
60 T* shared_to_raw_ptr(const Expr_c& expr)
61 {
62  auto const &pointed = *expr;
63  if (typeid(T) != typeid(pointed))
64  callError(cslError::UndefinedBehaviour,
65  "shared_to_raw_ptr(const Expr_c&).");
66 
67  return static_cast<T*>(expr.get());
68 }
69 
70 
71 template<typename T>
72 Expr object_to_shared(T& csl_expr)
73 {
74  static_assert(std::is_base_of<Abstract, T>::value);
75 
76  return csl_expr.self();
77 }
78 
79 } // End of namespace csl
80 
81 #endif
Namespace for csl library.
Definition: abreviation.h:34
File containing functions that are called by the program when something wrong happened: determines th...
void callError(cslError::Error error, const std::string &caller, T spec)
Displays an error message depending on the error error, the name of the caller function caller and a ...
Definition: error.h:115
Base classes for all exprs in the program.