Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Conversion class definition file -*- C++ -*-
/* Copyright 2008 Mike Williams (mwill@jlab.org)
*
* This file is part of qft++.
*
* qft++ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* qft++ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with qft++. If not, see <http://www.gnu.org/licenses/>.
*/
// Author: Mike Williams
#ifndef _Conversion_H
#define _Conversion_H
//_____________________________________________________________________________
/** @file Conversion.h
* @brief Conversion class definition file (also defines IsDerived macro).
*/
//_____________________________________________________________________________
/** @class Conversion
* @author Mike Williams
*
* @brief Internal template class used to determine type convertibility.
*
* Note: This class follows very closey the work of Andrei Alexandrescu from
* <em>Modern C++ Design</em>, Ch. 2, Sec. 7.
*
* Conversion<T,U> determines whether or not a converion from T to U exists.
* If such a conversion does exist, then Conversion<T,U>::exists is @a true,
* otherwise it's @a false. If T and U are the same type, then
* Conversion<T,U>::same_type is @a true, otherwise it's @a false.
*
* <b> Example Usage </b>
*
* \include Conversion.ex
*/
//_____________________________________________________________________________
using namespace std;
//_____________________________________________________________________________
// default template:
template <typename _T,typename _U> class Conversion {
private:
#ifndef DOXYGEN_SKIP_THIS
class _big {char dummy[2];};
#endif
typedef char _small_type; ///< a type with size 1
typedef _big _big_type; ///< a type with size > 1
static _small_type _Test(_U); ///< return _small_type
static _big_type _Test(...); ///< return _big_type
static _T _MakeT(); ///< make an object of type _T
public:
/// Does a converion of @a T to @a U exist?
static const bool exists = sizeof(_Test(_MakeT())) == sizeof(_small_type);
/// Are @a T and @a U the same type?
static const bool same_type = false;
};
//_____________________________________________________________________________
#ifndef DOXYGEN_SKIP_THIS
// specialization for _T = _U:
template <typename _T> class Conversion<_T,_T> {
public:
static const bool exists = true;
static const bool same_type = true;
};
#endif /* DOXYGEN_SKIP_THIS */
//_____________________________________________________________________________
/// Does @a Derived inherit from @a Base? (note IsDerived(T,T) is @a true)
#define IsDerived(Derived,Base) (Conversion<const Derived*,const Base*>::exists && !Conversion<const Derived*,const void*>::same_type)
#include "qft++/c++-template-utils/Type.hh"
/// Should @a T be passed by value?
#define PassByValue(__T) Conversion<__T,typename Type<__T>::ParamType>::same_type
//_____________________________________________________________________________
#endif /* _Conversion_H */