c++-gtk-utils
param.h
Go to the documentation of this file.
1 /* Copyright (C) 2010 and 2011 Chris Vine
2 
3 The library comprised in this file or of which this file is part is
4 distributed by Chris Vine under the GNU Lesser General Public
5 License as follows:
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public License
9  as published by the Free Software Foundation; either version 2.1 of
10  the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License, version 2.1, for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License, version 2.1, along with this library (see the file LGPL.TXT
19  which came with this source code package in the c++-gtk-utils
20  sub-directory); if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 However, it is not intended that the object code of a program whose
24 source code instantiates a template from this file or uses macros or
25 inline functions (of any length) should by reason only of that
26 instantiation or use be subject to the restrictions of use in the GNU
27 Lesser General Public License. With that in mind, the words "and
28 macros, inline functions and instantiations of templates (of any
29 length)" shall be treated as substituted for the words "and small
30 macros and small inline functions (ten lines or less in length)" in
31 the fourth paragraph of section 5 of that licence. This does not
32 affect any other reason why object code may be subject to the
33 restrictions in that licence (nor for the avoidance of doubt does it
34 affect the application of section 2 of that licence to modifications
35 of the source code in this file).
36 
37 */
38 
39 #ifndef CGU_PARAM_H
40 #define CGU_PARAM_H
41 
43 
44 namespace Cgu {
45 
46 /**
47  * @class Param param.h c++-gtk-utils/param.h
48  * @brief Struct for automatic typing of function parameter arguments
49  *
50  * @details This struct uses template partial specialisation in order
51  * to provide automatic type mapping for function arguments. It is
52  * used by the unbound arguments of callback objects and their related
53  * functors and emitter objects. (A more detailed explanation is that
54  * it is used where a virtual function is at the end of a call chain,
55  * notably the dispatch() method of callback objects, with the result
56  * that we cannot templatise the virtual function itself in order to
57  * use std::forward for argument passing, but instead have to type-map
58  * the arguments using class templates.)
59  *
60  * Mapping is as follows:
61  *
62  * A value argument is mapped to reference to const of the value
63  * type.
64  *
65  * A pointer argument is mapped to pointer argument (its original
66  * type).
67  *
68  * A non-const reference argument is mapped to non-const reference
69  * (its original type).
70  *
71  * A const reference argument is mapped to const reference (its
72  * original type).
73  */
74 
75 template<class T>
76 struct Param {
77  typedef const T& ParamType;
78 };
79 
80 template<class T>
81 struct Param<T&> {
82  typedef T& ParamType;
83 };
84 
85 template<class T>
86 struct Param<T*> {
87  typedef T* ParamType;
88 };
89 
90 /**
91  * @class RemoveRefCond param.h c++-gtk-utils/param.h
92  * @brief Struct which will conditionally convert a reference type to
93  * a value type
94  *
95  * @details This struct is used by Callback::make(),
96  * Callback::make_val() and Callback::make_ref() so that where a call
97  * is made to Callback::make_ref() and the target function's arguments
98  * include one with a reference, that reference is removed.
99  *
100  * Since 2.0.0-rc3
101  */
102 template<class T, bool unref>
103 struct RemoveRefCond {};
104 
105 template<class T>
106 struct RemoveRefCond<T, true> {
107  typedef T Type;
108 };
109 
110 template<class T>
111 struct RemoveRefCond<T&, true> {
112  typedef T Type;
113 };
114 
115 template<class T>
116 struct RemoveRefCond<T, false> {
117  typedef T Type;
118 };
119 
120 // explicatory only - we could do without this specialisation
121 template<class T>
122 struct RemoveRefCond<T&, false> {
123  typedef T& Type;
124 };
125 
126 } // namespace Cgu
127 
128 #endif // CGU_PARAM_H
Cgu::Param
Struct for automatic typing of function parameter arguments.
Definition: param.h:76
Cgu::Param< T & >::ParamType
T & ParamType
Definition: param.h:82
Cgu
Definition: application.h:44
Cgu::Param::ParamType
const typedef T & ParamType
Definition: param.h:77
Cgu::RemoveRefCond< T, false >::Type
T Type
Definition: param.h:117
Cgu::RemoveRefCond
Struct which will conditionally convert a reference type to a value type.
Definition: param.h:103
Cgu::Param< T * >::ParamType
T * ParamType
Definition: param.h:87
Cgu::RemoveRefCond< T &, false >::Type
T & Type
Definition: param.h:123
Cgu::RemoveRefCond< T &, true >::Type
T Type
Definition: param.h:112
Cgu::RemoveRefCond< T, true >::Type
T Type
Definition: param.h:107
cgu_config.h