Awali
Another Weighted Automata library
cast.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_MISC_CAST_HH
18 # define AWALI_MISC_CAST_HH
19 
20 # include <iostream>
21 
22 # ifdef NDEBUG
23 # define down_cast static_cast
24 # define down_pointer_cast std::static_pointer_cast
25 # else
26 
27 // We want to support the "down_cast<FOO>(BAR)" syntax (with a pair of
28 // <> and of ()). So expand "down_cast" into a templated function.
29 // Actually, make it a functor that can be given the current location.
30 #define down_cast \
31  ::awali::sttc::internal::down_caster(__FILE__, __LINE__).cast
32 #define down_pointer_cast down_cast
33 
34 namespace awali { namespace sttc {
35 
36  namespace internal
37  {
40  struct down_caster
41  {
42  down_caster(const char* f, int l)
43  : file(f), line(l)
44  {}
45  const char* file;
46  int line;
47 
48  template <typename T, typename U>
49  void
50  error(const char* msg)
51  {
52  std::cerr
53  << file << ':' << line << ": " << msg
54  << " from " << typeid(U).name() << " to " << typeid(T).name()
55  << std::endl;
56  abort();
57  }
58 
59  template <typename T, typename U>
60  inline
61  T
62  cast(U t)
63  {
64  if (!t)
65  error<T, U>("down_casting nullptr");
66  T res = dynamic_cast<const T>(t);
67  if (!res)
68  error<T, U>("failed down_cast");
69  return res;
70  }
71 
73  template <typename T, typename U>
74  inline
75  std::shared_ptr<T>
76  cast(std::shared_ptr<U> t)
77  {
78  if (!t)
79  error<std::shared_ptr<T>, std::shared_ptr<U>>("down_casting nullptr");
80  std::shared_ptr<T> res = std::dynamic_pointer_cast<T>(t);
81  if (!res)
82  error<std::shared_ptr<T>, std::shared_ptr<U>>("failed down_cast");
83  return res;
84  }
85  };
86  }
87 
88 }}//end of ns awali::stc
89 #endif
90 
91 #endif // !AWALI_MISC_CAST_HH
Main namespace of Awali.
Definition: ato.hh:22
A functor that captures the current location (to report errors), and provides "cast".
Definition: cast.hh:41
down_caster(const char *f, int l)
Definition: cast.hh:42
const char * file
Definition: cast.hh:45
void error(const char *msg)
Definition: cast.hh:50
T cast(U t)
Definition: cast.hh:62
int line
Definition: cast.hh:46
std::shared_ptr< T > cast(std::shared_ptr< U > t)
Special case for shared_ptr.
Definition: cast.hh:76