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 parameter arguments for template functions
49  *
50  * @details This struct uses template partial specialisation in order
51  * to provide automatic type mapping for arguments for templated
52  * functions. It is used by callback object constructors, the unbound
53  * arguments of callback objects and their related functors and
54  * emitter objects, the Callback::make_ref() functions, the
55  * Thread::Future::make() functions and the DoIf::mem_fun() and
56  * DoIf::fun() functions. With Callback::make_ref(),
57  * Thread::Future::make(), DoIf::mem_fun() and DoIf::fun(), its use
58  * avoids errors arising from a template function being instantiated
59  * for a reference type, and also ensures that value arguments are
60  * passed by reference to const to avoid unnecessary copying.
61  *
62  * Mapping is as follows:
63  *
64  * A value argument is mapped to reference to const of the value
65  * type.
66  *
67  * A pointer argument is mapped to pointer argument (its original
68  * type).
69  *
70  * A non-const reference argument is mapped to non-const reference
71  * (its original type).
72  *
73  * A const reference argument is mapped to const reference (its
74  * original type).
75  *
76  * Since 1.2.10
77  */
78 template<class T>
79 struct Param {
80  typedef const T& ParamType;
81 };
82 
83 template<class T>
84 struct Param<T&> {
85  typedef T& ParamType;
86 };
87 
88 template<class T>
89 struct Param<T*> {
90  typedef T* ParamType;
91 };
92 
93 /**
94  * @class RemoveRefCond param.h c++-gtk-utils/param.h
95  * @brief Struct which will conditionally convert a reference type to
96  * a value type
97  *
98  * @details This struct is used by Callback::make(),
99  * Callback::make_val() and Callback::make_ref() so that where a call
100  * is made to Callback::make_ref() and the target function's arguments
101  * include one with a reference, that reference is removed.
102  *
103  * Since 1.2.13
104  */
105 template<class T, bool unref>
106 struct RemoveRefCond {};
107 
108 template<class T>
109 struct RemoveRefCond<T, true> {
110  typedef T Type;
111 };
112 
113 template<class T>
114 struct RemoveRefCond<T&, true> {
115  typedef T Type;
116 };
117 
118 template<class T>
119 struct RemoveRefCond<T, false> {
120  typedef T Type;
121 };
122 
123 // explicatory only - we could do without this specialisation
124 template<class T>
125 struct RemoveRefCond<T&, false> {
126  typedef T& Type;
127 };
128 
129 /**
130  * @class TypeTuple param.h c++-gtk-utils/param.h
131  * @brief Struct type to provide unbound arguments for
132  * Callback::CallbackArg objects.
133  *
134  * @details This struct is used with template partial specialisation
135  * of Callback::CallbackArg, Callback::FunctorArg and EmitterArg
136  * objects to enable them to take up to five unbound arguments,
137  * although at present these classes are only specialised to take two
138  * or three unbound arguments. This struct has no members and is
139  * never instantiated: it is just used as a placeholder for its
140  * contained types.
141  *
142  * As the compiled part of this library does not use this struct (it
143  * just makes it available), the advantage of specialising the
144  * (single) template type of Callback::CallbackArg,
145  * Callback::FunctorArg and EmitterArg via this struct, rather than
146  * specialising them directly, is that this struct can be extended (or
147  * have Loki-like typelists substituted) without breaking binary
148  * compatibility. The types have default type values of void,
149  * indicating no argument, so this struct is capable of representing
150  * any number of arguments from 0 to 5.
151  *
152  * Since 1.2.10
153  */
154 template <class T1 = void, class T2 = void, class T3 = void,
155  class T4 = void, class T5 = void> struct TypeTuple {};
156 
157 } // namespace Cgu
158 
159 #endif // CGU_PARAM_H
Cgu::Param
Struct for automatic typing of parameter arguments for template functions.
Definition: param.h:79
Cgu::Param< T & >::ParamType
T & ParamType
Definition: param.h:85
Cgu
Definition: application.h:45
Cgu::Param::ParamType
const typedef T & ParamType
Definition: param.h:80
Cgu::RemoveRefCond< T, false >::Type
T Type
Definition: param.h:120
Cgu::RemoveRefCond
Struct which will conditionally convert a reference type to a value type.
Definition: param.h:106
Cgu::Param< T * >::ParamType
T * ParamType
Definition: param.h:90
Cgu::RemoveRefCond< T &, false >::Type
T & Type
Definition: param.h:126
Cgu::RemoveRefCond< T &, true >::Type
T Type
Definition: param.h:115
Cgu::TypeTuple
Struct type to provide unbound arguments for Callback::CallbackArg objects.
Definition: param.h:155
Cgu::RemoveRefCond< T, true >::Type
T Type
Definition: param.h:110
cgu_config.h