Bayeux  3.4.1
Core Foundation library for SuperNEMO
i_cloneable.h
Go to the documentation of this file.
1 /* Author(s) : Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date : 2011-03-24
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  * An abstract interface for cloneable objects
26  *
27  */
28 
29 #ifndef DATATOOLS_I_CLONEABLE_H
30 #define DATATOOLS_I_CLONEABLE_H
31 
32 // Standard Library:
33 #include <iostream>
34 #include <typeinfo>
35 
36 // This Project:
37 #include <datatools/caster_utils.h>
38 
39 namespace datatools {
40 
41  class i_serializable; // forward declaration
42 
44 
76  {
77  public:
82  virtual i_cloneable* clone(void) const = 0;
83 
86  virtual ~i_cloneable()
87  {
88  return;
89  };
90 
93  template<class Copyable>
94  static Copyable* clone_it(const Copyable& a_copyable)
95  {
96  return new Copyable(a_copyable);
97  }
98 
99  template<class CandidateType>
100  bool is_cloneable(const CandidateType & candidate_)
101  {
102  const datatools::i_cloneable * tc = dynamic_cast<const datatools::i_cloneable *>(&candidate_);
103  return tc != nullptr;
104  }
105 
106  };
107 
108 } // end of namespace datatools
109 
110 //----------------------------------------------------------------------
111 // i_cloneable macros
112 
113 #define DATATOOLS_CLONEABLE_DECLARATION(Copyable) \
114  public: \
115  virtual Copyable* clone(void) const; \
116 
117 
118 #define DATATOOLS_CLONEABLE_INLINE(Copyable) \
119  public: \
120  virtual Copyable* clone(void) const { \
121  return datatools::i_cloneable::clone_it<Copyable>(*this); \
122  }
123 
124 
125 #define DATATOOLS_CLONEABLE_IMPLEMENTATION(Copyable) \
126  Copyable* Copyable::clone(void) const { \
127  return datatools::i_cloneable::clone_it<Copyable>(*this); \
128  } \
129 
130 
131 #define DATATOOLS_CLONEABLE_TO_SERIALIZABLE_CASTER_DECLARATION(Cloneable) \
132  DATATOOLS_CASTER_DECLARATION(datatools::i_cloneable,datatools::i_serializable, Cloneable,g_cloneable_to_serializable_caster,get_cloneable_to_serializable_caster) \
133 
134 
135 #define DATATOOLS_CLONEABLE_TO_SERIALIZABLE_CASTER_IMPLEMENTATION(Cloneable) \
136  DATATOOLS_CASTER_IMPLEMENTATION(datatools::i_cloneable,datatools::i_serializable, Cloneable,g_cloneable_to_serializable_caster,get_cloneable_to_serializable_caster) \
137 
138 
139 #endif // DATATOOLS_I_CLONEABLE_H
140 
141 // Local Variables: --
142 // mode: c++ --
143 // c-file-style: "gnu" --
144 // tab-width: 2 --
145 // End: --
bool is_cloneable(const CandidateType &candidate_)
Definition: i_cloneable.h:100
virtual ~i_cloneable()
Definition: i_cloneable.h:86
A pure abstract class (interface) for inherited cloneable classes.
Definition: i_cloneable.h:75
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
virtual i_cloneable * clone(void) const =0
static Copyable * clone_it(const Copyable &a_copyable)
Definition: i_cloneable.h:94