aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/SelectArg.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/SelectArg.hh')
-rw-r--r--src/FbTk/SelectArg.hh74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/FbTk/SelectArg.hh b/src/FbTk/SelectArg.hh
new file mode 100644
index 0000000..83626ad
--- /dev/null
+++ b/src/FbTk/SelectArg.hh
@@ -0,0 +1,74 @@
1// SelectArg.hh for FbTk
2// Copyright (c) 2010 Fluxbox Team (fluxgen at fluxbox dot org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22#ifndef FBTK_STLUTIL_SELECT_ARG_HH
23#define FBTK_STLUTIL_SELECT_ARG_HH
24
25#include "STLUtil.hh"
26
27namespace FbTk {
28
29namespace STLUtil {
30
31/**
32 * Selects a single argument from a maximum set of three arguments at compile time.
33 * For example:
34 * \begincode
35 * SelectArg<0>()( 10, 20, "hello" ); // returns first argument ( 10 )
36 * SelectArg<1>()( 10, "hello", 30 ); // returns second argument ( "hello" )
37 * SelectArg<2>()( 10, "hello", 30 ); // returns third argument ( 30 )
38 * \endcode
39 *
40 * The selection of argument 1 and 2 can return the last argument if the
41 * arguments are less or equal to two. For instance:
42 * \begincode
43 * SelectArg<1>()(10); // returns 10
44 * SelectArg<2>()(10, 20); // returns 20
45 * SelectArg<2>()(10); // returns 10
46 * \endcode
47 */
48template < int N >
49struct SelectArg {
50
51 template <typename Type1, typename Type2>
52 typename IfThenElse<N==0, Type1, Type2>::ResultType& operator ()(Type1& a, Type2& b){
53 return IfThenElse<N==0, Type1, Type2>()(a, b);
54 }
55
56 template <typename Type1, typename Type2, typename Type3>
57 typename IfThenElse<N==0, Type1,
58 typename IfThenElse<N==1, Type2, Type3>::ResultType>::ResultType&
59 operator () (Type1& a, Type2& b, Type3& c) {
60 return IfThenElse<N==0, Type1,
61 typename IfThenElse<N==1, Type2, Type3>::ResultType>()
62 (a, IfThenElse<N==1, Type2, Type3>() (b, c) );
63 }
64
65 template < typename Type1 >
66 Type1 operator() (Type1 a) {
67 return a;
68 }
69};
70
71} // end namespace STLUtil
72} // end namespace FbTk
73
74#endif // FBTK_STLUTIL_SELECT_ARG_HH