HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_LHSTuple.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS:
7  * This file defines a number of functions named UTlhsTuple() (tuples
8  * that are valid left hand sides in assignments). These functions
9  * are inspired by python, where it's very easy to return multiple values
10  * from a function:
11  *
12  * def foo():
13  * return ([0, 1, 2], 6)
14  *
15  * (a, b) = foo()
16  * ...do something with a and b...
17  *
18  * The C++ equivalent works with boost tuples, and looks like:
19  *
20  * static hboost::tuple<UT_Vector3, int> foo()
21  * { return hboost::make_tuple(UT_Vector3(0, 1, 2), 6); }
22  *
23  * UT_Vector3 a;
24  * int b;
25  * UTlhsTuple(a, b) = foo();
26  * ...do something with a and b...
27  *
28  * One handy use of these functions is when you want to pass multiple
29  * values into a callback function that takes a void *. Instead of
30  * creating a struct to hold the values, pass in a pointer to a
31  * hboost::tuple:
32  * void iterateOverSomething(
33  * void (*callback)(void *), void *callback_data));
34  *
35  * static void my_callback(void *data)
36  * {
37  * UT_Vector3 a;
38  * int b;
39  * UTlhsTuple(a, b) = *(hboost::tuple<UT_Vector3, int>*)data;
40  * ...do something with a and b...
41  * }
42  *
43  * hboost::tuple<UT_Vector3, int> data(UT_Vector3(1,2,3), 4);
44  * iterateOverSomething(my_callback, &data);
45  */
46 
47 #ifndef HBOOST_PP_IS_ITERATING
48 #ifndef __UT_LHSTuple_h__
49 #define __UT_LHSTuple_h__
50 
51 #include <hboost/preprocessor/repetition/repeat.hpp>
52 #include <hboost/preprocessor/iteration/iterate.hpp>
53 #include <hboost/preprocessor/punctuation/comma_if.hpp>
54 #include <hboost/preprocessor/arithmetic/sub.hpp>
55 #include <hboost/preprocessor/comparison/less.hpp>
56 #include <hboost/preprocessor/repetition/enum_params.hpp>
57 #include <hboost/preprocessor/repetition/enum_binary_params.hpp>
58 
59 #include <hboost/tuple/tuple.hpp>
60 
61 // Including HBOOST_PP_ITERATE() will make this file get included again
62 // over and over, with HBOOST_PP_IS_ITERATING set and HBOOST_PP_ITERATION()
63 // set to a different value each time.
64 //
65 // We need to use different includes depending on whether we're being
66 // included from UT or not.
67 #define HBOOST_PP_ITERATION_LIMITS (2, 10)
68 #ifdef UT_EXPORTS
69 #define HBOOST_PP_FILENAME_1 "UT_LHSTuple.h"
70 #else
71 #define HBOOST_PP_FILENAME_1 <UT/UT_LHSTuple.h>
72 #endif
73 #include HBOOST_PP_ITERATE()
74 
75 #endif // __UT_LHSTuple_h__
76 
77 
78 #else // HBOOST_PP_IS_ITERATING
79 
80 
81 #define n HBOOST_PP_ITERATION()
82 
83 // This macro creates output of the form:
84 // T1&, T2&, T3&
85 #define TYPE_REF_LIST(z, num, p) T ## num & \
86  HBOOST_PP_COMMA_IF(HBOOST_PP_LESS(num, HBOOST_PP_SUB(n, 1)))
87 
88 // This macro creates output of the form:
89 // hboost::ref(parm1), hboost::ref(parm2), hboost::ref(parm3)
90 // hboost::ref ensures that make_tuple creates a tuple of references.
91 #define PARAMS_AS_REF_LIST(z, num, p) hboost::ref(parm ## num) \
92  HBOOST_PP_COMMA_IF(HBOOST_PP_LESS(num, HBOOST_PP_SUB(n, 1)))
93 
94 // Overload a function named UTlhsTuple() that takes different numbers of
95 // arguments of any type and returns a hboost::tuple of references to the
96 // parameters passed it. You can then assign another tuple to the return
97 // value of this function, causing it to assign to each of the variables
98 // that were passed into the function.
99 template <HBOOST_PP_ENUM_PARAMS(n, typename T)>
100 hboost::tuple<HBOOST_PP_REPEAT(n, TYPE_REF_LIST, _)>
101 UTlhsTuple(HBOOST_PP_ENUM_BINARY_PARAMS(n, T, &parm))
102 {
103  return hboost::make_tuple(HBOOST_PP_REPEAT(n, PARAMS_AS_REF_LIST, _));
104 }
105 
106 #undef TYPE_REF_LIST
107 #undef PARAMS_AS_REF_LIST
108 #undef n
109 
110 #endif // HBOOST_PP_IS_ITERATING
GLdouble n
Definition: glcorearb.h:2007