Bayeux  3.4.1
Core Foundation library for SuperNEMO
caster_utils.h
Go to the documentation of this file.
1 /* Author(s) : Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date : 2011-04-01
4  * Last modified : 2013-04-22
5  *
6  * Copyright (C) 2011-2013 Francois Mauger <mauger@lpccaen.in2p3.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or (at
11  * your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  * Description:
24  *
25  * Some templatized caster utilities and associated macros.
26  *
27  */
28 
29 #ifndef DATATOOLS_CASTER_UTILS_H
30 #define DATATOOLS_CASTER_UTILS_H
31 
32 // Standard Library:
33 #include <iostream>
34 #include <typeinfo>
35 #include <memory>
36 
37 // Third Party:
38 // - Boost:
39 // #include <boost/scoped_ptr.hpp>
40 
41 namespace datatools {
42 
46  template<typename From, typename ToBase>
47  struct i_caster
48  {
49  virtual ToBase * cast(From *) = 0;
50  };
51 
55  template <typename From, typename ToBase, typename ToDaughter>
56  struct caster
57  : public i_caster<From, ToBase>
58  {
59 
60  virtual ToDaughter * cast(From * ptr_) {
61  return reinterpret_cast<ToDaughter*>(ptr_);
62  }
63 
64  virtual ~caster() {}
65  };
66 
67  template<class Base, class Derived>
68  bool is_covariant(const Base & b_)
69  {
70  const Base * pb = &b_;
71  const Derived * dummy = dynamic_cast<const Derived *>(pb);
72  if (!dummy) {
73  return false;
74  }
75  return true;
76  }
77 
78 } // end of namespace datatools
79 
81 #define DATATOOLS_CASTER_DECLARATION(From,ToBase,ToDaughter,CasterId,CasterGetter) \
82  private: \
83  static std::unique_ptr<datatools::caster<From,ToBase,ToDaughter> > CasterId; \
84 public: \
85  virtual datatools::i_caster<From,ToBase>* CasterGetter() const; \
86 
87 
89 #define DATATOOLS_CASTER_IMPLEMENTATION(From,ToBase,ToDaughter,CasterId,CasterGetter) \
90  std::unique_ptr<datatools::caster<From,ToBase,ToDaughter> > ToDaughter::CasterId; \
91  datatools::i_caster<From,ToBase>* ToDaughter::CasterGetter() const { \
92  if (ToDaughter::CasterId.get() == 0) { \
93  ToDaughter::CasterId.reset(new datatools::caster<From,ToBase,ToDaughter>); \
94  } \
95  return ToDaughter::CasterId.get(); \
96  } \
97 
98 
99 #endif // DATATOOLS_CASTER_UTILS_H
100 
101 // Local Variables: --
102 // mode: c++ --
103 // c-file-style: "gnu" --
104 // tab-width: 2 --
105 // End: --
Templatized abstract interface class with a cast method using covariant return types .
Definition: caster_utils.h:47
virtual ToDaughter * cast(From *ptr_)
Definition: caster_utils.h:60
virtual ~caster()
Definition: caster_utils.h:64
bool is_covariant(const Base &b_)
Definition: caster_utils.h:68
Templatized concrete caster class for casting pointers from a covariant class hierarchy to some other...
Definition: caster_utils.h:56
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
virtual ToBase * cast(From *)=0